diff --git a/README.MD b/README.MD index e12a4d4..cf134b9 100644 --- a/README.MD +++ b/README.MD @@ -16,6 +16,7 @@ Below are a list of the included namespaces with links to their README files: - [SabreTools.Logging](https://github.com/SabreTools/SabreTools.IO/tree/main/SabreTools.Logging) - [SabreTools.Matching](https://github.com/SabreTools/SabreTools.IO/tree/main/SabreTools.Matching) - [SabreTools.Numerics](https://github.com/SabreTools/SabreTools.IO/tree/main/SabreTools.Numerics) +- [SabreTools.Numerics.Extensions](https://github.com/SabreTools/SabreTools.IO/tree/main/SabreTools.Numerics.Extensions) - [SabreTools.Security.Cryptography](https://github.com/SabreTools/SabreTools.IO/tree/main/SabreTools.Security.Cryptography) - [SabreTools.Text.ClrMamePro](https://github.com/SabreTools/SabreTools.IO/tree/main/SabreTools.Text.ClrMamePro) - [SabreTools.Text.Compare](https://github.com/SabreTools/SabreTools.IO/tree/main/SabreTools.Text.Compare) diff --git a/SabreTools.IO.Compression/Deflate/InflateWrapper.cs b/SabreTools.IO.Compression/Deflate/InflateWrapper.cs index caf2f46..c7eda91 100644 --- a/SabreTools.IO.Compression/Deflate/InflateWrapper.cs +++ b/SabreTools.IO.Compression/Deflate/InflateWrapper.cs @@ -3,6 +3,7 @@ using System.IO; using System.Text; using SabreTools.Hashing; using SabreTools.IO.Extensions; +using SabreTools.Numerics.Extensions; namespace SabreTools.IO.Compression.Deflate { diff --git a/SabreTools.IO.Compression/MSZIP/Decompressor.cs b/SabreTools.IO.Compression/MSZIP/Decompressor.cs index c25726a..708f5a1 100644 --- a/SabreTools.IO.Compression/MSZIP/Decompressor.cs +++ b/SabreTools.IO.Compression/MSZIP/Decompressor.cs @@ -1,6 +1,6 @@ using System; using System.IO; -using SabreTools.IO.Extensions; +using SabreTools.Numerics.Extensions; namespace SabreTools.IO.Compression.MSZIP { diff --git a/SabreTools.IO.Compression/SabreTools.IO.Compression.csproj b/SabreTools.IO.Compression/SabreTools.IO.Compression.csproj index cd5e1a7..c0e99a5 100644 --- a/SabreTools.IO.Compression/SabreTools.IO.Compression.csproj +++ b/SabreTools.IO.Compression/SabreTools.IO.Compression.csproj @@ -39,6 +39,7 @@ + diff --git a/SabreTools.IO.Extensions.Test/BinaryReaderExtensionsTests.cs b/SabreTools.IO.Extensions.Test/BinaryReaderExtensionsTests.cs index c3dd359..d3d1620 100644 --- a/SabreTools.IO.Extensions.Test/BinaryReaderExtensionsTests.cs +++ b/SabreTools.IO.Extensions.Test/BinaryReaderExtensionsTests.cs @@ -3,7 +3,6 @@ using System.IO; using System.Linq; using System.Numerics; using System.Text; -using SabreTools.Numerics; using Xunit; namespace SabreTools.IO.Extensions.Test @@ -19,770 +18,6 @@ namespace SabreTools.IO.Extensions.Test 0x08, 0x09, 0x0A, 0x0B, 0x0C, 0x0D, 0x0E, 0x0F, ]; - /// - /// Represents the decimal value 0.0123456789 - /// - private static readonly byte[] _decimalBytes = - [ - 0x15, 0xCD, 0x5B, 0x07, 0x00, 0x00, 0x00, 0x00, - 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x0A, 0x00, - ]; - - /// - /// Test pattern for big-endian GUID created from - /// - private static readonly byte[] _guidBigEndianbytes = - [ - 0x03, 0x02, 0x01, 0x00, 0x05, 0x04, 0x07, 0x06, - 0x08, 0x09, 0x0A, 0x0B, 0x0C, 0x0D, 0x0E, 0x0F, - ]; - - #region Exact Read - - [Fact] - public void ReadByteArrayTest() - { - byte[] arr = new byte[4]; - var stream = new MemoryStream(_bytes); - var br = new BinaryReader(stream); - int read = br.Read(arr, 0, 4); - Assert.Equal(4, read); - Assert.True(arr.SequenceEqual(_bytes.Take(4))); - } - - [Fact] - public void ReadCharArrayTest() - { - char[] arr = new char[4]; - var stream = new MemoryStream(_bytes); - var br = new BinaryReader(stream); - int read = br.Read(arr, 0, 4); - Assert.Equal(4, read); - Assert.True(arr.SequenceEqual(_bytes.Take(4).Select(b => (char)b))); - } - - [Fact] - public void ReadByteTest() - { - var stream = new MemoryStream(_bytes); - var br = new BinaryReader(stream); - byte read = br.ReadByte(); - Assert.Equal(0x00, read); - } - - [Fact] - public void ReadByteBothEndianTest() - { - var stream = new MemoryStream(_bytes); - var br = new BinaryReader(stream); - BothUInt8 read = br.ReadByteBothEndian(); - Assert.Equal(0x00, read.LittleEndian); - Assert.Equal(0x01, read.BigEndian); - } - - [Fact] - public void ReadBytesTest() - { - var stream = new MemoryStream(_bytes); - var br = new BinaryReader(stream); - int length = 4; - byte[] read = br.ReadBytes(length); - Assert.Equal(length, read.Length); - Assert.True(read.SequenceEqual(_bytes.Take(length))); - } - - [Fact] - public void ReadCharsTest() - { - var stream = new MemoryStream(_bytes); - var br = new BinaryReader(stream); - int length = 4; - char[] read = br.ReadChars(length); - Assert.Equal(length, read.Length); - Assert.True(read.SequenceEqual(_bytes.Take(length).Select(b => (char)b))); - } - - [Fact] - public void ReadSByteTest() - { - var stream = new MemoryStream(_bytes); - var br = new BinaryReader(stream); - sbyte read = br.ReadSByte(); - Assert.Equal(0x00, read); - } - - [Fact] - public void ReadSByteBothEndianTest() - { - var stream = new MemoryStream(_bytes); - var br = new BinaryReader(stream); - BothInt8 read = br.ReadSByteBothEndian(); - Assert.Equal(0x00, read.LittleEndian); - Assert.Equal(0x01, read.BigEndian); - } - - [Fact] - public void ReadCharTest() - { - var stream = new MemoryStream(_bytes); - var br = new BinaryReader(stream); - char read = br.ReadChar(); - Assert.Equal('\0', read); - } - - [Fact] - public void ReadInt16Test() - { - var stream = new MemoryStream(_bytes); - var br = new BinaryReader(stream); - short read = br.ReadInt16(); - Assert.Equal(0x0100, read); - } - - [Fact] - public void ReadInt16BigEndianTest() - { - var stream = new MemoryStream(_bytes); - var br = new BinaryReader(stream); - short read = br.ReadInt16BigEndian(); - Assert.Equal(0x0001, read); - } - - [Fact] - public void ReadInt16LittleEndianTest() - { - var stream = new MemoryStream(_bytes); - var br = new BinaryReader(stream); - short read = br.ReadInt16LittleEndian(); - Assert.Equal(0x0100, read); - } - - [Fact] - public void ReadInt16BothEndianTest() - { - var stream = new MemoryStream(_bytes); - var br = new BinaryReader(stream); - BothInt16 read = br.ReadInt16BothEndian(); - Assert.Equal(0x0100, read.LittleEndian); - Assert.Equal(0x0203, read.BigEndian); - } - - [Fact] - public void ReadUInt16Test() - { - var stream = new MemoryStream(_bytes); - var br = new BinaryReader(stream); - ushort read = br.ReadUInt16(); - Assert.Equal(0x0100, read); - } - - [Fact] - public void ReadUInt16BigEndianTest() - { - var stream = new MemoryStream(_bytes); - var br = new BinaryReader(stream); - ushort read = br.ReadUInt16BigEndian(); - Assert.Equal(0x0001, read); - } - - [Fact] - public void ReadUInt16LittleEndianTest() - { - var stream = new MemoryStream(_bytes); - var br = new BinaryReader(stream); - ushort read = br.ReadUInt16LittleEndian(); - Assert.Equal(0x0100, read); - } - - [Fact] - public void ReadUInt16BothEndianTest() - { - var stream = new MemoryStream(_bytes); - var br = new BinaryReader(stream); - BothUInt16 read = br.ReadUInt16BothEndian(); - Assert.Equal(0x0100, read.LittleEndian); - Assert.Equal(0x0203, read.BigEndian); - } - - [Fact] - public void ReadWORDTest() - { - var stream = new MemoryStream(_bytes); - var br = new BinaryReader(stream); - ushort read = br.ReadWORD(); - Assert.Equal(0x0100, read); - } - - [Fact] - public void ReadWORDBigEndianTest() - { - var stream = new MemoryStream(_bytes); - var br = new BinaryReader(stream); - ushort read = br.ReadWORDBigEndian(); - Assert.Equal(0x0001, read); - } - - [Fact] - public void ReadWORDLittleEndianTest() - { - var stream = new MemoryStream(_bytes); - var br = new BinaryReader(stream); - ushort read = br.ReadWORDLittleEndian(); - Assert.Equal(0x0100, read); - } - - [Fact] - public void ReadWORDBothEndianTest() - { - var stream = new MemoryStream(_bytes); - var br = new BinaryReader(stream); - BothUInt16 read = br.ReadWORDBothEndian(); - Assert.Equal(0x0100, read.LittleEndian); - Assert.Equal(0x0203, read.BigEndian); - } - - [Fact] - public void ReadHalfTest() - { - var stream = new MemoryStream(_bytes); - var br = new BinaryReader(stream); - Half expected = BitConverter.Int16BitsToHalf(0x0100); - Half read = br.ReadHalf(); - Assert.Equal(expected, read); - } - - [Fact] - public void ReadHalfBigEndianTest() - { - var stream = new MemoryStream(_bytes); - var br = new BinaryReader(stream); - Half expected = BitConverter.Int16BitsToHalf(0x0001); - Half read = br.ReadHalfBigEndian(); - Assert.Equal(expected, read); - } - - [Fact] - public void ReadHalfLittleEndianTest() - { - var stream = new MemoryStream(_bytes); - var br = new BinaryReader(stream); - Half expected = BitConverter.Int16BitsToHalf(0x0100); - Half read = br.ReadHalfLittleEndian(); - Assert.Equal(expected, read); - } - - [Fact] - public void ReadInt24Test() - { - var stream = new MemoryStream(_bytes); - var br = new BinaryReader(stream); - Int24 read = br.ReadInt24(); - Assert.Equal(0x020100, (int)read); - } - - [Fact] - public void ReadInt24BigEndianTest() - { - var stream = new MemoryStream(_bytes); - var br = new BinaryReader(stream); - Int24 read = br.ReadInt24BigEndian(); - Assert.Equal(0x000102, (int)read); - } - - [Fact] - public void ReadInt24LittleEndianTest() - { - var stream = new MemoryStream(_bytes); - var br = new BinaryReader(stream); - Int24 read = br.ReadInt24LittleEndian(); - Assert.Equal(0x020100, (int)read); - } - - [Fact] - public void ReadUInt24Test() - { - var stream = new MemoryStream(_bytes); - var br = new BinaryReader(stream); - UInt24 read = br.ReadUInt24(); - Assert.Equal((uint)0x020100, (uint)read); - } - - [Fact] - public void ReadUInt24BigEndianTest() - { - var stream = new MemoryStream(_bytes); - var br = new BinaryReader(stream); - UInt24 read = br.ReadUInt24BigEndian(); - Assert.Equal((uint)0x000102, (uint)read); - } - - [Fact] - public void ReadUInt24LittleEndianTest() - { - var stream = new MemoryStream(_bytes); - var br = new BinaryReader(stream); - UInt24 read = br.ReadUInt24LittleEndian(); - Assert.Equal((uint)0x020100, (uint)read); - } - - [Fact] - public void ReadInt32Test() - { - var stream = new MemoryStream(_bytes); - var br = new BinaryReader(stream); - int read = br.ReadInt32(); - Assert.Equal(0x03020100, read); - } - - [Fact] - public void ReadInt32BigEndianTest() - { - var stream = new MemoryStream(_bytes); - var br = new BinaryReader(stream); - int read = br.ReadInt32BigEndian(); - Assert.Equal(0x00010203, read); - } - - [Fact] - public void ReadInt32LittleEndianTest() - { - var stream = new MemoryStream(_bytes); - var br = new BinaryReader(stream); - int read = br.ReadInt32LittleEndian(); - Assert.Equal(0x03020100, read); - } - - [Fact] - public void ReadInt32BothEndianTest() - { - var stream = new MemoryStream(_bytes); - var br = new BinaryReader(stream); - BothInt32 read = br.ReadInt32BothEndian(); - Assert.Equal(0x03020100, read.LittleEndian); - Assert.Equal(0x04050607, read.BigEndian); - } - - [Fact] - public void ReadUInt32Test() - { - var stream = new MemoryStream(_bytes); - var br = new BinaryReader(stream); - uint read = br.ReadUInt32(); - Assert.Equal((uint)0x03020100, read); - } - - [Fact] - public void ReadUInt32BigEndianTest() - { - var stream = new MemoryStream(_bytes); - var br = new BinaryReader(stream); - uint read = br.ReadUInt32BigEndian(); - Assert.Equal((uint)0x00010203, read); - } - - [Fact] - public void ReadUInt32LittleEndianTest() - { - var stream = new MemoryStream(_bytes); - var br = new BinaryReader(stream); - uint read = br.ReadUInt32LittleEndian(); - Assert.Equal((uint)0x03020100, read); - } - - [Fact] - public void ReadUInt32BothEndianTest() - { - var stream = new MemoryStream(_bytes); - var br = new BinaryReader(stream); - BothUInt32 read = br.ReadUInt32BothEndian(); - Assert.Equal((uint)0x03020100, read.LittleEndian); - Assert.Equal((uint)0x04050607, read.BigEndian); - } - - [Fact] - public void ReadDWORDTest() - { - var stream = new MemoryStream(_bytes); - var br = new BinaryReader(stream); - uint read = br.ReadDWORD(); - Assert.Equal((uint)0x03020100, read); - } - - [Fact] - public void ReadDWORDBigEndianTest() - { - var stream = new MemoryStream(_bytes); - var br = new BinaryReader(stream); - uint read = br.ReadDWORDBigEndian(); - Assert.Equal((uint)0x00010203, read); - } - - [Fact] - public void ReadDWORDLittleEndianTest() - { - var stream = new MemoryStream(_bytes); - var br = new BinaryReader(stream); - uint read = br.ReadDWORDLittleEndian(); - Assert.Equal((uint)0x03020100, read); - } - - [Fact] - public void ReadDWORDBothEndianTest() - { - var stream = new MemoryStream(_bytes); - var br = new BinaryReader(stream); - BothUInt32 read = br.ReadDWORDBothEndian(); - Assert.Equal((uint)0x03020100, read.LittleEndian); - Assert.Equal((uint)0x04050607, read.BigEndian); - } - - [Fact] - public void ReadSingleTest() - { - var stream = new MemoryStream(_bytes); - var br = new BinaryReader(stream); - float expected = BitConverter.Int32BitsToSingle(0x03020100); - float read = br.ReadSingle(); - Assert.Equal(expected, read); - } - - [Fact] - public void ReadSingleBigEndianTest() - { - var stream = new MemoryStream(_bytes); - var br = new BinaryReader(stream); - float expected = BitConverter.Int32BitsToSingle(0x00010203); - float read = br.ReadSingleBigEndian(); - Assert.Equal(expected, read); - } - - [Fact] - public void ReadSingleLittleEndianTest() - { - var stream = new MemoryStream(_bytes); - var br = new BinaryReader(stream); - float expected = BitConverter.Int32BitsToSingle(0x03020100); - float read = br.ReadSingleLittleEndian(); - Assert.Equal(expected, read); - } - - [Fact] - public void ReadInt48Test() - { - var stream = new MemoryStream(_bytes); - var br = new BinaryReader(stream); - Int48 read = br.ReadInt48(); - Assert.Equal(0x050403020100, (long)read); - } - - [Fact] - public void ReadInt48BigEndianTest() - { - var stream = new MemoryStream(_bytes); - var br = new BinaryReader(stream); - Int48 read = br.ReadInt48BigEndian(); - Assert.Equal(0x000102030405, (long)read); - } - - [Fact] - public void ReadInt48LittleEndianTest() - { - var stream = new MemoryStream(_bytes); - var br = new BinaryReader(stream); - Int48 read = br.ReadInt48LittleEndian(); - Assert.Equal(0x050403020100, (long)read); - } - - [Fact] - public void ReadUInt48Test() - { - var stream = new MemoryStream(_bytes); - var br = new BinaryReader(stream); - UInt48 read = br.ReadUInt48(); - Assert.Equal((ulong)0x050403020100, (ulong)read); - } - - [Fact] - public void ReadUInt48BigEndianTest() - { - var stream = new MemoryStream(_bytes); - var br = new BinaryReader(stream); - UInt48 read = br.ReadUInt48BigEndian(); - Assert.Equal((ulong)0x000102030405, (ulong)read); - } - - [Fact] - public void ReadUInt48LittleEndianTest() - { - var stream = new MemoryStream(_bytes); - var br = new BinaryReader(stream); - UInt48 read = br.ReadUInt48LittleEndian(); - Assert.Equal((ulong)0x050403020100, (ulong)read); - } - - [Fact] - public void ReadInt64Test() - { - var stream = new MemoryStream(_bytes); - var br = new BinaryReader(stream); - long read = br.ReadInt64(); - Assert.Equal(0x0706050403020100, read); - } - - [Fact] - public void ReadInt64BigEndianTest() - { - var stream = new MemoryStream(_bytes); - var br = new BinaryReader(stream); - long read = br.ReadInt64BigEndian(); - Assert.Equal(0x0001020304050607, read); - } - - [Fact] - public void ReadInt64LittleEndianTest() - { - var stream = new MemoryStream(_bytes); - var br = new BinaryReader(stream); - long read = br.ReadInt64LittleEndian(); - Assert.Equal(0x0706050403020100, read); - } - - [Fact] - public void ReadInt64BothEndianTest() - { - var stream = new MemoryStream(_bytes); - var br = new BinaryReader(stream); - BothInt64 read = br.ReadInt64BothEndian(); - Assert.Equal(0x0706050403020100, read.LittleEndian); - Assert.Equal(0x08090A0B0C0D0E0F, read.BigEndian); - } - - [Fact] - public void ReadUInt64Test() - { - var stream = new MemoryStream(_bytes); - var br = new BinaryReader(stream); - ulong read = br.ReadUInt64(); - Assert.Equal((ulong)0x0706050403020100, read); - } - - [Fact] - public void ReadUInt64BigEndianTest() - { - var stream = new MemoryStream(_bytes); - var br = new BinaryReader(stream); - ulong read = br.ReadUInt64BigEndian(); - Assert.Equal((ulong)0x0001020304050607, read); - } - - [Fact] - public void ReadUInt64LittleEndianTest() - { - var stream = new MemoryStream(_bytes); - var br = new BinaryReader(stream); - ulong read = br.ReadUInt64LittleEndian(); - Assert.Equal((ulong)0x0706050403020100, read); - } - - [Fact] - public void ReadUInt64BothEndianTest() - { - var stream = new MemoryStream(_bytes); - var br = new BinaryReader(stream); - BothUInt64 read = br.ReadUInt64BothEndian(); - Assert.Equal((ulong)0x0706050403020100, read.LittleEndian); - Assert.Equal((ulong)0x08090A0B0C0D0E0F, read.BigEndian); - } - - [Fact] - public void ReadQWORDTest() - { - var stream = new MemoryStream(_bytes); - var br = new BinaryReader(stream); - ulong read = br.ReadQWORD(); - Assert.Equal((ulong)0x0706050403020100, read); - } - - [Fact] - public void ReadQWORDBigEndianTest() - { - var stream = new MemoryStream(_bytes); - var br = new BinaryReader(stream); - ulong read = br.ReadQWORDBigEndian(); - Assert.Equal((ulong)0x0001020304050607, read); - } - - [Fact] - public void ReadQWORDLittleEndianTest() - { - var stream = new MemoryStream(_bytes); - var br = new BinaryReader(stream); - ulong read = br.ReadQWORDLittleEndian(); - Assert.Equal((ulong)0x0706050403020100, read); - } - - [Fact] - public void ReadQWORDBothEndianTest() - { - var stream = new MemoryStream(_bytes); - var br = new BinaryReader(stream); - BothUInt64 read = br.ReadQWORDBothEndian(); - Assert.Equal((ulong)0x0706050403020100, read.LittleEndian); - Assert.Equal((ulong)0x08090A0B0C0D0E0F, read.BigEndian); - } - - [Fact] - public void ReadDoubleTest() - { - var stream = new MemoryStream(_bytes); - var br = new BinaryReader(stream); - double expected = BitConverter.Int64BitsToDouble(0x0706050403020100); - double read = br.ReadDouble(); - Assert.Equal(expected, read); - } - - [Fact] - public void ReadDoubleBigEndianTest() - { - var stream = new MemoryStream(_bytes); - var br = new BinaryReader(stream); - double expected = BitConverter.Int64BitsToDouble(0x0001020304050607); - double read = br.ReadDoubleBigEndian(); - Assert.Equal(expected, read); - } - - [Fact] - public void ReadDoubleLittleEndianTest() - { - var stream = new MemoryStream(_bytes); - var br = new BinaryReader(stream); - double expected = BitConverter.Int64BitsToDouble(0x0706050403020100); - double read = br.ReadDoubleLittleEndian(); - Assert.Equal(expected, read); - } - - [Fact] - public void ReadGuidTest() - { - var stream = new MemoryStream(_bytes); - var br = new BinaryReader(stream); - var expected = new Guid(_bytes); - Guid read = br.ReadGuid(); - Assert.Equal(expected, read); - } - - [Fact] - public void ReadGuidBigEndianTest() - { - var stream = new MemoryStream(_bytes); - var br = new BinaryReader(stream); - var expected = new Guid(_guidBigEndianbytes); - Guid read = br.ReadGuidBigEndian(); - Assert.Equal(expected, read); - } - - [Fact] - public void ReadGuidLittleEndianTest() - { - var stream = new MemoryStream(_bytes); - var br = new BinaryReader(stream); - var expected = new Guid(_bytes); - Guid read = br.ReadGuidLittleEndian(); - Assert.Equal(expected, read); - } - - [Fact] - public void ReadInt128Test() - { - var stream = new MemoryStream(_bytes); - var br = new BinaryReader(stream); - var expected = (Int128)new BigInteger(_bytes); - Int128 read = br.ReadInt128(); - Assert.Equal(expected, read); - } - - [Fact] - public void ReadInt128BigEndianTest() - { - var stream = new MemoryStream(_bytes); - var br = new BinaryReader(stream); - var reversed = Enumerable.Reverse(_bytes).ToArray(); - var expected = (Int128)new BigInteger(reversed); - Int128 read = br.ReadInt128BigEndian(); - Assert.Equal(expected, read); - } - - [Fact] - public void ReadInt128LittleEndianTest() - { - var stream = new MemoryStream(_bytes); - var br = new BinaryReader(stream); - var expected = (Int128)new BigInteger(_bytes); - Int128 read = br.ReadInt128LittleEndian(); - Assert.Equal(expected, read); - } - - [Fact] - public void ReadUInt128Test() - { - var stream = new MemoryStream(_bytes); - var br = new BinaryReader(stream); - var expected = (UInt128)new BigInteger(_bytes); - UInt128 read = br.ReadUInt128(); - Assert.Equal(expected, read); - } - - [Fact] - public void ReadUInt128BigEndianTest() - { - var stream = new MemoryStream(_bytes); - var br = new BinaryReader(stream); - var reversed = Enumerable.Reverse(_bytes).ToArray(); - var expected = (UInt128)new BigInteger(reversed); - UInt128 read = br.ReadUInt128BigEndian(); - Assert.Equal(expected, read); - } - - [Fact] - public void ReadUInt128LittleEndianTest() - { - var stream = new MemoryStream(_bytes); - var br = new BinaryReader(stream); - var expected = (UInt128)new BigInteger(_bytes); - UInt128 read = br.ReadUInt128LittleEndian(); - Assert.Equal(expected, read); - } - - [Fact] - public void ReadDecimalTest() - { - var stream = new MemoryStream(_decimalBytes); - var br = new BinaryReader(stream); - decimal expected = 0.0123456789M; - decimal read = br.ReadDecimal(); - Assert.Equal(expected, read); - } - - [Fact] - public void ReadDecimalBigEndianTest() - { - var stream = new MemoryStream([.. Enumerable.Reverse(_decimalBytes)]); - var br = new BinaryReader(stream); - decimal expected = 0.0123456789M; - decimal read = br.ReadDecimalBigEndian(); - Assert.Equal(expected, read); - } - - [Fact] - public void ReadDecimalLittleEndianTest() - { - var stream = new MemoryStream(_decimalBytes); - var br = new BinaryReader(stream); - decimal expected = 0.0123456789M; - decimal read = br.ReadDecimalLittleEndian(); - Assert.Equal(expected, read); - } - [Fact] public void ReadNullTerminatedStringTest() { @@ -1078,1562 +313,5 @@ namespace SabreTools.IO.Extensions.Test Assert.Equal(expected2.FieldA, read2.FieldA); Assert.Equal(expected2.FieldB, read2.FieldB); } - - #endregion - - #region Peek Read - - [Fact] - public void PeekByteTest() - { - var stream = new MemoryStream(_bytes); - var br = new BinaryReader(stream); - byte read = br.PeekByte(); - Assert.Equal(0x00, read); - Assert.Equal(0, br.BaseStream.Position); - } - - [Fact] - public void PeekByteBothEndianTest() - { - var stream = new MemoryStream(_bytes); - var br = new BinaryReader(stream); - BothUInt8 read = br.PeekByteBothEndian(); - Assert.Equal(0x00, read.LittleEndian); - Assert.Equal(0x01, read.BigEndian); - Assert.Equal(0, br.BaseStream.Position); - } - - [Fact] - public void PeekBytesTest() - { - var stream = new MemoryStream(_bytes); - var br = new BinaryReader(stream); - int length = 4; - byte[] read = br.PeekBytes(length); - Assert.Equal(length, read.Length); - Assert.True(read.SequenceEqual(_bytes.Take(length))); - Assert.Equal(0, br.BaseStream.Position); - } - - [Fact] - public void PeekSByteTest() - { - var stream = new MemoryStream(_bytes); - var br = new BinaryReader(stream); - sbyte read = br.PeekSByte(); - Assert.Equal(0x00, read); - } - - [Fact] - public void PeekSByteBothEndianTest() - { - var stream = new MemoryStream(_bytes); - var br = new BinaryReader(stream); - BothInt8 read = br.PeekSByteBothEndian(); - Assert.Equal(0x00, read.LittleEndian); - Assert.Equal(0x01, read.BigEndian); - Assert.Equal(0, br.BaseStream.Position); - } - - [Fact] - public void PeekInt16Test() - { - var stream = new MemoryStream(_bytes); - var br = new BinaryReader(stream); - short read = br.PeekInt16(); - Assert.Equal(0x0100, read); - Assert.Equal(0, br.BaseStream.Position); - } - - [Fact] - public void PeekInt16BigEndianTest() - { - var stream = new MemoryStream(_bytes); - var br = new BinaryReader(stream); - short read = br.PeekInt16BigEndian(); - Assert.Equal(0x0001, read); - Assert.Equal(0, br.BaseStream.Position); - } - - [Fact] - public void PeekInt16LittleEndianTest() - { - var stream = new MemoryStream(_bytes); - var br = new BinaryReader(stream); - short read = br.PeekInt16LittleEndian(); - Assert.Equal(0x0100, read); - Assert.Equal(0, br.BaseStream.Position); - } - - [Fact] - public void PeekInt16BothEndianTest() - { - var stream = new MemoryStream(_bytes); - var br = new BinaryReader(stream); - BothInt16 read = br.PeekInt16BothEndian(); - Assert.Equal(0x0100, read.LittleEndian); - Assert.Equal(0x0203, read.BigEndian); - Assert.Equal(0, br.BaseStream.Position); - } - - [Fact] - public void PeekUInt16Test() - { - var stream = new MemoryStream(_bytes); - var br = new BinaryReader(stream); - ushort read = br.PeekUInt16(); - Assert.Equal(0x0100, read); - Assert.Equal(0, br.BaseStream.Position); - } - - [Fact] - public void PeekUInt16BigEndianTest() - { - var stream = new MemoryStream(_bytes); - var br = new BinaryReader(stream); - ushort read = br.PeekUInt16BigEndian(); - Assert.Equal(0x0001, read); - Assert.Equal(0, br.BaseStream.Position); - } - - [Fact] - public void PeekUInt16LittleEndianTest() - { - var stream = new MemoryStream(_bytes); - var br = new BinaryReader(stream); - ushort read = br.PeekUInt16LittleEndian(); - Assert.Equal(0x0100, read); - Assert.Equal(0, br.BaseStream.Position); - } - - [Fact] - public void PeekUInt16BothEndianTest() - { - var stream = new MemoryStream(_bytes); - var br = new BinaryReader(stream); - BothUInt16 read = br.PeekUInt16BothEndian(); - Assert.Equal(0x0100, read.LittleEndian); - Assert.Equal(0x0203, read.BigEndian); - Assert.Equal(0, br.BaseStream.Position); - } - - [Fact] - public void PeekWORDTest() - { - var stream = new MemoryStream(_bytes); - var br = new BinaryReader(stream); - ushort read = br.PeekWORD(); - Assert.Equal(0x0100, read); - Assert.Equal(0, br.BaseStream.Position); - } - - [Fact] - public void PeekWORDBigEndianTest() - { - var stream = new MemoryStream(_bytes); - var br = new BinaryReader(stream); - ushort read = br.PeekWORDBigEndian(); - Assert.Equal(0x0001, read); - Assert.Equal(0, br.BaseStream.Position); - } - - [Fact] - public void PeekWORDLittleEndianTest() - { - var stream = new MemoryStream(_bytes); - var br = new BinaryReader(stream); - ushort read = br.PeekWORDLittleEndian(); - Assert.Equal(0x0100, read); - Assert.Equal(0, br.BaseStream.Position); - } - - [Fact] - public void PeekWORDBothEndianTest() - { - var stream = new MemoryStream(_bytes); - var br = new BinaryReader(stream); - BothUInt16 read = br.PeekWORDBothEndian(); - Assert.Equal(0x0100, read.LittleEndian); - Assert.Equal(0x0203, read.BigEndian); - Assert.Equal(0, br.BaseStream.Position); - } - - [Fact] - public void PeekHalfTest() - { - var stream = new MemoryStream(_bytes); - var br = new BinaryReader(stream); - Half expected = BitConverter.Int16BitsToHalf(0x0100); - Half read = br.PeekHalf(); - Assert.Equal(expected, read); - Assert.Equal(0, br.BaseStream.Position); - } - - [Fact] - public void PeekHalfBigEndianTest() - { - var stream = new MemoryStream(_bytes); - var br = new BinaryReader(stream); - Half expected = BitConverter.Int16BitsToHalf(0x0001); - Half read = br.PeekHalfBigEndian(); - Assert.Equal(expected, read); - Assert.Equal(0, br.BaseStream.Position); - } - - [Fact] - public void PeekHalfLittleEndianTest() - { - var stream = new MemoryStream(_bytes); - var br = new BinaryReader(stream); - Half expected = BitConverter.Int16BitsToHalf(0x0100); - Half read = br.PeekHalfLittleEndian(); - Assert.Equal(expected, read); - Assert.Equal(0, br.BaseStream.Position); - } - - [Fact] - public void PeekInt24Test() - { - var stream = new MemoryStream(_bytes); - var br = new BinaryReader(stream); - Int24 read = br.PeekInt24(); - Assert.Equal(0x020100, (int)read); - Assert.Equal(0, br.BaseStream.Position); - } - - [Fact] - public void PeekInt24BigEndianTest() - { - var stream = new MemoryStream(_bytes); - var br = new BinaryReader(stream); - Int24 read = br.PeekInt24BigEndian(); - Assert.Equal(0x000102, (int)read); - Assert.Equal(0, br.BaseStream.Position); - } - - [Fact] - public void PeekInt24LittleEndianTest() - { - var stream = new MemoryStream(_bytes); - var br = new BinaryReader(stream); - Int24 read = br.PeekInt24LittleEndian(); - Assert.Equal(0x020100, (int)read); - Assert.Equal(0, br.BaseStream.Position); - } - - [Fact] - public void PeekUInt24Test() - { - var stream = new MemoryStream(_bytes); - var br = new BinaryReader(stream); - UInt24 read = br.PeekUInt24(); - Assert.Equal((uint)0x020100, (uint)read); - Assert.Equal(0, br.BaseStream.Position); - } - - [Fact] - public void PeekUInt24BigEndianTest() - { - var stream = new MemoryStream(_bytes); - var br = new BinaryReader(stream); - UInt24 read = br.PeekUInt24BigEndian(); - Assert.Equal((uint)0x000102, (uint)read); - Assert.Equal(0, br.BaseStream.Position); - } - - [Fact] - public void PeekUInt24LittleEndianTest() - { - var stream = new MemoryStream(_bytes); - var br = new BinaryReader(stream); - UInt24 read = br.PeekUInt24LittleEndian(); - Assert.Equal((uint)0x020100, (uint)read); - Assert.Equal(0, br.BaseStream.Position); - } - - [Fact] - public void PeekInt32Test() - { - var stream = new MemoryStream(_bytes); - var br = new BinaryReader(stream); - int read = br.PeekInt32(); - Assert.Equal(0x03020100, read); - Assert.Equal(0, br.BaseStream.Position); - } - - [Fact] - public void PeekInt32BigEndianTest() - { - var stream = new MemoryStream(_bytes); - var br = new BinaryReader(stream); - int read = br.PeekInt32BigEndian(); - Assert.Equal(0x00010203, read); - Assert.Equal(0, br.BaseStream.Position); - } - - [Fact] - public void PeekInt32LittleEndianTest() - { - var stream = new MemoryStream(_bytes); - var br = new BinaryReader(stream); - int read = br.PeekInt32LittleEndian(); - Assert.Equal(0x03020100, read); - Assert.Equal(0, br.BaseStream.Position); - } - - [Fact] - public void PeekInt32BothEndianTest() - { - var stream = new MemoryStream(_bytes); - var br = new BinaryReader(stream); - BothInt32 read = br.PeekInt32BothEndian(); - Assert.Equal(0x03020100, read.LittleEndian); - Assert.Equal(0x04050607, read.BigEndian); - Assert.Equal(0, br.BaseStream.Position); - } - - [Fact] - public void PeekUInt32Test() - { - var stream = new MemoryStream(_bytes); - var br = new BinaryReader(stream); - uint read = br.PeekUInt32(); - Assert.Equal((uint)0x03020100, read); - Assert.Equal(0, br.BaseStream.Position); - } - - [Fact] - public void PeekUInt32BigEndianTest() - { - var stream = new MemoryStream(_bytes); - var br = new BinaryReader(stream); - uint read = br.PeekUInt32BigEndian(); - Assert.Equal((uint)0x00010203, read); - Assert.Equal(0, br.BaseStream.Position); - } - - [Fact] - public void PeekUInt32LittleEndianTest() - { - var stream = new MemoryStream(_bytes); - var br = new BinaryReader(stream); - uint read = br.PeekUInt32LittleEndian(); - Assert.Equal((uint)0x03020100, read); - Assert.Equal(0, br.BaseStream.Position); - } - - [Fact] - public void PeekUInt32BothEndianTest() - { - var stream = new MemoryStream(_bytes); - var br = new BinaryReader(stream); - BothUInt32 read = br.PeekUInt32BothEndian(); - Assert.Equal((uint)0x03020100, read.LittleEndian); - Assert.Equal((uint)0x04050607, read.BigEndian); - Assert.Equal(0, br.BaseStream.Position); - } - - [Fact] - public void PeekDWORDTest() - { - var stream = new MemoryStream(_bytes); - var br = new BinaryReader(stream); - uint read = br.PeekDWORD(); - Assert.Equal((uint)0x03020100, read); - Assert.Equal(0, br.BaseStream.Position); - } - - [Fact] - public void PeekDWORDBigEndianTest() - { - var stream = new MemoryStream(_bytes); - var br = new BinaryReader(stream); - uint read = br.PeekDWORDBigEndian(); - Assert.Equal((uint)0x00010203, read); - Assert.Equal(0, br.BaseStream.Position); - } - - [Fact] - public void PeekDWORDLittleEndianTest() - { - var stream = new MemoryStream(_bytes); - var br = new BinaryReader(stream); - uint read = br.PeekDWORDLittleEndian(); - Assert.Equal((uint)0x03020100, read); - Assert.Equal(0, br.BaseStream.Position); - } - - [Fact] - public void PeekDWORDBothEndianTest() - { - var stream = new MemoryStream(_bytes); - var br = new BinaryReader(stream); - BothUInt32 read = br.PeekDWORDBothEndian(); - Assert.Equal((uint)0x03020100, read.LittleEndian); - Assert.Equal((uint)0x04050607, read.BigEndian); - Assert.Equal(0, br.BaseStream.Position); - } - - [Fact] - public void PeekSingleTest() - { - var stream = new MemoryStream(_bytes); - var br = new BinaryReader(stream); - float expected = BitConverter.Int32BitsToSingle(0x03020100); - float read = br.PeekSingle(); - Assert.Equal(expected, read); - Assert.Equal(0, br.BaseStream.Position); - } - - [Fact] - public void PeekSingleBigEndianTest() - { - var stream = new MemoryStream(_bytes); - var br = new BinaryReader(stream); - float expected = BitConverter.Int32BitsToSingle(0x00010203); - float read = br.PeekSingleBigEndian(); - Assert.Equal(expected, read); - Assert.Equal(0, br.BaseStream.Position); - } - - [Fact] - public void PeekSingleLittleEndianTest() - { - var stream = new MemoryStream(_bytes); - var br = new BinaryReader(stream); - float expected = BitConverter.Int32BitsToSingle(0x03020100); - float read = br.PeekSingleLittleEndian(); - Assert.Equal(expected, read); - Assert.Equal(0, br.BaseStream.Position); - } - - [Fact] - public void PeekInt48Test() - { - var stream = new MemoryStream(_bytes); - var br = new BinaryReader(stream); - Int48 read = br.PeekInt48(); - Assert.Equal(0x050403020100, (long)read); - Assert.Equal(0, br.BaseStream.Position); - } - - [Fact] - public void PeekInt48BigEndianTest() - { - var stream = new MemoryStream(_bytes); - var br = new BinaryReader(stream); - Int48 read = br.PeekInt48BigEndian(); - Assert.Equal(0x000102030405, (long)read); - Assert.Equal(0, br.BaseStream.Position); - } - - [Fact] - public void PeekInt48LittleEndianTest() - { - var stream = new MemoryStream(_bytes); - var br = new BinaryReader(stream); - Int48 read = br.PeekInt48LittleEndian(); - Assert.Equal(0x050403020100, (long)read); - Assert.Equal(0, br.BaseStream.Position); - } - - [Fact] - public void PeekUInt48Test() - { - var stream = new MemoryStream(_bytes); - var br = new BinaryReader(stream); - UInt48 read = br.PeekUInt48(); - Assert.Equal((ulong)0x050403020100, (ulong)read); - Assert.Equal(0, br.BaseStream.Position); - } - - [Fact] - public void PeekUInt48BigEndianTest() - { - var stream = new MemoryStream(_bytes); - var br = new BinaryReader(stream); - UInt48 read = br.PeekUInt48BigEndian(); - Assert.Equal((ulong)0x000102030405, (ulong)read); - Assert.Equal(0, br.BaseStream.Position); - } - - [Fact] - public void PeekUInt48LittleEndianTest() - { - var stream = new MemoryStream(_bytes); - var br = new BinaryReader(stream); - UInt48 read = br.PeekUInt48LittleEndian(); - Assert.Equal((ulong)0x050403020100, (ulong)read); - Assert.Equal(0, br.BaseStream.Position); - } - - [Fact] - public void PeekInt64Test() - { - var stream = new MemoryStream(_bytes); - var br = new BinaryReader(stream); - long read = br.PeekInt64(); - Assert.Equal(0x0706050403020100, read); - Assert.Equal(0, br.BaseStream.Position); - } - - [Fact] - public void PeekInt64BigEndianTest() - { - var stream = new MemoryStream(_bytes); - var br = new BinaryReader(stream); - long read = br.PeekInt64BigEndian(); - Assert.Equal(0x0001020304050607, read); - Assert.Equal(0, br.BaseStream.Position); - } - - [Fact] - public void PeekInt64LittleEndianTest() - { - var stream = new MemoryStream(_bytes); - var br = new BinaryReader(stream); - long read = br.PeekInt64LittleEndian(); - Assert.Equal(0x0706050403020100, read); - Assert.Equal(0, br.BaseStream.Position); - } - - [Fact] - public void PeekInt64BothEndianTest() - { - var stream = new MemoryStream(_bytes); - var br = new BinaryReader(stream); - BothInt64 read = br.PeekInt64BothEndian(); - Assert.Equal(0x0706050403020100, read.LittleEndian); - Assert.Equal(0x08090A0B0C0D0E0F, read.BigEndian); - Assert.Equal(0, br.BaseStream.Position); - } - - [Fact] - public void PeekUInt64Test() - { - var stream = new MemoryStream(_bytes); - var br = new BinaryReader(stream); - ulong read = br.PeekUInt64(); - Assert.Equal((ulong)0x0706050403020100, read); - Assert.Equal(0, br.BaseStream.Position); - } - - [Fact] - public void PeekUInt64BigEndianTest() - { - var stream = new MemoryStream(_bytes); - var br = new BinaryReader(stream); - ulong read = br.PeekUInt64BigEndian(); - Assert.Equal((ulong)0x0001020304050607, read); - Assert.Equal(0, br.BaseStream.Position); - } - - [Fact] - public void PeekUInt64LittleEndianTest() - { - var stream = new MemoryStream(_bytes); - var br = new BinaryReader(stream); - ulong read = br.PeekUInt64LittleEndian(); - Assert.Equal((ulong)0x0706050403020100, read); - Assert.Equal(0, br.BaseStream.Position); - } - - [Fact] - public void PeekUInt64BothEndianTest() - { - var stream = new MemoryStream(_bytes); - var br = new BinaryReader(stream); - BothUInt64 read = br.PeekUInt64BothEndian(); - Assert.Equal((ulong)0x0706050403020100, read.LittleEndian); - Assert.Equal((ulong)0x08090A0B0C0D0E0F, read.BigEndian); - Assert.Equal(0, br.BaseStream.Position); - } - - [Fact] - public void PeekQWORDTest() - { - var stream = new MemoryStream(_bytes); - var br = new BinaryReader(stream); - ulong read = br.PeekQWORD(); - Assert.Equal((ulong)0x0706050403020100, read); - Assert.Equal(0, br.BaseStream.Position); - } - - [Fact] - public void PeekQWORDBigEndianTest() - { - var stream = new MemoryStream(_bytes); - var br = new BinaryReader(stream); - ulong read = br.PeekQWORDBigEndian(); - Assert.Equal((ulong)0x0001020304050607, read); - Assert.Equal(0, br.BaseStream.Position); - } - - [Fact] - public void PeekQWORDLittleEndianTest() - { - var stream = new MemoryStream(_bytes); - var br = new BinaryReader(stream); - ulong read = br.PeekQWORDLittleEndian(); - Assert.Equal((ulong)0x0706050403020100, read); - Assert.Equal(0, br.BaseStream.Position); - } - - [Fact] - public void PeekQWORDBothEndianTest() - { - var stream = new MemoryStream(_bytes); - var br = new BinaryReader(stream); - BothUInt64 read = br.PeekQWORDBothEndian(); - Assert.Equal((ulong)0x0706050403020100, read.LittleEndian); - Assert.Equal((ulong)0x08090A0B0C0D0E0F, read.BigEndian); - Assert.Equal(0, br.BaseStream.Position); - } - - [Fact] - public void PeekDoubleTest() - { - var stream = new MemoryStream(_bytes); - var br = new BinaryReader(stream); - double expected = BitConverter.Int64BitsToDouble(0x0706050403020100); - double read = br.PeekDouble(); - Assert.Equal(expected, read); - Assert.Equal(0, br.BaseStream.Position); - } - - [Fact] - public void PeekDoubleBigEndianTest() - { - var stream = new MemoryStream(_bytes); - var br = new BinaryReader(stream); - double expected = BitConverter.Int64BitsToDouble(0x0001020304050607); - double read = br.PeekDoubleBigEndian(); - Assert.Equal(expected, read); - Assert.Equal(0, br.BaseStream.Position); - } - - [Fact] - public void PeekDoubleLittleEndianTest() - { - var stream = new MemoryStream(_bytes); - var br = new BinaryReader(stream); - double expected = BitConverter.Int64BitsToDouble(0x0706050403020100); - double read = br.PeekDoubleLittleEndian(); - Assert.Equal(expected, read); - Assert.Equal(0, br.BaseStream.Position); - } - - [Fact] - public void PeekGuidTest() - { - var stream = new MemoryStream(_bytes); - var br = new BinaryReader(stream); - var expected = new Guid(_bytes); - Guid read = br.PeekGuid(); - Assert.Equal(expected, read); - Assert.Equal(0, br.BaseStream.Position); - } - - [Fact] - public void PeekGuidBigEndianTest() - { - var stream = new MemoryStream(_bytes); - var br = new BinaryReader(stream); - var expected = new Guid(_guidBigEndianbytes); - Guid read = br.PeekGuidBigEndian(); - Assert.Equal(expected, read); - Assert.Equal(0, br.BaseStream.Position); - } - - [Fact] - public void PeekGuidLittleEndianTest() - { - var stream = new MemoryStream(_bytes); - var br = new BinaryReader(stream); - var expected = new Guid(_bytes); - Guid read = br.PeekGuidLittleEndian(); - Assert.Equal(expected, read); - Assert.Equal(0, br.BaseStream.Position); - } - - [Fact] - public void PeekInt128Test() - { - var stream = new MemoryStream(_bytes); - var br = new BinaryReader(stream); - var expected = (Int128)new BigInteger(_bytes); - Int128 read = br.PeekInt128(); - Assert.Equal(expected, read); - Assert.Equal(0, br.BaseStream.Position); - } - - [Fact] - public void PeekInt128BigEndianTest() - { - var stream = new MemoryStream(_bytes); - var br = new BinaryReader(stream); - var reversed = Enumerable.Reverse(_bytes).ToArray(); - var expected = (Int128)new BigInteger(reversed); - Int128 read = br.PeekInt128BigEndian(); - Assert.Equal(expected, read); - Assert.Equal(0, br.BaseStream.Position); - } - - [Fact] - public void PeekInt128LittleEndianTest() - { - var stream = new MemoryStream(_bytes); - var br = new BinaryReader(stream); - var expected = (Int128)new BigInteger(_bytes); - Int128 read = br.PeekInt128LittleEndian(); - Assert.Equal(expected, read); - Assert.Equal(0, br.BaseStream.Position); - } - - [Fact] - public void PeekUInt128Test() - { - var stream = new MemoryStream(_bytes); - var br = new BinaryReader(stream); - var expected = (UInt128)new BigInteger(_bytes); - UInt128 read = br.PeekUInt128(); - Assert.Equal(expected, read); - Assert.Equal(0, br.BaseStream.Position); - } - - [Fact] - public void PeekUInt128BigEndianTest() - { - var stream = new MemoryStream(_bytes); - var br = new BinaryReader(stream); - var reversed = Enumerable.Reverse(_bytes).ToArray(); - var expected = (UInt128)new BigInteger(reversed); - UInt128 read = br.PeekUInt128BigEndian(); - Assert.Equal(expected, read); - Assert.Equal(0, br.BaseStream.Position); - } - - [Fact] - public void PeekUInt128LittleEndianTest() - { - var stream = new MemoryStream(_bytes); - var br = new BinaryReader(stream); - var expected = (UInt128)new BigInteger(_bytes); - UInt128 read = br.PeekUInt128LittleEndian(); - Assert.Equal(expected, read); - Assert.Equal(0, br.BaseStream.Position); - } - - [Fact] - public void PeekDecimalTest() - { - var stream = new MemoryStream(_decimalBytes); - var br = new BinaryReader(stream); - decimal expected = 0.0123456789M; - decimal read = br.PeekDecimal(); - Assert.Equal(expected, read); - Assert.Equal(0, br.BaseStream.Position); - } - - [Fact] - public void PeekDecimalBigEndianTest() - { - var stream = new MemoryStream([.. Enumerable.Reverse(_decimalBytes)]); - var br = new BinaryReader(stream); - decimal expected = 0.0123456789M; - decimal read = br.PeekDecimalBigEndian(); - Assert.Equal(expected, read); - Assert.Equal(0, br.BaseStream.Position); - } - - [Fact] - public void PeekDecimalLittleEndianTest() - { - var stream = new MemoryStream(_decimalBytes); - var br = new BinaryReader(stream); - decimal expected = 0.0123456789M; - decimal read = br.PeekDecimalLittleEndian(); - Assert.Equal(expected, read); - Assert.Equal(0, br.BaseStream.Position); - } - - #endregion - - #region Try Read - - [Fact] - public void TryReadByteArrayTest() - { - var stream = new MemoryStream([]); - var br = new BinaryReader(stream); - bool actual = br.TryReadBytes(4, out byte[] read); - Assert.False(actual); - Assert.Empty(read); - } - - [Fact] - public void TryReadByteTest() - { - var stream = new MemoryStream([]); - var br = new BinaryReader(stream); - bool actual = br.TryReadByteValue(out byte read); - Assert.False(actual); - Assert.Equal(default, read); - } - - [Fact] - public void TryReadByteBothEndianTest() - { - var stream = new MemoryStream([]); - var br = new BinaryReader(stream); - bool actual = br.TryReadByteBothEndian(out BothUInt8 read); - Assert.False(actual); - Assert.Equal(default, read.LittleEndian); - Assert.Equal(default, read.BigEndian); - } - - [Fact] - public void TryReadBytesTest() - { - var stream = new MemoryStream([]); - var br = new BinaryReader(stream); - int length = 4; - bool actual = br.TryReadBytes(length, out byte[] read); - Assert.False(actual); - Assert.Empty(read); - } - - [Fact] - public void TryReadSByteTest() - { - var stream = new MemoryStream([]); - var br = new BinaryReader(stream); - bool actual = br.TryReadSByte(out sbyte read); - Assert.False(actual); - Assert.Equal(default, read); - } - - [Fact] - public void TryReadSByteBothEndianTest() - { - var stream = new MemoryStream([]); - var br = new BinaryReader(stream); - bool actual = br.TryReadSByteBothEndian(out BothInt8 read); - Assert.False(actual); - Assert.Equal(default, read.LittleEndian); - Assert.Equal(default, read.BigEndian); - } - - [Fact] - public void TryReadCharTest() - { - var stream = new MemoryStream([]); - var br = new BinaryReader(stream); - bool actual = br.TryReadChar(out char read); - Assert.False(actual); - Assert.Equal(default, read); - } - - [Fact] - public void TryReadInt16Test() - { - var stream = new MemoryStream([]); - var br = new BinaryReader(stream); - bool actual = br.TryReadInt16(out short read); - Assert.False(actual); - Assert.Equal(default, read); - } - - [Fact] - public void TryReadInt16BigEndianTest() - { - var stream = new MemoryStream([]); - var br = new BinaryReader(stream); - bool actual = br.TryReadInt16BigEndian(out short read); - Assert.False(actual); - Assert.Equal(default, read); - } - - [Fact] - public void TryReadInt16LittleEndianTest() - { - var stream = new MemoryStream([]); - var br = new BinaryReader(stream); - bool actual = br.TryReadInt16LittleEndian(out short read); - Assert.False(actual); - Assert.Equal(default, read); - } - - [Fact] - public void TryReadInt16BothEndianTest() - { - var stream = new MemoryStream([]); - var br = new BinaryReader(stream); - bool actual = br.TryReadInt16BothEndian(out BothInt16 read); - Assert.False(actual); - Assert.Equal(default, read.LittleEndian); - Assert.Equal(default, read.BigEndian); - } - - [Fact] - public void TryReadUInt16Test() - { - var stream = new MemoryStream([]); - var br = new BinaryReader(stream); - bool actual = br.TryReadUInt16(out ushort read); - Assert.False(actual); - Assert.Equal(default, read); - } - - [Fact] - public void TryReadUInt16BigEndianTest() - { - var stream = new MemoryStream([]); - var br = new BinaryReader(stream); - bool actual = br.TryReadUInt16BigEndian(out ushort read); - Assert.False(actual); - Assert.Equal(default, read); - } - - [Fact] - public void TryReadUInt16LittleEndianTest() - { - var stream = new MemoryStream([]); - var br = new BinaryReader(stream); - bool actual = br.TryReadUInt16LittleEndian(out ushort read); - Assert.False(actual); - Assert.Equal(default, read); - } - - [Fact] - public void TryReadUInt16BothEndianTest() - { - var stream = new MemoryStream([]); - var br = new BinaryReader(stream); - bool actual = br.TryReadUInt16BothEndian(out BothUInt16 read); - Assert.False(actual); - Assert.Equal(default, read.LittleEndian); - Assert.Equal(default, read.BigEndian); - } - - [Fact] - public void TryReadWORDTest() - { - var stream = new MemoryStream([]); - var br = new BinaryReader(stream); - bool actual = br.TryReadWORD(out ushort read); - Assert.False(actual); - Assert.Equal(default, read); - } - - [Fact] - public void TryReadWORDBigEndianTest() - { - var stream = new MemoryStream([]); - var br = new BinaryReader(stream); - bool actual = br.TryReadWORDBigEndian(out ushort read); - Assert.False(actual); - Assert.Equal(default, read); - } - - [Fact] - public void TryReadWORDLittleEndianTest() - { - var stream = new MemoryStream([]); - var br = new BinaryReader(stream); - bool actual = br.TryReadWORDLittleEndian(out ushort read); - Assert.False(actual); - Assert.Equal(default, read); - } - - [Fact] - public void TryReadWORDBothEndianTest() - { - var stream = new MemoryStream([]); - var br = new BinaryReader(stream); - bool actual = br.TryReadWORDBothEndian(out BothUInt16 read); - Assert.False(actual); - Assert.Equal(default, read.LittleEndian); - Assert.Equal(default, read.BigEndian); - } - - [Fact] - public void TryReadHalfTest() - { - var stream = new MemoryStream([]); - var br = new BinaryReader(stream); - bool actual = br.TryReadHalf(out Half read); - Assert.False(actual); - Assert.Equal(default, read); - } - - [Fact] - public void TryReadHalfBigEndianTest() - { - var stream = new MemoryStream([]); - var br = new BinaryReader(stream); - bool actual = br.TryReadHalfBigEndian(out Half read); - Assert.False(actual); - Assert.Equal(default, read); - } - - [Fact] - public void TryReadHalfLittleEndianTest() - { - var stream = new MemoryStream([]); - var br = new BinaryReader(stream); - bool actual = br.TryReadHalfLittleEndian(out Half read); - Assert.False(actual); - Assert.Equal(default, read); - } - - [Fact] - public void TryReadInt24Test() - { - var stream = new MemoryStream([]); - var br = new BinaryReader(stream); - bool actual = br.TryReadInt24(out Int24 read); - Assert.False(actual); - Assert.Equal(default, (int)read); - } - - [Fact] - public void TryReadInt24BigEndianTest() - { - var stream = new MemoryStream([]); - var br = new BinaryReader(stream); - bool actual = br.TryReadInt24BigEndian(out Int24 read); - Assert.False(actual); - Assert.Equal(default, (int)read); - } - - [Fact] - public void TryReadInt24LittleEndianTest() - { - var stream = new MemoryStream([]); - var br = new BinaryReader(stream); - bool actual = br.TryReadInt24LittleEndian(out Int24 read); - Assert.False(actual); - Assert.Equal(default, (int)read); - } - - [Fact] - public void TryReadUInt24Test() - { - var stream = new MemoryStream([]); - var br = new BinaryReader(stream); - bool actual = br.TryReadUInt24(out UInt24 read); - Assert.False(actual); - Assert.Equal(default, (uint)read); - } - - [Fact] - public void TryReadUInt24BigEndianTest() - { - var stream = new MemoryStream([]); - var br = new BinaryReader(stream); - bool actual = br.TryReadUInt24BigEndian(out UInt24 read); - Assert.False(actual); - Assert.Equal(default, (uint)read); - } - - [Fact] - public void TryReadUInt24LittleEndianTest() - { - var stream = new MemoryStream([]); - var br = new BinaryReader(stream); - bool actual = br.TryReadUInt24LittleEndian(out UInt24 read); - Assert.False(actual); - Assert.Equal(default, (uint)read); - } - - [Fact] - public void TryReadInt32Test() - { - var stream = new MemoryStream([]); - var br = new BinaryReader(stream); - bool actual = br.TryReadInt32(out int read); - Assert.False(actual); - Assert.Equal(default, read); - } - - [Fact] - public void TryReadInt32BigEndianTest() - { - var stream = new MemoryStream([]); - var br = new BinaryReader(stream); - bool actual = br.TryReadInt32BigEndian(out int read); - Assert.False(actual); - Assert.Equal(default, read); - } - - [Fact] - public void TryReadInt32LittleEndianTest() - { - var stream = new MemoryStream([]); - var br = new BinaryReader(stream); - bool actual = br.TryReadInt32LittleEndian(out int read); - Assert.False(actual); - Assert.Equal(default, read); - } - - [Fact] - public void TryReadInt32BothEndianTest() - { - var stream = new MemoryStream([]); - var br = new BinaryReader(stream); - bool actual = br.TryReadInt32BothEndian(out BothInt32 read); - Assert.False(actual); - Assert.Equal(default, read.LittleEndian); - Assert.Equal(default, read.BigEndian); - } - - [Fact] - public void TryReadUInt32Test() - { - var stream = new MemoryStream([]); - var br = new BinaryReader(stream); - bool actual = br.TryReadUInt32(out uint read); - Assert.False(actual); - Assert.Equal(default, read); - } - - [Fact] - public void TryReadUInt32BigEndianTest() - { - var stream = new MemoryStream([]); - var br = new BinaryReader(stream); - bool actual = br.TryReadUInt32BigEndian(out uint read); - Assert.False(actual); - Assert.Equal(default, read); - } - - [Fact] - public void TryReadUInt32LittleEndianTest() - { - var stream = new MemoryStream([]); - var br = new BinaryReader(stream); - bool actual = br.TryReadUInt32LittleEndian(out uint read); - Assert.False(actual); - Assert.Equal(default, read); - } - - [Fact] - public void TryReadUInt32BothEndianTest() - { - var stream = new MemoryStream([]); - var br = new BinaryReader(stream); - bool actual = br.TryReadUInt32BothEndian(out BothUInt32 read); - Assert.False(actual); - Assert.Equal(default, read.LittleEndian); - Assert.Equal(default, read.BigEndian); - } - - [Fact] - public void TryReadDWORDTest() - { - var stream = new MemoryStream([]); - var br = new BinaryReader(stream); - bool actual = br.TryReadDWORD(out uint read); - Assert.False(actual); - Assert.Equal(default, read); - } - - [Fact] - public void TryReadDWORDBigEndianTest() - { - var stream = new MemoryStream([]); - var br = new BinaryReader(stream); - bool actual = br.TryReadDWORDBigEndian(out uint read); - Assert.False(actual); - Assert.Equal(default, read); - } - - [Fact] - public void TryReadDWORDLittleEndianTest() - { - var stream = new MemoryStream([]); - var br = new BinaryReader(stream); - bool actual = br.TryReadDWORDLittleEndian(out uint read); - Assert.False(actual); - Assert.Equal(default, read); - } - - [Fact] - public void TryReadDWORDBothEndianTest() - { - var stream = new MemoryStream([]); - var br = new BinaryReader(stream); - bool actual = br.TryReadDWORDBothEndian(out BothUInt32 read); - Assert.False(actual); - Assert.Equal(default, read.LittleEndian); - Assert.Equal(default, read.BigEndian); - } - - [Fact] - public void TryReadSingleTest() - { - var stream = new MemoryStream([]); - var br = new BinaryReader(stream); - bool actual = br.TryReadSingle(out float read); - Assert.False(actual); - Assert.Equal(default, read); - } - - [Fact] - public void TryReadSingleBigEndianTest() - { - var stream = new MemoryStream([]); - var br = new BinaryReader(stream); - bool actual = br.TryReadSingleBigEndian(out float read); - Assert.False(actual); - Assert.Equal(default, read); - } - - [Fact] - public void TryReadSingleLittleEndianTest() - { - var stream = new MemoryStream([]); - var br = new BinaryReader(stream); - bool actual = br.TryReadSingleLittleEndian(out float read); - Assert.False(actual); - Assert.Equal(default, read); - } - - [Fact] - public void TryReadInt48Test() - { - var stream = new MemoryStream([]); - var br = new BinaryReader(stream); - bool actual = br.TryReadInt48(out Int48 read); - Assert.False(actual); - Assert.Equal(default, (long)read); - } - - [Fact] - public void TryReadInt48BigEndianTest() - { - var stream = new MemoryStream([]); - var br = new BinaryReader(stream); - bool actual = br.TryReadInt48BigEndian(out Int48 read); - Assert.False(actual); - Assert.Equal(default, (long)read); - } - - [Fact] - public void TryReadInt48LittleEndianTest() - { - var stream = new MemoryStream([]); - var br = new BinaryReader(stream); - bool actual = br.TryReadInt48LittleEndian(out Int48 read); - Assert.False(actual); - Assert.Equal(default, (long)read); - } - - [Fact] - public void TryReadUInt48Test() - { - var stream = new MemoryStream([]); - var br = new BinaryReader(stream); - bool actual = br.TryReadUInt48(out UInt48 read); - Assert.False(actual); - Assert.Equal(default, (ulong)read); - } - - [Fact] - public void TryReadUInt48BigEndianTest() - { - var stream = new MemoryStream([]); - var br = new BinaryReader(stream); - bool actual = br.TryReadUInt48BigEndian(out UInt48 read); - Assert.False(actual); - Assert.Equal(default, (ulong)read); - } - - [Fact] - public void TryReadUInt48LittleEndianTest() - { - var stream = new MemoryStream([]); - var br = new BinaryReader(stream); - bool actual = br.TryReadUInt48LittleEndian(out UInt48 read); - Assert.False(actual); - Assert.Equal(default, (ulong)read); - } - - [Fact] - public void TryReadInt64Test() - { - var stream = new MemoryStream([]); - var br = new BinaryReader(stream); - bool actual = br.TryReadInt64(out long read); - Assert.False(actual); - Assert.Equal(default, read); - } - - [Fact] - public void TryReadInt64BigEndianTest() - { - var stream = new MemoryStream([]); - var br = new BinaryReader(stream); - bool actual = br.TryReadInt64BigEndian(out long read); - Assert.False(actual); - Assert.Equal(default, read); - } - - [Fact] - public void TryReadInt64LittleEndianTest() - { - var stream = new MemoryStream([]); - var br = new BinaryReader(stream); - bool actual = br.TryReadInt64LittleEndian(out long read); - Assert.False(actual); - Assert.Equal(default, read); - } - - [Fact] - public void TryReadInt64BothEndianTest() - { - var stream = new MemoryStream([]); - var br = new BinaryReader(stream); - bool actual = br.TryReadInt64BothEndian(out BothInt64 read); - Assert.False(actual); - Assert.Equal(default, read.LittleEndian); - Assert.Equal(default, read.BigEndian); - } - - [Fact] - public void TryReadUInt64Test() - { - var stream = new MemoryStream([]); - var br = new BinaryReader(stream); - bool actual = br.TryReadUInt64(out ulong read); - Assert.False(actual); - Assert.Equal(default, read); - } - - [Fact] - public void TryReadUInt64BigEndianTest() - { - var stream = new MemoryStream([]); - var br = new BinaryReader(stream); - bool actual = br.TryReadUInt64BigEndian(out ulong read); - Assert.False(actual); - Assert.Equal(default, read); - } - - [Fact] - public void TryReadUInt64LittleEndianTest() - { - var stream = new MemoryStream([]); - var br = new BinaryReader(stream); - bool actual = br.TryReadUInt64LittleEndian(out ulong read); - Assert.False(actual); - Assert.Equal(default, read); - } - - [Fact] - public void TryReadUInt64BothEndianTest() - { - var stream = new MemoryStream([]); - var br = new BinaryReader(stream); - bool actual = br.TryReadUInt64BothEndian(out BothUInt64 read); - Assert.False(actual); - Assert.Equal(default, read.LittleEndian); - Assert.Equal(default, read.BigEndian); - } - - [Fact] - public void TryReadQWORDTest() - { - var stream = new MemoryStream([]); - var br = new BinaryReader(stream); - bool actual = br.TryReadQWORD(out ulong read); - Assert.False(actual); - Assert.Equal(default, read); - } - - [Fact] - public void TryReadQWORDBigEndianTest() - { - var stream = new MemoryStream([]); - var br = new BinaryReader(stream); - bool actual = br.TryReadQWORDBigEndian(out ulong read); - Assert.False(actual); - Assert.Equal(default, read); - } - - [Fact] - public void TryReadQWORDLittleEndianTest() - { - var stream = new MemoryStream([]); - var br = new BinaryReader(stream); - bool actual = br.TryReadQWORDLittleEndian(out ulong read); - Assert.False(actual); - Assert.Equal(default, read); - } - - [Fact] - public void TryReadQWORDBothEndianTest() - { - var stream = new MemoryStream([]); - var br = new BinaryReader(stream); - bool actual = br.TryReadQWORDBothEndian(out BothUInt64 read); - Assert.False(actual); - Assert.Equal(default, read.LittleEndian); - Assert.Equal(default, read.BigEndian); - } - - [Fact] - public void TryReadDoubleTest() - { - var stream = new MemoryStream([]); - var br = new BinaryReader(stream); - bool actual = br.TryReadDouble(out double read); - Assert.False(actual); - Assert.Equal(default, read); - } - - [Fact] - public void TryReadDoubleBigEndianTest() - { - var stream = new MemoryStream([]); - var br = new BinaryReader(stream); - bool actual = br.TryReadDoubleBigEndian(out double read); - Assert.False(actual); - Assert.Equal(default, read); - } - - [Fact] - public void TryReadDoubleLittleEndianTest() - { - var stream = new MemoryStream([]); - var br = new BinaryReader(stream); - bool actual = br.TryReadDoubleLittleEndian(out double read); - Assert.False(actual); - Assert.Equal(default, read); - } - - [Fact] - public void TryReadGuidTest() - { - var stream = new MemoryStream([]); - var br = new BinaryReader(stream); - bool actual = br.TryReadGuid(out Guid read); - Assert.False(actual); - Assert.Equal(default, read); - } - - [Fact] - public void TryReadGuidBigEndianTest() - { - var stream = new MemoryStream([]); - var br = new BinaryReader(stream); - bool actual = br.TryReadGuidBigEndian(out Guid read); - Assert.False(actual); - Assert.Equal(default, read); - } - - [Fact] - public void TryReadGuidLittleEndianTest() - { - var stream = new MemoryStream([]); - var br = new BinaryReader(stream); - bool actual = br.TryReadGuidLittleEndian(out Guid read); - Assert.False(actual); - Assert.Equal(default, read); - } - - [Fact] - public void TryReadInt128Test() - { - var stream = new MemoryStream([]); - var br = new BinaryReader(stream); - bool actual = br.TryReadInt128(out Int128 read); - Assert.False(actual); - Assert.Equal(default, read); - } - - [Fact] - public void TryReadInt128BigEndianTest() - { - var stream = new MemoryStream([]); - var br = new BinaryReader(stream); - bool actual = br.TryReadInt128BigEndian(out Int128 read); - Assert.False(actual); - Assert.Equal(default, read); - } - - [Fact] - public void TryReadInt128LittleEndianTest() - { - var stream = new MemoryStream([]); - var br = new BinaryReader(stream); - bool actual = br.TryReadInt128LittleEndian(out Int128 read); - Assert.False(actual); - Assert.Equal(default, read); - } - - [Fact] - public void TryReadUInt128Test() - { - var stream = new MemoryStream([]); - var br = new BinaryReader(stream); - bool actual = br.TryReadUInt128(out UInt128 read); - Assert.False(actual); - Assert.Equal(default, read); - } - - [Fact] - public void TryReadUInt128BigEndianTest() - { - var stream = new MemoryStream([]); - var br = new BinaryReader(stream); - bool actual = br.TryReadUInt128BigEndian(out UInt128 read); - Assert.False(actual); - Assert.Equal(default, read); - } - - [Fact] - public void TryReadUInt128LittleEndianTest() - { - var stream = new MemoryStream([]); - var br = new BinaryReader(stream); - bool actual = br.TryReadUInt128LittleEndian(out UInt128 read); - Assert.False(actual); - Assert.Equal(default, read); - } - - [Fact] - public void TryReadDecimalTest() - { - var stream = new MemoryStream([]); - var br = new BinaryReader(stream); - bool actual = br.TryReadDecimal(out decimal read); - Assert.False(actual); - Assert.Equal(default, read); - } - - [Fact] - public void TryReadDecimalBigEndianTest() - { - var stream = new MemoryStream([]); - var br = new BinaryReader(stream); - bool actual = br.TryReadDecimalBigEndian(out decimal read); - Assert.False(actual); - Assert.Equal(default, read); - } - - [Fact] - public void TryReadDecimalLittleEndianTest() - { - var stream = new MemoryStream([]); - var br = new BinaryReader(stream); - bool actual = br.TryReadDecimalLittleEndian(out decimal read); - Assert.False(actual); - Assert.Equal(default, read); - } - - #endregion } } diff --git a/SabreTools.IO.Extensions.Test/BinaryWriterExtensionsTests.cs b/SabreTools.IO.Extensions.Test/BinaryWriterExtensionsTests.cs index 05931ca..206242c 100644 --- a/SabreTools.IO.Extensions.Test/BinaryWriterExtensionsTests.cs +++ b/SabreTools.IO.Extensions.Test/BinaryWriterExtensionsTests.cs @@ -2,8 +2,6 @@ using System; using System.IO; using System.Linq; using System.Numerics; -using System.Text; -using SabreTools.Numerics; using Xunit; namespace SabreTools.IO.Extensions.Test @@ -19,716 +17,6 @@ namespace SabreTools.IO.Extensions.Test 0x08, 0x09, 0x0A, 0x0B, 0x0C, 0x0D, 0x0E, 0x0F, ]; - /// - /// Represents the decimal value 0.0123456789 - /// - private static readonly byte[] _decimalBytes = - [ - 0x15, 0xCD, 0x5B, 0x07, 0x00, 0x00, 0x00, 0x00, - 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x0A, 0x00, - ]; - - /// - /// Test pattern for big-endian GUID created from - /// - private static readonly byte[] _guidBigEndianbytes = - [ - 0x03, 0x02, 0x01, 0x00, 0x05, 0x04, 0x07, 0x06, - 0x08, 0x09, 0x0A, 0x0B, 0x0C, 0x0D, 0x0E, 0x0F, - ]; - - [Fact] - public void WriteByteValueTest() - { - var stream = new MemoryStream(new byte[16], 0, 16, true, true); - var bw = new BinaryWriter(stream); - byte[] expected = [.. _bytes.Take(1)]; - bw.Write((byte)0x00); - ValidateBytes(expected, stream.GetBuffer()); - } - - [Fact] - public void WriteByteBothEndianTest() - { - var stream = new MemoryStream(new byte[16], 0, 16, true, true); - var bw = new BinaryWriter(stream); - byte[] expected = [.. _bytes.Take(2)]; - - int offset = 0; - bw.WriteBothEndian(_bytes.ReadByteBothEndian(ref offset)); - ValidateBytes(expected, stream.GetBuffer()); - } - - [Fact] - public void WriteBytesTest() - { - var stream = new MemoryStream(new byte[16], 0, 16, true, true); - var bw = new BinaryWriter(stream); - byte[] expected = [.. _bytes.Take(4)]; - bw.Write([0x00, 0x01, 0x02, 0x03]); - ValidateBytes(expected, stream.GetBuffer()); - } - - [Fact] - public void WriteBytesBigEndianTest() - { - var stream = new MemoryStream(new byte[16], 0, 16, true, true); - var bw = new BinaryWriter(stream); - byte[] expected = [.. _bytes.Take(4)]; - bw.WriteBigEndian([0x03, 0x02, 0x01, 0x00]); - ValidateBytes(expected, stream.GetBuffer()); - } - - [Fact] - public void WriteSByteTest() - { - var stream = new MemoryStream(new byte[16], 0, 16, true, true); - var bw = new BinaryWriter(stream); - byte[] expected = [.. _bytes.Take(1)]; - bw.Write((sbyte)0x00); - ValidateBytes(expected, stream.GetBuffer()); - } - - [Fact] - public void WriteSByteBothEndianTest() - { - var stream = new MemoryStream(new byte[16], 0, 16, true, true); - var bw = new BinaryWriter(stream); - byte[] expected = [.. _bytes.Take(2)]; - - int offset = 0; - bw.WriteBothEndian(_bytes.ReadSByteBothEndian(ref offset)); - ValidateBytes(expected, stream.GetBuffer()); - } - - [Fact] - public void WriteCharTest() - { - var stream = new MemoryStream(new byte[16], 0, 16, true, true); - var bw = new BinaryWriter(stream); - byte[] expected = [.. _bytes.Take(1)]; - bw.Write('\0'); - ValidateBytes(expected, stream.GetBuffer()); - } - - [Fact] - public void WriteCharEncodingTest() - { - var stream = new MemoryStream(new byte[16], 0, 16, true, true); - var bw = new BinaryWriter(stream); - byte[] expected = [0x00, 0x00]; - bw.Write('\0', Encoding.Unicode); - ValidateBytes(expected, stream.GetBuffer()); - } - - [Fact] - public void WriteInt16Test() - { - var stream = new MemoryStream(new byte[16], 0, 16, true, true); - var bw = new BinaryWriter(stream); - byte[] expected = [.. _bytes.Take(2)]; - bw.Write((short)0x0100); - ValidateBytes(expected, stream.GetBuffer()); - } - - [Fact] - public void WriteInt16BigEndianTest() - { - var stream = new MemoryStream(new byte[16], 0, 16, true, true); - var bw = new BinaryWriter(stream); - byte[] expected = [.. _bytes.Take(2)]; - bool write = bw.WriteBigEndian((short)0x0001); - Assert.True(write); - ValidateBytes(expected, stream.GetBuffer()); - } - - [Fact] - public void WriteInt16LittleEndianTest() - { - var stream = new MemoryStream(new byte[16], 0, 16, true, true); - var bw = new BinaryWriter(stream); - byte[] expected = [.. _bytes.Take(2)]; - bw.WriteLittleEndian((short)0x0100); - ValidateBytes(expected, stream.GetBuffer()); - } - - [Fact] - public void WriteInt16BothEndianTest() - { - var stream = new MemoryStream(new byte[16], 0, 16, true, true); - var bw = new BinaryWriter(stream); - byte[] expected = [.. _bytes.Take(4)]; - - int offset = 0; - bw.WriteBothEndian(_bytes.ReadInt16BothEndian(ref offset)); - ValidateBytes(expected, stream.GetBuffer()); - } - - [Fact] - public void WriteUInt16Test() - { - var stream = new MemoryStream(new byte[16], 0, 16, true, true); - var bw = new BinaryWriter(stream); - byte[] expected = [.. _bytes.Take(2)]; - bw.Write((ushort)0x0100); - ValidateBytes(expected, stream.GetBuffer()); - } - - [Fact] - public void WriteUInt16BigEndianTest() - { - var stream = new MemoryStream(new byte[16], 0, 16, true, true); - var bw = new BinaryWriter(stream); - byte[] expected = [.. _bytes.Take(2)]; - bool write = bw.WriteBigEndian((ushort)0x0001); - Assert.True(write); - ValidateBytes(expected, stream.GetBuffer()); - } - - [Fact] - public void WriteUInt16LittleEndianTest() - { - var stream = new MemoryStream(new byte[16], 0, 16, true, true); - var bw = new BinaryWriter(stream); - byte[] expected = [.. _bytes.Take(2)]; - bw.WriteLittleEndian((ushort)0x0100); - ValidateBytes(expected, stream.GetBuffer()); - } - - [Fact] - public void WriteUInt16BothEndianTest() - { - var stream = new MemoryStream(new byte[16], 0, 16, true, true); - var bw = new BinaryWriter(stream); - byte[] expected = [.. _bytes.Take(4)]; - - int offset = 0; - bw.WriteBothEndian(_bytes.ReadUInt16BothEndian(ref offset)); - ValidateBytes(expected, stream.GetBuffer()); - } - - [Fact] - public void WriteHalfTest() - { - var stream = new MemoryStream(new byte[16], 0, 16, true, true); - var bw = new BinaryWriter(stream); - byte[] expected = [.. _bytes.Take(2)]; - bw.Write(BitConverter.Int16BitsToHalf(0x0100)); - ValidateBytes(expected, stream.GetBuffer()); - } - - [Fact] - public void WriteHalfBigEndianTest() - { - var stream = new MemoryStream(new byte[16], 0, 16, true, true); - var bw = new BinaryWriter(stream); - byte[] expected = [.. _bytes.Take(2)]; - bool write = bw.WriteBigEndian(BitConverter.Int16BitsToHalf(0x0001)); - Assert.True(write); - ValidateBytes(expected, stream.GetBuffer()); - } - - [Fact] - public void WriteHalfLittleEndianTest() - { - var stream = new MemoryStream(new byte[16], 0, 16, true, true); - var bw = new BinaryWriter(stream); - byte[] expected = [.. _bytes.Take(2)]; - bw.WriteLittleEndian(BitConverter.Int16BitsToHalf(0x0100)); - ValidateBytes(expected, stream.GetBuffer()); - } - - [Fact] - public void WriteInt24Test() - { - var stream = new MemoryStream(new byte[16], 0, 16, true, true); - var bw = new BinaryWriter(stream); - byte[] expected = [.. _bytes.Take(3)]; - bw.Write((Int24)0x020100); - ValidateBytes(expected, stream.GetBuffer()); - } - - [Fact] - public void WriteInt24BigEndianTest() - { - var stream = new MemoryStream(new byte[16], 0, 16, true, true); - var bw = new BinaryWriter(stream); - byte[] expected = [.. _bytes.Take(3)]; - bool write = bw.WriteBigEndian((Int24)0x000102); - Assert.True(write); - ValidateBytes(expected, stream.GetBuffer()); - } - - [Fact] - public void WriteInt24LittleEndianTest() - { - var stream = new MemoryStream(new byte[16], 0, 16, true, true); - var bw = new BinaryWriter(stream); - byte[] expected = [.. _bytes.Take(3)]; - bw.WriteLittleEndian((Int24)0x020100); - ValidateBytes(expected, stream.GetBuffer()); - } - - [Fact] - public void WriteUInt24Test() - { - var stream = new MemoryStream(new byte[16], 0, 16, true, true); - var bw = new BinaryWriter(stream); - byte[] expected = [.. _bytes.Take(3)]; - bw.Write((UInt24)0x020100); - ValidateBytes(expected, stream.GetBuffer()); - } - - [Fact] - public void WriteUInt24BigEndianTest() - { - var stream = new MemoryStream(new byte[16], 0, 16, true, true); - var bw = new BinaryWriter(stream); - byte[] expected = [.. _bytes.Take(3)]; - bool write = bw.WriteBigEndian((UInt24)0x000102); - Assert.True(write); - ValidateBytes(expected, stream.GetBuffer()); - } - - [Fact] - public void WriteUInt24LittleEndianTest() - { - var stream = new MemoryStream(new byte[16], 0, 16, true, true); - var bw = new BinaryWriter(stream); - byte[] expected = [.. _bytes.Take(3)]; - bw.WriteLittleEndian((UInt24)0x020100); - ValidateBytes(expected, stream.GetBuffer()); - } - - [Fact] - public void WriteInt32Test() - { - var stream = new MemoryStream(new byte[16], 0, 16, true, true); - var bw = new BinaryWriter(stream); - byte[] expected = [.. _bytes.Take(4)]; - bw.Write(0x03020100); - ValidateBytes(expected, stream.GetBuffer()); - } - - [Fact] - public void WriteInt32BigEndianTest() - { - var stream = new MemoryStream(new byte[16], 0, 16, true, true); - var bw = new BinaryWriter(stream); - byte[] expected = [.. _bytes.Take(4)]; - bool write = bw.WriteBigEndian(0x00010203); - Assert.True(write); - ValidateBytes(expected, stream.GetBuffer()); - } - - [Fact] - public void WriteInt32LittleEndianTest() - { - var stream = new MemoryStream(new byte[16], 0, 16, true, true); - var bw = new BinaryWriter(stream); - byte[] expected = [.. _bytes.Take(4)]; - bw.WriteLittleEndian(0x03020100); - ValidateBytes(expected, stream.GetBuffer()); - } - - [Fact] - public void WriteInt32BothEndianTest() - { - var stream = new MemoryStream(new byte[16], 0, 16, true, true); - var bw = new BinaryWriter(stream); - byte[] expected = [.. _bytes.Take(8)]; - - int offset = 0; - bw.WriteBothEndian(_bytes.ReadInt32BothEndian(ref offset)); - ValidateBytes(expected, stream.GetBuffer()); - } - - [Fact] - public void WriteUInt32Test() - { - var stream = new MemoryStream(new byte[16], 0, 16, true, true); - var bw = new BinaryWriter(stream); - byte[] expected = [.. _bytes.Take(4)]; - bw.Write((uint)0x03020100); - ValidateBytes(expected, stream.GetBuffer()); - } - - [Fact] - public void WriteUInt32BigEndianTest() - { - var stream = new MemoryStream(new byte[16], 0, 16, true, true); - var bw = new BinaryWriter(stream); - byte[] expected = [.. _bytes.Take(4)]; - bool write = bw.WriteBigEndian((uint)0x00010203); - Assert.True(write); - ValidateBytes(expected, stream.GetBuffer()); - } - - [Fact] - public void WriteUInt32LittleEndianTest() - { - var stream = new MemoryStream(new byte[16], 0, 16, true, true); - var bw = new BinaryWriter(stream); - byte[] expected = [.. _bytes.Take(4)]; - bw.WriteLittleEndian((uint)0x03020100); - ValidateBytes(expected, stream.GetBuffer()); - } - - [Fact] - public void WriteUInt32BothEndianTest() - { - var stream = new MemoryStream(new byte[16], 0, 16, true, true); - var bw = new BinaryWriter(stream); - byte[] expected = [.. _bytes.Take(8)]; - - int offset = 0; - bw.WriteBothEndian(_bytes.ReadUInt32BothEndian(ref offset)); - ValidateBytes(expected, stream.GetBuffer()); - } - - [Fact] - public void WriteSingleTest() - { - var stream = new MemoryStream(new byte[16], 0, 16, true, true); - var bw = new BinaryWriter(stream); - byte[] expected = [.. _bytes.Take(4)]; - bw.Write(BitConverter.Int32BitsToSingle(0x03020100)); - ValidateBytes(expected, stream.GetBuffer()); - } - - [Fact] - public void WriteSingleBigEndianTest() - { - var stream = new MemoryStream(new byte[16], 0, 16, true, true); - var bw = new BinaryWriter(stream); - byte[] expected = [.. _bytes.Take(4)]; - bool write = bw.WriteBigEndian(BitConverter.Int32BitsToSingle(0x00010203)); - Assert.True(write); - ValidateBytes(expected, stream.GetBuffer()); - } - - [Fact] - public void WriteSingleLittleEndianTest() - { - var stream = new MemoryStream(new byte[16], 0, 16, true, true); - var bw = new BinaryWriter(stream); - byte[] expected = [.. _bytes.Take(4)]; - bool write = bw.WriteLittleEndian(BitConverter.Int32BitsToSingle(0x03020100)); - Assert.True(write); - ValidateBytes(expected, stream.GetBuffer()); - } - - [Fact] - public void WriteInt48Test() - { - var stream = new MemoryStream(new byte[16], 0, 16, true, true); - var bw = new BinaryWriter(stream); - byte[] expected = [.. _bytes.Take(6)]; - bw.Write((Int48)0x050403020100); - ValidateBytes(expected, stream.GetBuffer()); - } - - [Fact] - public void WriteInt48BigEndianTest() - { - var stream = new MemoryStream(new byte[16], 0, 16, true, true); - var bw = new BinaryWriter(stream); - byte[] expected = [.. _bytes.Take(6)]; - bool write = bw.WriteBigEndian((Int48)0x000102030405); - Assert.True(write); - ValidateBytes(expected, stream.GetBuffer()); - } - - [Fact] - public void WriteInt48LittleEndianTest() - { - var stream = new MemoryStream(new byte[16], 0, 16, true, true); - var bw = new BinaryWriter(stream); - byte[] expected = [.. _bytes.Take(6)]; - bw.WriteLittleEndian((Int48)0x050403020100); - ValidateBytes(expected, stream.GetBuffer()); - } - - [Fact] - public void WriteUInt48Test() - { - var stream = new MemoryStream(new byte[16], 0, 16, true, true); - var bw = new BinaryWriter(stream); - byte[] expected = [.. _bytes.Take(6)]; - bw.Write((UInt48)0x050403020100); - ValidateBytes(expected, stream.GetBuffer()); - } - - [Fact] - public void WriteUInt48BigEndianTest() - { - var stream = new MemoryStream(new byte[16], 0, 16, true, true); - var bw = new BinaryWriter(stream); - byte[] expected = [.. _bytes.Take(6)]; - bool write = bw.WriteBigEndian((UInt48)0x000102030405); - Assert.True(write); - ValidateBytes(expected, stream.GetBuffer()); - } - - [Fact] - public void WriteUInt48LittleEndianTest() - { - var stream = new MemoryStream(new byte[16], 0, 16, true, true); - var bw = new BinaryWriter(stream); - byte[] expected = [.. _bytes.Take(6)]; - bw.WriteLittleEndian((UInt48)0x050403020100); - ValidateBytes(expected, stream.GetBuffer()); - } - - [Fact] - public void WriteInt64Test() - { - var stream = new MemoryStream(new byte[16], 0, 16, true, true); - var bw = new BinaryWriter(stream); - byte[] expected = [.. _bytes.Take(8)]; - bw.Write(0x0706050403020100); - ValidateBytes(expected, stream.GetBuffer()); - } - - [Fact] - public void WriteInt64BigEndianTest() - { - var stream = new MemoryStream(new byte[16], 0, 16, true, true); - var bw = new BinaryWriter(stream); - byte[] expected = [.. _bytes.Take(8)]; - bool write = bw.WriteBigEndian(0x0001020304050607); - Assert.True(write); - ValidateBytes(expected, stream.GetBuffer()); - } - - [Fact] - public void WriteInt64LittleEndianTest() - { - var stream = new MemoryStream(new byte[16], 0, 16, true, true); - var bw = new BinaryWriter(stream); - byte[] expected = [.. _bytes.Take(8)]; - bw.WriteLittleEndian(0x0706050403020100); - ValidateBytes(expected, stream.GetBuffer()); - } - - [Fact] - public void WriteInt64BothEndianTest() - { - var stream = new MemoryStream(new byte[16], 0, 16, true, true); - var bw = new BinaryWriter(stream); - byte[] expected = [.. _bytes.Take(16)]; - - int offset = 0; - bw.WriteBothEndian(_bytes.ReadInt64BothEndian(ref offset)); - ValidateBytes(expected, stream.GetBuffer()); - } - - [Fact] - public void WriteUInt64Test() - { - var stream = new MemoryStream(new byte[16], 0, 16, true, true); - var bw = new BinaryWriter(stream); - byte[] expected = [.. _bytes.Take(8)]; - bw.Write((ulong)0x0706050403020100); - ValidateBytes(expected, stream.GetBuffer()); - } - - [Fact] - public void WriteUInt64BigEndianTest() - { - var stream = new MemoryStream(new byte[16], 0, 16, true, true); - var bw = new BinaryWriter(stream); - byte[] expected = [.. _bytes.Take(8)]; - bool write = bw.WriteBigEndian((ulong)0x0001020304050607); - Assert.True(write); - ValidateBytes(expected, stream.GetBuffer()); - } - - [Fact] - public void WriteUInt64LittleEndianTest() - { - var stream = new MemoryStream(new byte[16], 0, 16, true, true); - var bw = new BinaryWriter(stream); - byte[] expected = [.. _bytes.Take(8)]; - bw.WriteLittleEndian((ulong)0x0706050403020100); - ValidateBytes(expected, stream.GetBuffer()); - } - - [Fact] - public void WriteUInt64BothEndianTest() - { - var stream = new MemoryStream(new byte[16], 0, 16, true, true); - var bw = new BinaryWriter(stream); - byte[] expected = [.. _bytes.Take(16)]; - - int offset = 0; - bw.WriteBothEndian(_bytes.ReadUInt64BothEndian(ref offset)); - ValidateBytes(expected, stream.GetBuffer()); - } - - [Fact] - public void WriteDoubleTest() - { - var stream = new MemoryStream(new byte[16], 0, 16, true, true); - var bw = new BinaryWriter(stream); - byte[] expected = [.. _bytes.Take(8)]; - bw.Write(BitConverter.Int64BitsToDouble(0x0706050403020100)); - ValidateBytes(expected, stream.GetBuffer()); - } - - [Fact] - public void WriteDoubleBigEndianTest() - { - var stream = new MemoryStream(new byte[16], 0, 16, true, true); - var bw = new BinaryWriter(stream); - byte[] expected = [.. _bytes.Take(8)]; - bool write = bw.WriteBigEndian(BitConverter.Int64BitsToDouble(0x0001020304050607)); - Assert.True(write); - ValidateBytes(expected, stream.GetBuffer()); - } - - [Fact] - public void WriteDoubleLittleEndianTest() - { - var stream = new MemoryStream(new byte[16], 0, 16, true, true); - var bw = new BinaryWriter(stream); - byte[] expected = [.. _bytes.Take(8)]; - bool write = bw.WriteLittleEndian(BitConverter.Int64BitsToDouble(0x0706050403020100)); - Assert.True(write); - ValidateBytes(expected, stream.GetBuffer()); - } - - [Fact] - public void WriteGuidTest() - { - var stream = new MemoryStream(new byte[16], 0, 16, true, true); - var bw = new BinaryWriter(stream); - byte[] expected = [.. _bytes.Take(16)]; - bool write = bw.Write(new Guid(_bytes)); - Assert.True(write); - ValidateBytes(expected, stream.GetBuffer()); - } - - [Fact] - public void WriteGuidBigEndianTest() - { - var stream = new MemoryStream(new byte[16], 0, 16, true, true); - var bw = new BinaryWriter(stream); - byte[] expected = [.. _guidBigEndianbytes.Take(16)]; - bool write = bw.WriteBigEndian(new Guid(_bytes)); - Assert.True(write); - ValidateBytes(expected, stream.GetBuffer()); - } - - [Fact] - public void WriteGuidLittleEndianTest() - { - var stream = new MemoryStream(new byte[16], 0, 16, true, true); - var bw = new BinaryWriter(stream); - byte[] expected = [.. _bytes.Take(16)]; - bool write = bw.WriteLittleEndian(new Guid(_bytes)); - Assert.True(write); - ValidateBytes(expected, stream.GetBuffer()); - } - - [Fact] - public void WriteInt128Test() - { - var stream = new MemoryStream(new byte[16], 0, 16, true, true); - var bw = new BinaryWriter(stream); - byte[] expected = [.. _bytes.Take(16)]; - bool write = bw.Write((Int128)new BigInteger(_bytes)); - Assert.True(write); - ValidateBytes(expected, stream.GetBuffer()); - } - - [Fact] - public void WriteInt128BigEndianTest() - { - var stream = new MemoryStream(new byte[16], 0, 16, true, true); - var bw = new BinaryWriter(stream); - byte[] expected = [.. _bytes.Take(16)]; - bool write = bw.WriteBigEndian((Int128)new BigInteger(Enumerable.Reverse(_bytes).ToArray())); - Assert.True(write); - ValidateBytes(expected, stream.GetBuffer()); - } - - [Fact] - public void WriteInt128LittleEndianTest() - { - var stream = new MemoryStream(new byte[16], 0, 16, true, true); - var bw = new BinaryWriter(stream); - byte[] expected = [.. _bytes.Take(16)]; - bool write = bw.WriteLittleEndian((Int128)new BigInteger(_bytes)); - Assert.True(write); - ValidateBytes(expected, stream.GetBuffer()); - } - - [Fact] - public void WriteUInt128Test() - { - var stream = new MemoryStream(new byte[16], 0, 16, true, true); - var bw = new BinaryWriter(stream); - byte[] expected = [.. _bytes.Take(16)]; - bool write = bw.Write((UInt128)new BigInteger(_bytes)); - Assert.True(write); - ValidateBytes(expected, stream.GetBuffer()); - } - - [Fact] - public void WriteUInt128BigEndianTest() - { - var stream = new MemoryStream(new byte[16], 0, 16, true, true); - var bw = new BinaryWriter(stream); - byte[] expected = [.. _bytes.Take(16)]; - bool write = bw.WriteBigEndian((UInt128)new BigInteger(Enumerable.Reverse(_bytes).ToArray())); - Assert.True(write); - ValidateBytes(expected, stream.GetBuffer()); - } - - [Fact] - public void WriteUInt128LittleEndianTest() - { - var stream = new MemoryStream(new byte[16], 0, 16, true, true); - var bw = new BinaryWriter(stream); - byte[] expected = [.. _bytes.Take(16)]; - bool write = bw.WriteLittleEndian((UInt128)new BigInteger(_bytes)); - Assert.True(write); - ValidateBytes(expected, stream.GetBuffer()); - } - - [Fact] - public void WriteDecimalTest() - { - var stream = new MemoryStream(new byte[16], 0, 16, true, true); - var bw = new BinaryWriter(stream); - byte[] expected = [.. _decimalBytes.Take(16)]; - bw.Write(0.0123456789M); - ValidateBytes(expected, stream.GetBuffer()); - } - - [Fact] - public void WriteDecimalBigEndianTest() - { - var stream = new MemoryStream(new byte[16], 0, 16, true, true); - var bw = new BinaryWriter(stream); - byte[] expected = [.. _decimalBytes.Take(16).Reverse()]; - bool write = bw.WriteBigEndian(0.0123456789M); - Assert.True(write); - ValidateBytes(expected, stream.GetBuffer()); - } - - [Fact] - public void WriteDecimalLittleEndianTest() - { - var stream = new MemoryStream(new byte[16], 0, 16, true, true); - var bw = new BinaryWriter(stream); - byte[] expected = [.. _decimalBytes.Take(16)]; - bool write = bw.WriteLittleEndian(0.0123456789M); - Assert.True(write); - ValidateBytes(expected, stream.GetBuffer()); - } - [Fact] public void WriteNullTerminatedAnsiStringTest() { diff --git a/SabreTools.IO.Extensions.Test/ByteArrayReaderExtensionsTests.cs b/SabreTools.IO.Extensions.Test/ByteArrayReaderExtensionsTests.cs index b31ae78..a771988 100644 --- a/SabreTools.IO.Extensions.Test/ByteArrayReaderExtensionsTests.cs +++ b/SabreTools.IO.Extensions.Test/ByteArrayReaderExtensionsTests.cs @@ -2,7 +2,6 @@ using System; using System.Linq; using System.Numerics; using System.Text; -using SabreTools.Numerics; using Xunit; namespace SabreTools.IO.Extensions.Test @@ -18,669 +17,6 @@ namespace SabreTools.IO.Extensions.Test 0x08, 0x09, 0x0A, 0x0B, 0x0C, 0x0D, 0x0E, 0x0F, ]; - /// - /// Represents the decimal value 0.0123456789 - /// - private static readonly byte[] _decimalBytes = - [ - 0x15, 0xCD, 0x5B, 0x07, 0x00, 0x00, 0x00, 0x00, - 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x0A, 0x00, - ]; - - /// - /// Test pattern for big-endian GUID created from - /// - private static readonly byte[] _guidBigEndianbytes = - [ - 0x03, 0x02, 0x01, 0x00, 0x05, 0x04, 0x07, 0x06, - 0x08, 0x09, 0x0A, 0x0B, 0x0C, 0x0D, 0x0E, 0x0F, - ]; - - #region Exact Read - - [Fact] - public void ReadByteTest() - { - int offset = 0; - byte read = _bytes.ReadByte(ref offset); - Assert.Equal(0x00, read); - } - - [Fact] - public void ReadByteValueTest() - { - int offset = 0; - byte read = _bytes.ReadByteValue(ref offset); - Assert.Equal(0x00, read); - } - - [Fact] - public void ReadByteBothEndianTest() - { - int offset = 0; - BothUInt8 read = _bytes.ReadByteBothEndian(ref offset); - Assert.Equal(0x00, read.LittleEndian); - Assert.Equal(0x01, read.BigEndian); - } - - [Fact] - public void ReadBytesTest() - { - int offset = 0, length = 4; - byte[] read = _bytes.ReadBytes(ref offset, length); - Assert.Equal(length, read.Length); - Assert.True(read.SequenceEqual(_bytes.Take(length))); - } - - [Fact] - public void ReadSByteTest() - { - int offset = 0; - sbyte read = _bytes.ReadSByte(ref offset); - Assert.Equal(0x00, read); - } - - [Fact] - public void ReadSByteBothEndianTest() - { - int offset = 0; - BothInt8 read = _bytes.ReadSByteBothEndian(ref offset); - Assert.Equal(0x00, read.LittleEndian); - Assert.Equal(0x01, read.BigEndian); - } - - [Fact] - public void ReadCharTest() - { - int offset = 0; - char read = _bytes.ReadChar(ref offset); - Assert.Equal('\0', read); - } - - [Fact] - public void ReadInt16Test() - { - int offset = 0; - short read = _bytes.ReadInt16(ref offset); - Assert.Equal(0x0100, read); - } - - [Fact] - public void ReadInt16BigEndianTest() - { - int offset = 0; - short read = _bytes.ReadInt16BigEndian(ref offset); - Assert.Equal(0x0001, read); - } - - [Fact] - public void ReadInt16LittleEndianTest() - { - int offset = 0; - short read = _bytes.ReadInt16LittleEndian(ref offset); - Assert.Equal(0x0100, read); - } - - [Fact] - public void ReadInt16BothEndianTest() - { - int offset = 0; - BothInt16 read = _bytes.ReadInt16BothEndian(ref offset); - Assert.Equal(0x0100, read.LittleEndian); - Assert.Equal(0x0203, read.BigEndian); - } - - [Fact] - public void ReadUInt16Test() - { - int offset = 0; - ushort read = _bytes.ReadUInt16(ref offset); - Assert.Equal(0x0100, read); - } - - [Fact] - public void ReadUInt16BigEndianTest() - { - int offset = 0; - ushort read = _bytes.ReadUInt16BigEndian(ref offset); - Assert.Equal(0x0001, read); - } - - [Fact] - public void ReadUInt16LittleEndianTest() - { - int offset = 0; - ushort read = _bytes.ReadUInt16LittleEndian(ref offset); - Assert.Equal(0x0100, read); - } - - [Fact] - public void ReadUInt16BothEndianTest() - { - int offset = 0; - BothUInt16 read = _bytes.ReadUInt16BothEndian(ref offset); - Assert.Equal(0x0100, read.LittleEndian); - Assert.Equal(0x0203, read.BigEndian); - } - - [Fact] - public void ReadWORDTest() - { - int offset = 0; - ushort read = _bytes.ReadWORD(ref offset); - Assert.Equal(0x0100, read); - } - - [Fact] - public void ReadWORDBigEndianTest() - { - int offset = 0; - ushort read = _bytes.ReadWORDBigEndian(ref offset); - Assert.Equal(0x0001, read); - } - - [Fact] - public void ReadWORDLittleEndianTest() - { - int offset = 0; - ushort read = _bytes.ReadWORDLittleEndian(ref offset); - Assert.Equal(0x0100, read); - } - - [Fact] - public void ReadWORDBothEndianTest() - { - int offset = 0; - BothUInt16 read = _bytes.ReadWORDBothEndian(ref offset); - Assert.Equal(0x0100, read.LittleEndian); - Assert.Equal(0x0203, read.BigEndian); - } - - [Fact] - public void ReadHalfTest() - { - int offset = 0; - Half expected = BitConverter.Int16BitsToHalf(0x0100); - Half read = _bytes.ReadHalf(ref offset); - Assert.Equal(expected, read); - } - - [Fact] - public void ReadHalfBigEndianTest() - { - int offset = 0; - Half expected = BitConverter.Int16BitsToHalf(0x0001); - Half read = _bytes.ReadHalfBigEndian(ref offset); - Assert.Equal(expected, read); - } - - [Fact] - public void ReadHalfLittleEndianTest() - { - int offset = 0; - Half expected = BitConverter.Int16BitsToHalf(0x0100); - Half read = _bytes.ReadHalfLittleEndian(ref offset); - Assert.Equal(expected, read); - } - - [Fact] - public void ReadInt24Test() - { - int offset = 0; - Int24 read = _bytes.ReadInt24(ref offset); - Assert.Equal(0x020100, (int)read); - } - - [Fact] - public void ReadInt24BigEndianTest() - { - int offset = 0; - Int24 read = _bytes.ReadInt24BigEndian(ref offset); - Assert.Equal(0x000102, (int)read); - } - - [Fact] - public void ReadInt24LittleEndianTest() - { - int offset = 0; - Int24 read = _bytes.ReadInt24LittleEndian(ref offset); - Assert.Equal(0x020100, (int)read); - } - - [Fact] - public void ReadUInt24Test() - { - int offset = 0; - UInt24 read = _bytes.ReadUInt24(ref offset); - Assert.Equal((uint)0x020100, (uint)read); - } - - [Fact] - public void ReadUInt24BigEndianTest() - { - int offset = 0; - UInt24 read = _bytes.ReadUInt24BigEndian(ref offset); - Assert.Equal((uint)0x000102, (uint)read); - } - - [Fact] - public void ReadUInt24LittleEndianTest() - { - int offset = 0; - UInt24 read = _bytes.ReadUInt24LittleEndian(ref offset); - Assert.Equal((uint)0x020100, (uint)read); - } - - [Fact] - public void ReadInt32Test() - { - int offset = 0; - int read = _bytes.ReadInt32(ref offset); - Assert.Equal(0x03020100, read); - } - - [Fact] - public void ReadInt32BigEndianTest() - { - int offset = 0; - int read = _bytes.ReadInt32BigEndian(ref offset); - Assert.Equal(0x00010203, read); - } - - [Fact] - public void ReadInt32LittleEndianTest() - { - int offset = 0; - int read = _bytes.ReadInt32LittleEndian(ref offset); - Assert.Equal(0x03020100, read); - } - - [Fact] - public void ReadInt32BothEndianTest() - { - int offset = 0; - BothInt32 read = _bytes.ReadInt32BothEndian(ref offset); - Assert.Equal(0x03020100, read.LittleEndian); - Assert.Equal(0x04050607, read.BigEndian); - } - - [Fact] - public void ReadUInt32Test() - { - int offset = 0; - uint read = _bytes.ReadUInt32(ref offset); - Assert.Equal((uint)0x03020100, read); - } - - [Fact] - public void ReadUInt32BigEndianTest() - { - int offset = 0; - uint read = _bytes.ReadUInt32BigEndian(ref offset); - Assert.Equal((uint)0x00010203, read); - } - - [Fact] - public void ReadUInt32LittleEndianTest() - { - int offset = 0; - uint read = _bytes.ReadUInt32LittleEndian(ref offset); - Assert.Equal((uint)0x03020100, read); - } - - [Fact] - public void ReadUInt32BothEndianTest() - { - int offset = 0; - BothUInt32 read = _bytes.ReadUInt32BothEndian(ref offset); - Assert.Equal((uint)0x03020100, read.LittleEndian); - Assert.Equal((uint)0x04050607, read.BigEndian); - } - - [Fact] - public void ReadDWORDTest() - { - int offset = 0; - uint read = _bytes.ReadDWORD(ref offset); - Assert.Equal((uint)0x03020100, read); - } - - [Fact] - public void ReadDWORDBigEndianTest() - { - int offset = 0; - uint read = _bytes.ReadDWORDBigEndian(ref offset); - Assert.Equal((uint)0x00010203, read); - } - - [Fact] - public void ReadDWORDLittleEndianTest() - { - int offset = 0; - uint read = _bytes.ReadDWORDLittleEndian(ref offset); - Assert.Equal((uint)0x03020100, read); - } - - [Fact] - public void ReadDWORDBothEndianTest() - { - int offset = 0; - BothUInt32 read = _bytes.ReadDWORDBothEndian(ref offset); - Assert.Equal((uint)0x03020100, read.LittleEndian); - Assert.Equal((uint)0x04050607, read.BigEndian); - } - - [Fact] - public void ReadSingleTest() - { - int offset = 0; - float expected = BitConverter.Int32BitsToSingle(0x03020100); - float read = _bytes.ReadSingle(ref offset); - Assert.Equal(expected, read); - } - - [Fact] - public void ReadSingleBigEndianTest() - { - int offset = 0; - float expected = BitConverter.Int32BitsToSingle(0x00010203); - float read = _bytes.ReadSingleBigEndian(ref offset); - Assert.Equal(expected, read); - } - - [Fact] - public void ReadSingleLittleEndianTest() - { - int offset = 0; - float expected = BitConverter.Int32BitsToSingle(0x03020100); - float read = _bytes.ReadSingleLittleEndian(ref offset); - Assert.Equal(expected, read); - } - - [Fact] - public void ReadInt48Test() - { - int offset = 0; - Int48 read = _bytes.ReadInt48(ref offset); - Assert.Equal(0x050403020100, (long)read); - } - - [Fact] - public void ReadInt48BigEndianTest() - { - int offset = 0; - Int48 read = _bytes.ReadInt48BigEndian(ref offset); - Assert.Equal(0x000102030405, (long)read); - } - - [Fact] - public void ReadInt48LittleEndianTest() - { - int offset = 0; - Int48 read = _bytes.ReadInt48LittleEndian(ref offset); - Assert.Equal(0x050403020100, (long)read); - } - - [Fact] - public void ReadUInt48Test() - { - int offset = 0; - UInt48 read = _bytes.ReadUInt48(ref offset); - Assert.Equal((ulong)0x050403020100, (ulong)read); - } - - [Fact] - public void ReadUInt48BigEndianTest() - { - int offset = 0; - UInt48 read = _bytes.ReadUInt48BigEndian(ref offset); - Assert.Equal((ulong)0x000102030405, (ulong)read); - } - - [Fact] - public void ReadUInt48LittleEndianTest() - { - int offset = 0; - UInt48 read = _bytes.ReadUInt48LittleEndian(ref offset); - Assert.Equal((ulong)0x050403020100, (ulong)read); - } - - [Fact] - public void ReadInt64Test() - { - int offset = 0; - long read = _bytes.ReadInt64(ref offset); - Assert.Equal(0x0706050403020100, read); - } - - [Fact] - public void ReadInt64BigEndianTest() - { - int offset = 0; - long read = _bytes.ReadInt64BigEndian(ref offset); - Assert.Equal(0x0001020304050607, read); - } - - [Fact] - public void ReadInt64LittleEndianTest() - { - int offset = 0; - long read = _bytes.ReadInt64LittleEndian(ref offset); - Assert.Equal(0x0706050403020100, read); - } - - [Fact] - public void ReadInt64BothEndianTest() - { - int offset = 0; - BothInt64 read = _bytes.ReadInt64BothEndian(ref offset); - Assert.Equal(0x0706050403020100, read.LittleEndian); - Assert.Equal(0x08090A0B0C0D0E0F, read.BigEndian); - } - - [Fact] - public void ReadUInt64Test() - { - int offset = 0; - ulong read = _bytes.ReadUInt64(ref offset); - Assert.Equal((ulong)0x0706050403020100, read); - } - - [Fact] - public void ReadUInt64BigEndianTest() - { - int offset = 0; - ulong read = _bytes.ReadUInt64BigEndian(ref offset); - Assert.Equal((ulong)0x0001020304050607, read); - } - - [Fact] - public void ReadUInt64LittleEndianTest() - { - int offset = 0; - ulong read = _bytes.ReadUInt64LittleEndian(ref offset); - Assert.Equal((ulong)0x0706050403020100, read); - } - - [Fact] - public void ReadUInt64BothEndianTest() - { - int offset = 0; - BothUInt64 read = _bytes.ReadUInt64BothEndian(ref offset); - Assert.Equal((ulong)0x0706050403020100, read.LittleEndian); - Assert.Equal((ulong)0x08090A0B0C0D0E0F, read.BigEndian); - } - - [Fact] - public void ReadQWORDTest() - { - int offset = 0; - ulong read = _bytes.ReadQWORD(ref offset); - Assert.Equal((ulong)0x0706050403020100, read); - } - - [Fact] - public void ReadQWORDBigEndianTest() - { - int offset = 0; - ulong read = _bytes.ReadQWORDBigEndian(ref offset); - Assert.Equal((ulong)0x0001020304050607, read); - } - - [Fact] - public void ReadQWORDLittleEndianTest() - { - int offset = 0; - ulong read = _bytes.ReadQWORDLittleEndian(ref offset); - Assert.Equal((ulong)0x0706050403020100, read); - } - - [Fact] - public void ReadQWORDBothEndianTest() - { - int offset = 0; - BothUInt64 read = _bytes.ReadQWORDBothEndian(ref offset); - Assert.Equal((ulong)0x0706050403020100, read.LittleEndian); - Assert.Equal((ulong)0x08090A0B0C0D0E0F, read.BigEndian); - } - - [Fact] - public void ReadDoubleTest() - { - int offset = 0; - double expected = BitConverter.Int64BitsToDouble(0x0706050403020100); - double read = _bytes.ReadDouble(ref offset); - Assert.Equal(expected, read); - } - - [Fact] - public void ReadDoubleBigEndianTest() - { - int offset = 0; - double expected = BitConverter.Int64BitsToDouble(0x0001020304050607); - double read = _bytes.ReadDoubleBigEndian(ref offset); - Assert.Equal(expected, read); - } - - [Fact] - public void ReadDoubleLittleEndianTest() - { - int offset = 0; - double expected = BitConverter.Int64BitsToDouble(0x0706050403020100); - double read = _bytes.ReadDoubleLittleEndian(ref offset); - Assert.Equal(expected, read); - } - - [Fact] - public void ReadGuidTest() - { - int offset = 0; - var expected = new Guid(_bytes); - Guid read = _bytes.ReadGuid(ref offset); - Assert.Equal(expected, read); - } - - [Fact] - public void ReadGuidBigEndianTest() - { - int offset = 0; - var expected = new Guid(_guidBigEndianbytes); - Guid read = _bytes.ReadGuidBigEndian(ref offset); - Assert.Equal(expected, read); - } - - [Fact] - public void ReadGuidLittleEndianTest() - { - int offset = 0; - var expected = new Guid(_bytes); - Guid read = _bytes.ReadGuidLittleEndian(ref offset); - Assert.Equal(expected, read); - } - - [Fact] - public void ReadInt128Test() - { - int offset = 0; - var expected = (Int128)new BigInteger(_bytes); - Int128 read = _bytes.ReadInt128(ref offset); - Assert.Equal(expected, read); - } - - [Fact] - public void ReadInt128BigEndianTest() - { - int offset = 0; - var reversed = Enumerable.Reverse(_bytes).ToArray(); - var expected = (Int128)new BigInteger(reversed); - Int128 read = _bytes.ReadInt128BigEndian(ref offset); - Assert.Equal(expected, read); - } - - [Fact] - public void ReadInt128LittleEndianTest() - { - int offset = 0; - var expected = (Int128)new BigInteger(_bytes); - Int128 read = _bytes.ReadInt128LittleEndian(ref offset); - Assert.Equal(expected, read); - } - - [Fact] - public void ReadUInt128Test() - { - int offset = 0; - var expected = (UInt128)new BigInteger(_bytes); - UInt128 read = _bytes.ReadUInt128(ref offset); - Assert.Equal(expected, read); - } - - [Fact] - public void ReadUInt128BigEndianTest() - { - int offset = 0; - var reversed = Enumerable.Reverse(_bytes).ToArray(); - var expected = (UInt128)new BigInteger(reversed); - UInt128 read = _bytes.ReadUInt128BigEndian(ref offset); - Assert.Equal(expected, read); - } - - [Fact] - public void ReadUInt128LittleEndianTest() - { - int offset = 0; - var expected = (UInt128)new BigInteger(_bytes); - UInt128 read = _bytes.ReadUInt128LittleEndian(ref offset); - Assert.Equal(expected, read); - } - - [Fact] - public void ReadDecimalTest() - { - int offset = 0; - decimal expected = 0.0123456789M; - decimal read = _decimalBytes.ReadDecimal(ref offset); - Assert.Equal(expected, read); - } - - [Fact] - public void ReadDecimalBigEndianTest() - { - int offset = 0; - decimal expected = 0.0123456789M; - decimal read = Enumerable.Reverse(_decimalBytes).ToArray().ReadDecimalBigEndian(ref offset); - Assert.Equal(expected, read); - } - - [Fact] - public void ReadDecimalLittleEndianTest() - { - int offset = 0; - decimal expected = 0.0123456789M; - decimal read = _decimalBytes.ReadDecimalLittleEndian(ref offset); - Assert.Equal(expected, read); - } - [Fact] public void ReadNullTerminatedStringTest() { @@ -957,1429 +293,5 @@ namespace SabreTools.IO.Extensions.Test Assert.Equal(expected2.FieldA, read2.FieldA); Assert.Equal(expected2.FieldB, read2.FieldB); } - - #endregion - - #region Peek Read - - [Fact] - public void PeekByteTest() - { - int offset = 0; - byte read = _bytes.PeekByte(ref offset); - Assert.Equal(0x00, read); - Assert.Equal(0, offset); - } - - [Fact] - public void PeekByteValueTest() - { - int offset = 0; - byte read = _bytes.PeekByteValue(ref offset); - Assert.Equal(0x00, read); - Assert.Equal(0, offset); - } - - [Fact] - public void PeekByteBothEndianTest() - { - int offset = 0; - BothUInt8 read = _bytes.PeekByteBothEndian(ref offset); - Assert.Equal(0x00, read.LittleEndian); - Assert.Equal(0x01, read.BigEndian); - Assert.Equal(0, offset); - } - - [Fact] - public void PeekBytesTest() - { - int offset = 0, length = 4; - byte[] read = _bytes.PeekBytes(ref offset, length); - Assert.Equal(length, read.Length); - Assert.True(read.SequenceEqual(_bytes.Take(length))); - Assert.Equal(0, offset); - } - - [Fact] - public void PeekSByteTest() - { - int offset = 0; - sbyte read = _bytes.PeekSByte(ref offset); - Assert.Equal(0x00, read); - Assert.Equal(0, offset); - } - - [Fact] - public void PeekSByteBothEndianTest() - { - int offset = 0; - BothInt8 read = _bytes.PeekSByteBothEndian(ref offset); - Assert.Equal(0x00, read.LittleEndian); - Assert.Equal(0x01, read.BigEndian); - Assert.Equal(0, offset); - } - - [Fact] - public void PeekCharTest() - { - int offset = 0; - char read = _bytes.PeekChar(ref offset); - Assert.Equal('\0', read); - Assert.Equal(0, offset); - } - - [Fact] - public void PeekInt16Test() - { - int offset = 0; - short read = _bytes.PeekInt16(ref offset); - Assert.Equal(0x0100, read); - Assert.Equal(0, offset); - } - - [Fact] - public void PeekInt16BigEndianTest() - { - int offset = 0; - short read = _bytes.PeekInt16BigEndian(ref offset); - Assert.Equal(0x0001, read); - Assert.Equal(0, offset); - } - - [Fact] - public void PeekInt16LittleEndianTest() - { - int offset = 0; - short read = _bytes.PeekInt16LittleEndian(ref offset); - Assert.Equal(0x0100, read); - Assert.Equal(0, offset); - } - - [Fact] - public void PeekInt16BothEndianTest() - { - int offset = 0; - BothInt16 read = _bytes.PeekInt16BothEndian(ref offset); - Assert.Equal(0x0100, read.LittleEndian); - Assert.Equal(0x0203, read.BigEndian); - Assert.Equal(0, offset); - } - - [Fact] - public void PeekUInt16Test() - { - int offset = 0; - ushort read = _bytes.PeekUInt16(ref offset); - Assert.Equal(0x0100, read); - Assert.Equal(0, offset); - } - - [Fact] - public void PeekUInt16BigEndianTest() - { - int offset = 0; - ushort read = _bytes.PeekUInt16BigEndian(ref offset); - Assert.Equal(0x0001, read); - Assert.Equal(0, offset); - } - - [Fact] - public void PeekUInt16LittleEndianTest() - { - int offset = 0; - ushort read = _bytes.PeekUInt16LittleEndian(ref offset); - Assert.Equal(0x0100, read); - Assert.Equal(0, offset); - } - - [Fact] - public void PeekUInt16BothEndianTest() - { - int offset = 0; - BothUInt16 read = _bytes.PeekUInt16BothEndian(ref offset); - Assert.Equal(0x0100, read.LittleEndian); - Assert.Equal(0x0203, read.BigEndian); - Assert.Equal(0, offset); - } - - [Fact] - public void PeekWORDTest() - { - int offset = 0; - ushort read = _bytes.PeekWORD(ref offset); - Assert.Equal(0x0100, read); - Assert.Equal(0, offset); - } - - [Fact] - public void PeekWORDBigEndianTest() - { - int offset = 0; - ushort read = _bytes.PeekWORDBigEndian(ref offset); - Assert.Equal(0x0001, read); - Assert.Equal(0, offset); - } - - [Fact] - public void PeekWORDLittleEndianTest() - { - int offset = 0; - ushort read = _bytes.PeekWORDLittleEndian(ref offset); - Assert.Equal(0x0100, read); - Assert.Equal(0, offset); - } - - [Fact] - public void PeekWORDBothEndianTest() - { - int offset = 0; - BothUInt16 read = _bytes.PeekWORDBothEndian(ref offset); - Assert.Equal(0x0100, read.LittleEndian); - Assert.Equal(0x0203, read.BigEndian); - Assert.Equal(0, offset); - } - - [Fact] - public void PeekHalfTest() - { - int offset = 0; - Half expected = BitConverter.Int16BitsToHalf(0x0100); - Half read = _bytes.PeekHalf(ref offset); - Assert.Equal(expected, read); - Assert.Equal(0, offset); - } - - [Fact] - public void PeekHalfBigEndianTest() - { - int offset = 0; - Half expected = BitConverter.Int16BitsToHalf(0x0001); - Half read = _bytes.PeekHalfBigEndian(ref offset); - Assert.Equal(expected, read); - Assert.Equal(0, offset); - } - - [Fact] - public void PeekHalfLittleEndianTest() - { - int offset = 0; - Half expected = BitConverter.Int16BitsToHalf(0x0100); - Half read = _bytes.PeekHalfLittleEndian(ref offset); - Assert.Equal(expected, read); - Assert.Equal(0, offset); - } - - [Fact] - public void PeekInt24Test() - { - int offset = 0; - Int24 read = _bytes.PeekInt24(ref offset); - Assert.Equal(0x020100, (int)read); - Assert.Equal(0, offset); - } - - [Fact] - public void PeekInt24BigEndianTest() - { - int offset = 0; - Int24 read = _bytes.PeekInt24BigEndian(ref offset); - Assert.Equal(0x000102, (int)read); - Assert.Equal(0, offset); - } - - [Fact] - public void PeekInt24LittleEndianTest() - { - int offset = 0; - Int24 read = _bytes.PeekInt24LittleEndian(ref offset); - Assert.Equal(0x020100, (int)read); - Assert.Equal(0, offset); - } - - [Fact] - public void PeekUInt24Test() - { - int offset = 0; - UInt24 read = _bytes.PeekUInt24(ref offset); - Assert.Equal((uint)0x020100, (uint)read); - Assert.Equal(0, offset); - } - - [Fact] - public void PeekUInt24BigEndianTest() - { - int offset = 0; - UInt24 read = _bytes.PeekUInt24BigEndian(ref offset); - Assert.Equal((uint)0x000102, (uint)read); - Assert.Equal(0, offset); - } - - [Fact] - public void PeekUInt24LittleEndianTest() - { - int offset = 0; - UInt24 read = _bytes.PeekUInt24LittleEndian(ref offset); - Assert.Equal((uint)0x020100, (uint)read); - Assert.Equal(0, offset); - } - - [Fact] - public void PeekInt32Test() - { - int offset = 0; - int read = _bytes.PeekInt32(ref offset); - Assert.Equal(0x03020100, read); - Assert.Equal(0, offset); - } - - [Fact] - public void PeekInt32BigEndianTest() - { - int offset = 0; - int read = _bytes.PeekInt32BigEndian(ref offset); - Assert.Equal(0x00010203, read); - Assert.Equal(0, offset); - } - - [Fact] - public void PeekInt32LittleEndianTest() - { - int offset = 0; - int read = _bytes.PeekInt32LittleEndian(ref offset); - Assert.Equal(0x03020100, read); - Assert.Equal(0, offset); - } - - [Fact] - public void PeekInt32BothEndianTest() - { - int offset = 0; - BothInt32 read = _bytes.PeekInt32BothEndian(ref offset); - Assert.Equal(0x03020100, read.LittleEndian); - Assert.Equal(0x04050607, read.BigEndian); - Assert.Equal(0, offset); - } - - [Fact] - public void PeekUInt32Test() - { - int offset = 0; - uint read = _bytes.PeekUInt32(ref offset); - Assert.Equal((uint)0x03020100, read); - Assert.Equal(0, offset); - } - - [Fact] - public void PeekUInt32BigEndianTest() - { - int offset = 0; - uint read = _bytes.PeekUInt32BigEndian(ref offset); - Assert.Equal((uint)0x00010203, read); - Assert.Equal(0, offset); - } - - [Fact] - public void PeekUInt32LittleEndianTest() - { - int offset = 0; - uint read = _bytes.PeekUInt32LittleEndian(ref offset); - Assert.Equal((uint)0x03020100, read); - Assert.Equal(0, offset); - } - - [Fact] - public void PeekUInt32BothEndianTest() - { - int offset = 0; - BothUInt32 read = _bytes.PeekUInt32BothEndian(ref offset); - Assert.Equal((uint)0x03020100, read.LittleEndian); - Assert.Equal((uint)0x04050607, read.BigEndian); - Assert.Equal(0, offset); - } - - [Fact] - public void PeekDWORDTest() - { - int offset = 0; - uint read = _bytes.PeekDWORD(ref offset); - Assert.Equal((uint)0x03020100, read); - Assert.Equal(0, offset); - } - - [Fact] - public void PeekDWORDBigEndianTest() - { - int offset = 0; - uint read = _bytes.PeekDWORDBigEndian(ref offset); - Assert.Equal((uint)0x00010203, read); - Assert.Equal(0, offset); - } - - [Fact] - public void PeekDWORDLittleEndianTest() - { - int offset = 0; - uint read = _bytes.PeekDWORDLittleEndian(ref offset); - Assert.Equal((uint)0x03020100, read); - Assert.Equal(0, offset); - } - - [Fact] - public void PeekDWORDBothEndianTest() - { - int offset = 0; - BothUInt32 read = _bytes.PeekDWORDBothEndian(ref offset); - Assert.Equal((uint)0x03020100, read.LittleEndian); - Assert.Equal((uint)0x04050607, read.BigEndian); - Assert.Equal(0, offset); - } - - [Fact] - public void PeekSingleTest() - { - int offset = 0; - float expected = BitConverter.Int32BitsToSingle(0x03020100); - float read = _bytes.PeekSingle(ref offset); - Assert.Equal(expected, read); - Assert.Equal(0, offset); - } - - [Fact] - public void PeekSingleBigEndianTest() - { - int offset = 0; - float expected = BitConverter.Int32BitsToSingle(0x00010203); - float read = _bytes.PeekSingleBigEndian(ref offset); - Assert.Equal(expected, read); - Assert.Equal(0, offset); - } - - [Fact] - public void PeekSingleLittleEndianTest() - { - int offset = 0; - float expected = BitConverter.Int32BitsToSingle(0x03020100); - float read = _bytes.PeekSingleLittleEndian(ref offset); - Assert.Equal(expected, read); - Assert.Equal(0, offset); - } - - [Fact] - public void PeekInt48Test() - { - int offset = 0; - Int48 read = _bytes.PeekInt48(ref offset); - Assert.Equal(0x050403020100, (long)read); - Assert.Equal(0, offset); - } - - [Fact] - public void PeekInt48BigEndianTest() - { - int offset = 0; - Int48 read = _bytes.PeekInt48BigEndian(ref offset); - Assert.Equal(0x000102030405, (long)read); - Assert.Equal(0, offset); - } - - [Fact] - public void PeekInt48LittleEndianTest() - { - int offset = 0; - Int48 read = _bytes.PeekInt48LittleEndian(ref offset); - Assert.Equal(0x050403020100, (long)read); - Assert.Equal(0, offset); - } - - [Fact] - public void PeekUInt48Test() - { - int offset = 0; - UInt48 read = _bytes.PeekUInt48(ref offset); - Assert.Equal((ulong)0x050403020100, (ulong)read); - Assert.Equal(0, offset); - } - - [Fact] - public void PeekUInt48BigEndianTest() - { - int offset = 0; - UInt48 read = _bytes.PeekUInt48BigEndian(ref offset); - Assert.Equal((ulong)0x000102030405, (ulong)read); - Assert.Equal(0, offset); - } - - [Fact] - public void PeekUInt48LittleEndianTest() - { - int offset = 0; - UInt48 read = _bytes.PeekUInt48LittleEndian(ref offset); - Assert.Equal((ulong)0x050403020100, (ulong)read); - Assert.Equal(0, offset); - } - - [Fact] - public void PeekInt64Test() - { - int offset = 0; - long read = _bytes.PeekInt64(ref offset); - Assert.Equal(0x0706050403020100, read); - Assert.Equal(0, offset); - } - - [Fact] - public void PeekInt64BigEndianTest() - { - int offset = 0; - long read = _bytes.PeekInt64BigEndian(ref offset); - Assert.Equal(0x0001020304050607, read); - Assert.Equal(0, offset); - } - - [Fact] - public void PeekInt64LittleEndianTest() - { - int offset = 0; - long read = _bytes.PeekInt64LittleEndian(ref offset); - Assert.Equal(0x0706050403020100, read); - Assert.Equal(0, offset); - } - - [Fact] - public void PeekInt64BothEndianTest() - { - int offset = 0; - BothInt64 read = _bytes.PeekInt64BothEndian(ref offset); - Assert.Equal(0x0706050403020100, read.LittleEndian); - Assert.Equal(0x08090A0B0C0D0E0F, read.BigEndian); - Assert.Equal(0, offset); - } - - [Fact] - public void PeekUInt64Test() - { - int offset = 0; - ulong read = _bytes.PeekUInt64(ref offset); - Assert.Equal((ulong)0x0706050403020100, read); - Assert.Equal(0, offset); - } - - [Fact] - public void PeekUInt64BigEndianTest() - { - int offset = 0; - ulong read = _bytes.PeekUInt64BigEndian(ref offset); - Assert.Equal((ulong)0x0001020304050607, read); - Assert.Equal(0, offset); - } - - [Fact] - public void PeekUInt64LittleEndianTest() - { - int offset = 0; - ulong read = _bytes.PeekUInt64LittleEndian(ref offset); - Assert.Equal((ulong)0x0706050403020100, read); - Assert.Equal(0, offset); - } - - [Fact] - public void PeekUInt64BothEndianTest() - { - int offset = 0; - BothUInt64 read = _bytes.PeekUInt64BothEndian(ref offset); - Assert.Equal((ulong)0x0706050403020100, read.LittleEndian); - Assert.Equal((ulong)0x08090A0B0C0D0E0F, read.BigEndian); - Assert.Equal(0, offset); - } - - [Fact] - public void PeekQWORDTest() - { - int offset = 0; - ulong read = _bytes.PeekQWORD(ref offset); - Assert.Equal((ulong)0x0706050403020100, read); - Assert.Equal(0, offset); - } - - [Fact] - public void PeekQWORDBigEndianTest() - { - int offset = 0; - ulong read = _bytes.PeekQWORDBigEndian(ref offset); - Assert.Equal((ulong)0x0001020304050607, read); - Assert.Equal(0, offset); - } - - [Fact] - public void PeekQWORDLittleEndianTest() - { - int offset = 0; - ulong read = _bytes.PeekQWORDLittleEndian(ref offset); - Assert.Equal((ulong)0x0706050403020100, read); - Assert.Equal(0, offset); - } - - [Fact] - public void PeekQWORDBothEndianTest() - { - int offset = 0; - BothUInt64 read = _bytes.PeekQWORDBothEndian(ref offset); - Assert.Equal((ulong)0x0706050403020100, read.LittleEndian); - Assert.Equal((ulong)0x08090A0B0C0D0E0F, read.BigEndian); - Assert.Equal(0, offset); - } - - [Fact] - public void PeekDoubleTest() - { - int offset = 0; - double expected = BitConverter.Int64BitsToDouble(0x0706050403020100); - double read = _bytes.PeekDouble(ref offset); - Assert.Equal(expected, read); - Assert.Equal(0, offset); - } - - [Fact] - public void PeekDoubleBigEndianTest() - { - int offset = 0; - double expected = BitConverter.Int64BitsToDouble(0x0001020304050607); - double read = _bytes.PeekDoubleBigEndian(ref offset); - Assert.Equal(expected, read); - Assert.Equal(0, offset); - } - - [Fact] - public void PeekDoubleLittleEndianTest() - { - int offset = 0; - double expected = BitConverter.Int64BitsToDouble(0x0706050403020100); - double read = _bytes.PeekDoubleLittleEndian(ref offset); - Assert.Equal(expected, read); - Assert.Equal(0, offset); - } - - [Fact] - public void PeekGuidTest() - { - int offset = 0; - var expected = new Guid(_bytes); - Guid read = _bytes.PeekGuid(ref offset); - Assert.Equal(expected, read); - Assert.Equal(0, offset); - } - - [Fact] - public void PeekGuidBigEndianTest() - { - int offset = 0; - var expected = new Guid(_guidBigEndianbytes); - Guid read = _bytes.PeekGuidBigEndian(ref offset); - Assert.Equal(expected, read); - Assert.Equal(0, offset); - } - - [Fact] - public void PeekGuidLittleEndianTest() - { - int offset = 0; - var expected = new Guid(_bytes); - Guid read = _bytes.PeekGuidLittleEndian(ref offset); - Assert.Equal(expected, read); - Assert.Equal(0, offset); - } - - [Fact] - public void PeekInt128Test() - { - int offset = 0; - var expected = (Int128)new BigInteger(_bytes); - Int128 read = _bytes.PeekInt128(ref offset); - Assert.Equal(expected, read); - Assert.Equal(0, offset); - } - - [Fact] - public void PeekInt128BigEndianTest() - { - int offset = 0; - var reversed = Enumerable.Reverse(_bytes).ToArray(); - var expected = (Int128)new BigInteger(reversed); - Int128 read = _bytes.PeekInt128BigEndian(ref offset); - Assert.Equal(expected, read); - Assert.Equal(0, offset); - } - - [Fact] - public void PeekInt128LittleEndianTest() - { - int offset = 0; - var expected = (Int128)new BigInteger(_bytes); - Int128 read = _bytes.PeekInt128LittleEndian(ref offset); - Assert.Equal(expected, read); - Assert.Equal(0, offset); - } - - [Fact] - public void PeekUInt128Test() - { - int offset = 0; - var expected = (UInt128)new BigInteger(_bytes); - UInt128 read = _bytes.PeekUInt128(ref offset); - Assert.Equal(expected, read); - Assert.Equal(0, offset); - } - - [Fact] - public void PeekUInt128BigEndianTest() - { - int offset = 0; - var reversed = Enumerable.Reverse(_bytes).ToArray(); - var expected = (UInt128)new BigInteger(reversed); - UInt128 read = _bytes.PeekUInt128BigEndian(ref offset); - Assert.Equal(expected, read); - Assert.Equal(0, offset); - } - - [Fact] - public void PeekUInt128LittleEndianTest() - { - int offset = 0; - var expected = (UInt128)new BigInteger(_bytes); - UInt128 read = _bytes.PeekUInt128LittleEndian(ref offset); - Assert.Equal(expected, read); - Assert.Equal(0, offset); - } - - [Fact] - public void PeekDecimalTest() - { - int offset = 0; - decimal expected = 0.0123456789M; - decimal read = _decimalBytes.PeekDecimal(ref offset); - Assert.Equal(expected, read); - Assert.Equal(0, offset); - } - - [Fact] - public void PeekDecimalBigEndianTest() - { - int offset = 0; - decimal expected = 0.0123456789M; - decimal read = Enumerable.Reverse(_decimalBytes).ToArray().PeekDecimalBigEndian(ref offset); - Assert.Equal(expected, read); - Assert.Equal(0, offset); - } - - [Fact] - public void PeekDecimalLittleEndianTest() - { - int offset = 0; - decimal expected = 0.0123456789M; - decimal read = _decimalBytes.PeekDecimalLittleEndian(ref offset); - Assert.Equal(expected, read); - Assert.Equal(0, offset); - } - - #endregion - - #region Try Read - - [Fact] - public void TryReadByteTest() - { - int offset = 0; - bool actual = Array.Empty().TryReadByte(ref offset, out byte read); - Assert.False(actual); - Assert.Equal(default, read); - } - - [Fact] - public void TryReadByteValueTest() - { - int offset = 0; - bool actual = Array.Empty().TryReadByteValue(ref offset, out byte read); - Assert.False(actual); - Assert.Equal(default, read); - } - - [Fact] - public void TryReadByteBothEndianTest() - { - int offset = 0; - bool actual = Array.Empty().TryReadByteBothEndian(ref offset, out BothUInt8 read); - Assert.False(actual); - Assert.Equal(default, read.LittleEndian); - Assert.Equal(default, read.BigEndian); - } - - [Fact] - public void TryReadBytesTest() - { - int offset = 0, length = 4; - bool actual = Array.Empty().TryReadBytes(ref offset, length, out byte[] read); - Assert.False(actual); - Assert.Empty(read); - } - - [Fact] - public void TryReadSByteTest() - { - int offset = 0; - bool actual = Array.Empty().TryReadSByte(ref offset, out sbyte read); - Assert.False(actual); - Assert.Equal(default, read); - } - - [Fact] - public void TryReadSByteBothEndianTest() - { - int offset = 0; - bool actual = Array.Empty().TryReadSByteBothEndian(ref offset, out BothInt8 read); - Assert.False(actual); - Assert.Equal(default, read.LittleEndian); - Assert.Equal(default, read.BigEndian); - } - - [Fact] - public void TryReadCharTest() - { - int offset = 0; - bool actual = Array.Empty().TryReadChar(ref offset, out char read); - Assert.False(actual); - Assert.Equal(default, read); - } - - [Fact] - public void TryReadInt16Test() - { - int offset = 0; - bool actual = Array.Empty().TryReadInt16(ref offset, out short read); - Assert.False(actual); - Assert.Equal(default, read); - } - - [Fact] - public void TryReadInt16BigEndianTest() - { - int offset = 0; - bool actual = Array.Empty().TryReadInt16BigEndian(ref offset, out short read); - Assert.False(actual); - Assert.Equal(default, read); - } - - [Fact] - public void TryReadInt16LittleEndianTest() - { - int offset = 0; - bool actual = Array.Empty().TryReadInt16LittleEndian(ref offset, out short read); - Assert.False(actual); - Assert.Equal(default, read); - } - - [Fact] - public void TryReadInt16BothEndianTest() - { - int offset = 0; - bool actual = Array.Empty().TryReadInt16BothEndian(ref offset, out BothInt16 read); - Assert.False(actual); - Assert.Equal(default, read.LittleEndian); - Assert.Equal(default, read.BigEndian); - } - - [Fact] - public void TryReadUInt16Test() - { - int offset = 0; - bool actual = Array.Empty().TryReadUInt16(ref offset, out ushort read); - Assert.False(actual); - Assert.Equal(default, read); - } - - [Fact] - public void TryReadUInt16BigEndianTest() - { - int offset = 0; - bool actual = Array.Empty().TryReadUInt16BigEndian(ref offset, out ushort read); - Assert.False(actual); - Assert.Equal(default, read); - } - - [Fact] - public void TryReadUInt16LittleEndianTest() - { - int offset = 0; - bool actual = Array.Empty().TryReadUInt16LittleEndian(ref offset, out ushort read); - Assert.False(actual); - Assert.Equal(default, read); - } - - [Fact] - public void TryReadUInt16BothEndianTest() - { - int offset = 0; - bool actual = Array.Empty().TryReadUInt16BothEndian(ref offset, out BothUInt16 read); - Assert.False(actual); - Assert.Equal(default, read.LittleEndian); - Assert.Equal(default, read.BigEndian); - } - - [Fact] - public void TryReadWORDTest() - { - int offset = 0; - bool actual = Array.Empty().TryReadWORD(ref offset, out ushort read); - Assert.False(actual); - Assert.Equal(default, read); - } - - [Fact] - public void TryReadWORDBigEndianTest() - { - int offset = 0; - bool actual = Array.Empty().TryReadWORDBigEndian(ref offset, out ushort read); - Assert.False(actual); - Assert.Equal(default, read); - } - - [Fact] - public void TryReadWORDLittleEndianTest() - { - int offset = 0; - bool actual = Array.Empty().TryReadWORDLittleEndian(ref offset, out ushort read); - Assert.False(actual); - Assert.Equal(default, read); - } - - [Fact] - public void TryReadWORDBothEndianTest() - { - int offset = 0; - bool actual = Array.Empty().TryReadWORDBothEndian(ref offset, out BothUInt16 read); - Assert.False(actual); - Assert.Equal(default, read.LittleEndian); - Assert.Equal(default, read.BigEndian); - } - - [Fact] - public void TryReadHalfTest() - { - int offset = 0; - bool actual = Array.Empty().TryReadHalf(ref offset, out Half read); - Assert.False(actual); - Assert.Equal(default, read); - } - - [Fact] - public void TryReadHalfBigEndianTest() - { - int offset = 0; - bool actual = Array.Empty().TryReadHalfBigEndian(ref offset, out Half read); - Assert.False(actual); - Assert.Equal(default, read); - } - - [Fact] - public void TryReadHalfLittleEndianTest() - { - int offset = 0; - bool actual = Array.Empty().TryReadHalfLittleEndian(ref offset, out Half read); - Assert.False(actual); - Assert.Equal(default, read); - } - - [Fact] - public void TryReadInt24Test() - { - int offset = 0; - bool actual = Array.Empty().TryReadInt24(ref offset, out Int24 read); - Assert.False(actual); - Assert.Equal(default, (int)read); - } - - [Fact] - public void TryReadInt24BigEndianTest() - { - int offset = 0; - bool actual = Array.Empty().TryReadInt24BigEndian(ref offset, out Int24 read); - Assert.False(actual); - Assert.Equal(default, (int)read); - } - - [Fact] - public void TryReadInt24LittleEndianTest() - { - int offset = 0; - bool actual = Array.Empty().TryReadInt24LittleEndian(ref offset, out Int24 read); - Assert.False(actual); - Assert.Equal(default, (int)read); - } - - [Fact] - public void TryReadUInt24Test() - { - int offset = 0; - bool actual = Array.Empty().TryReadUInt24(ref offset, out UInt24 read); - Assert.False(actual); - Assert.Equal(default, (uint)read); - } - - [Fact] - public void TryReadUInt24BigEndianTest() - { - int offset = 0; - bool actual = Array.Empty().TryReadUInt24BigEndian(ref offset, out UInt24 read); - Assert.False(actual); - Assert.Equal(default, (uint)read); - } - - [Fact] - public void TryReadUInt24LittleEndianTest() - { - int offset = 0; - bool actual = Array.Empty().TryReadUInt24LittleEndian(ref offset, out UInt24 read); - Assert.False(actual); - Assert.Equal(default, (uint)read); - } - - [Fact] - public void TryReadInt32Test() - { - int offset = 0; - bool actual = Array.Empty().TryReadInt32(ref offset, out int read); - Assert.False(actual); - Assert.Equal(default, read); - } - - [Fact] - public void TryReadInt32BigEndianTest() - { - int offset = 0; - bool actual = Array.Empty().TryReadInt32BigEndian(ref offset, out int read); - Assert.False(actual); - Assert.Equal(default, read); - } - - [Fact] - public void TryReadInt32LittleEndianTest() - { - int offset = 0; - bool actual = Array.Empty().TryReadInt32LittleEndian(ref offset, out int read); - Assert.False(actual); - Assert.Equal(default, read); - } - - [Fact] - public void TryReadInt32BothEndianTest() - { - int offset = 0; - bool actual = Array.Empty().TryReadInt32BothEndian(ref offset, out BothInt32 read); - Assert.False(actual); - Assert.Equal(default, read.LittleEndian); - Assert.Equal(default, read.BigEndian); - } - - [Fact] - public void TryReadUInt32Test() - { - int offset = 0; - bool actual = Array.Empty().TryReadUInt32(ref offset, out uint read); - Assert.False(actual); - Assert.Equal(default, read); - } - - [Fact] - public void TryReadUInt32BigEndianTest() - { - int offset = 0; - bool actual = Array.Empty().TryReadUInt32BigEndian(ref offset, out uint read); - Assert.False(actual); - Assert.Equal(default, read); - } - - [Fact] - public void TryReadUInt32LittleEndianTest() - { - int offset = 0; - bool actual = Array.Empty().TryReadUInt32LittleEndian(ref offset, out uint read); - Assert.False(actual); - Assert.Equal(default, read); - } - - [Fact] - public void TryReadUInt32BothEndianTest() - { - int offset = 0; - bool actual = Array.Empty().TryReadUInt32BothEndian(ref offset, out BothUInt32 read); - Assert.False(actual); - Assert.Equal(default, read.LittleEndian); - Assert.Equal(default, read.BigEndian); - } - - [Fact] - public void TryReadDWORDTest() - { - int offset = 0; - bool actual = Array.Empty().TryReadDWORD(ref offset, out uint read); - Assert.False(actual); - Assert.Equal(default, read); - } - - [Fact] - public void TryReadDWORDBigEndianTest() - { - int offset = 0; - bool actual = Array.Empty().TryReadDWORDBigEndian(ref offset, out uint read); - Assert.False(actual); - Assert.Equal(default, read); - } - - [Fact] - public void TryReadDWORDLittleEndianTest() - { - int offset = 0; - bool actual = Array.Empty().TryReadDWORDLittleEndian(ref offset, out uint read); - Assert.False(actual); - Assert.Equal(default, read); - } - - [Fact] - public void TryReadDWORDBothEndianTest() - { - int offset = 0; - bool actual = Array.Empty().TryReadDWORDBothEndian(ref offset, out BothUInt32 read); - Assert.False(actual); - Assert.Equal(default, read.LittleEndian); - Assert.Equal(default, read.BigEndian); - } - - [Fact] - public void TryReadSingleTest() - { - int offset = 0; - bool actual = Array.Empty().TryReadSingle(ref offset, out float read); - Assert.False(actual); - Assert.Equal(default, read); - } - - [Fact] - public void TryReadSingleBigEndianTest() - { - int offset = 0; - bool actual = Array.Empty().TryReadSingleBigEndian(ref offset, out float read); - Assert.False(actual); - Assert.Equal(default, read); - } - - [Fact] - public void TryReadSingleLittleEndianTest() - { - int offset = 0; - bool actual = Array.Empty().TryReadSingleLittleEndian(ref offset, out float read); - Assert.False(actual); - Assert.Equal(default, read); - } - - [Fact] - public void TryReadInt48Test() - { - int offset = 0; - bool actual = Array.Empty().TryReadInt48(ref offset, out Int48 read); - Assert.False(actual); - Assert.Equal(default, (long)read); - } - - [Fact] - public void TryReadInt48BigEndianTest() - { - int offset = 0; - bool actual = Array.Empty().TryReadInt48BigEndian(ref offset, out Int48 read); - Assert.False(actual); - Assert.Equal(default, (long)read); - } - - [Fact] - public void TryReadInt48LittleEndianTest() - { - int offset = 0; - bool actual = Array.Empty().TryReadInt48LittleEndian(ref offset, out Int48 read); - Assert.False(actual); - Assert.Equal(default, (long)read); - } - - [Fact] - public void TryReadUInt48Test() - { - int offset = 0; - bool actual = Array.Empty().TryReadUInt48(ref offset, out UInt48 read); - Assert.False(actual); - Assert.Equal(default, (ulong)read); - } - - [Fact] - public void TryReadUInt48BigEndianTest() - { - int offset = 0; - bool actual = Array.Empty().TryReadUInt48BigEndian(ref offset, out UInt48 read); - Assert.False(actual); - Assert.Equal(default, (ulong)read); - } - - [Fact] - public void TryReadUInt48LittleEndianTest() - { - int offset = 0; - bool actual = Array.Empty().TryReadUInt48LittleEndian(ref offset, out UInt48 read); - Assert.False(actual); - Assert.Equal(default, (ulong)read); - } - - [Fact] - public void TryReadInt64Test() - { - int offset = 0; - bool actual = Array.Empty().TryReadInt64(ref offset, out long read); - Assert.False(actual); - Assert.Equal(default, read); - } - - [Fact] - public void TryReadInt64BigEndianTest() - { - int offset = 0; - bool actual = Array.Empty().TryReadInt64BigEndian(ref offset, out long read); - Assert.False(actual); - Assert.Equal(default, read); - } - - [Fact] - public void TryReadInt64LittleEndianTest() - { - int offset = 0; - bool actual = Array.Empty().TryReadInt64LittleEndian(ref offset, out long read); - Assert.False(actual); - Assert.Equal(default, read); - } - - [Fact] - public void TryReadInt64BothEndianTest() - { - int offset = 0; - bool actual = Array.Empty().TryReadInt64BothEndian(ref offset, out BothInt64 read); - Assert.False(actual); - Assert.Equal(default, read.LittleEndian); - Assert.Equal(default, read.BigEndian); - } - - [Fact] - public void TryReadUInt64Test() - { - int offset = 0; - bool actual = Array.Empty().TryReadUInt64(ref offset, out ulong read); - Assert.False(actual); - Assert.Equal(default, read); - } - - [Fact] - public void TryReadUInt64BigEndianTest() - { - int offset = 0; - bool actual = Array.Empty().TryReadUInt64BigEndian(ref offset, out ulong read); - Assert.False(actual); - Assert.Equal(default, read); - } - - [Fact] - public void TryReadUInt64LittleEndianTest() - { - int offset = 0; - bool actual = Array.Empty().TryReadUInt64LittleEndian(ref offset, out ulong read); - Assert.False(actual); - Assert.Equal(default, read); - } - - [Fact] - public void TryReadUInt64BothEndianTest() - { - int offset = 0; - bool actual = Array.Empty().TryReadUInt64BothEndian(ref offset, out BothUInt64 read); - Assert.False(actual); - Assert.Equal(default, read.LittleEndian); - Assert.Equal(default, read.BigEndian); - } - - [Fact] - public void TryReadQWORDTest() - { - int offset = 0; - bool actual = Array.Empty().TryReadQWORD(ref offset, out ulong read); - Assert.False(actual); - Assert.Equal(default, read); - } - - [Fact] - public void TryReadQWORDBigEndianTest() - { - int offset = 0; - bool actual = Array.Empty().TryReadQWORDBigEndian(ref offset, out ulong read); - Assert.False(actual); - Assert.Equal(default, read); - } - - [Fact] - public void TryReadQWORDLittleEndianTest() - { - int offset = 0; - bool actual = Array.Empty().TryReadQWORDLittleEndian(ref offset, out ulong read); - Assert.False(actual); - Assert.Equal(default, read); - } - - [Fact] - public void TryReadQWORDBothEndianTest() - { - int offset = 0; - bool actual = Array.Empty().TryReadQWORDBothEndian(ref offset, out BothUInt64 read); - Assert.False(actual); - Assert.Equal(default, read.LittleEndian); - Assert.Equal(default, read.BigEndian); - } - - [Fact] - public void TryReadDoubleTest() - { - int offset = 0; - bool actual = Array.Empty().TryReadDouble(ref offset, out double read); - Assert.False(actual); - Assert.Equal(default, read); - } - - [Fact] - public void TryReadDoubleBigEndianTest() - { - int offset = 0; - bool actual = Array.Empty().TryReadDoubleBigEndian(ref offset, out double read); - Assert.False(actual); - Assert.Equal(default, read); - } - - [Fact] - public void TryReadDoubleLittleEndianTest() - { - int offset = 0; - bool actual = Array.Empty().TryReadDoubleLittleEndian(ref offset, out double read); - Assert.False(actual); - Assert.Equal(default, read); - } - - [Fact] - public void TryReadGuidTest() - { - int offset = 0; - bool actual = Array.Empty().TryReadGuid(ref offset, out Guid read); - Assert.False(actual); - Assert.Equal(default, read); - } - - [Fact] - public void TryReadGuidBigEndianTest() - { - int offset = 0; - bool actual = Array.Empty().TryReadGuidBigEndian(ref offset, out Guid read); - Assert.False(actual); - Assert.Equal(default, read); - } - - [Fact] - public void TryReadGuidLittleEndianTest() - { - int offset = 0; - bool actual = Array.Empty().TryReadGuidLittleEndian(ref offset, out Guid read); - Assert.False(actual); - Assert.Equal(default, read); - } - - [Fact] - public void TryReadInt128Test() - { - int offset = 0; - bool actual = Array.Empty().TryReadInt128(ref offset, out Int128 read); - Assert.False(actual); - Assert.Equal(default, read); - } - - [Fact] - public void TryReadInt128BigEndianTest() - { - int offset = 0; - bool actual = Array.Empty().TryReadInt128BigEndian(ref offset, out Int128 read); - Assert.False(actual); - Assert.Equal(default, read); - } - - [Fact] - public void TryReadInt128LittleEndianTest() - { - int offset = 0; - bool actual = Array.Empty().TryReadInt128LittleEndian(ref offset, out Int128 read); - Assert.False(actual); - Assert.Equal(default, read); - } - - [Fact] - public void TryReadUInt128Test() - { - int offset = 0; - bool actual = Array.Empty().TryReadUInt128(ref offset, out UInt128 read); - Assert.False(actual); - Assert.Equal(default, read); - } - - [Fact] - public void TryReadUInt128BigEndianTest() - { - int offset = 0; - bool actual = Array.Empty().TryReadUInt128BigEndian(ref offset, out UInt128 read); - Assert.False(actual); - Assert.Equal(default, read); - } - - [Fact] - public void TryReadUInt128LittleEndianTest() - { - int offset = 0; - bool actual = Array.Empty().TryReadUInt128LittleEndian(ref offset, out UInt128 read); - Assert.False(actual); - Assert.Equal(default, read); - } - - [Fact] - public void TryReadDecimalTest() - { - int offset = 0; - bool actual = Array.Empty().TryReadDecimal(ref offset, out decimal read); - Assert.False(actual); - Assert.Equal(default, read); - } - - [Fact] - public void TryReadDecimalBigEndianTest() - { - int offset = 0; - bool actual = Array.Empty().TryReadDecimalBigEndian(ref offset, out decimal read); - Assert.False(actual); - Assert.Equal(default, read); - } - - [Fact] - public void TryReadDecimalLittleEndianTest() - { - int offset = 0; - bool actual = Array.Empty().TryReadDecimalLittleEndian(ref offset, out decimal read); - Assert.False(actual); - Assert.Equal(default, read); - } - - #endregion } } diff --git a/SabreTools.IO.Extensions.Test/ByteArrayWriterExtensionsTests.cs b/SabreTools.IO.Extensions.Test/ByteArrayWriterExtensionsTests.cs index baed3b2..81ef8a7 100644 --- a/SabreTools.IO.Extensions.Test/ByteArrayWriterExtensionsTests.cs +++ b/SabreTools.IO.Extensions.Test/ByteArrayWriterExtensionsTests.cs @@ -1,8 +1,6 @@ using System; using System.Linq; using System.Numerics; -using System.Text; -using SabreTools.Numerics; using Xunit; namespace SabreTools.IO.Extensions.Test @@ -18,747 +16,6 @@ namespace SabreTools.IO.Extensions.Test 0x08, 0x09, 0x0A, 0x0B, 0x0C, 0x0D, 0x0E, 0x0F, ]; - /// - /// Represents the decimal value 0.0123456789 - /// - private static readonly byte[] _decimalBytes = - [ - 0x15, 0xCD, 0x5B, 0x07, 0x00, 0x00, 0x00, 0x00, - 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x0A, 0x00, - ]; - - /// - /// Test pattern for big-endian GUID created from - /// - private static readonly byte[] _guidBigEndianbytes = - [ - 0x03, 0x02, 0x01, 0x00, 0x05, 0x04, 0x07, 0x06, - 0x08, 0x09, 0x0A, 0x0B, 0x0C, 0x0D, 0x0E, 0x0F, - ]; - - [Fact] - public void WriteByteTest() - { - byte[] buffer = new byte[16]; - int offset = 0; - byte[] expected = [.. _bytes.Take(1)]; - bool write = buffer.Write(ref offset, (byte)0x00); - Assert.True(write); - ValidateBytes(expected, buffer); - } - - [Fact] - public void WriteByteBothEndianTest() - { - byte[] buffer = new byte[16]; - int offset = 0; - byte[] expected = [.. _bytes.Take(2)]; - - int readOffset = 0; - buffer.WriteBothEndian(ref offset, _bytes.ReadByteBothEndian(ref readOffset)); - ValidateBytes(expected, buffer); - } - - [Fact] - public void WriteBytesTest() - { - byte[] buffer = new byte[16]; - int offset = 0; - byte[] expected = [.. _bytes.Take(4)]; - bool write = buffer.Write(ref offset, [0x00, 0x01, 0x02, 0x03]); - Assert.True(write); - ValidateBytes(expected, buffer); - } - - [Fact] - public void WriteBytesBigEndianTest() - { - byte[] buffer = new byte[16]; - int offset = 0; - byte[] expected = [.. _bytes.Take(4)]; - bool write = buffer.WriteBigEndian(ref offset, [0x03, 0x02, 0x01, 0x00]); - Assert.True(write); - ValidateBytes(expected, buffer); - } - - [Fact] - public void WriteSByteTest() - { - byte[] buffer = new byte[16]; - int offset = 0; - byte[] expected = [.. _bytes.Take(1)]; - bool write = buffer.Write(ref offset, (sbyte)0x00); - Assert.True(write); - ValidateBytes(expected, buffer); - } - - [Fact] - public void WriteSByteBothEndianTest() - { - byte[] buffer = new byte[16]; - int offset = 0; - byte[] expected = [.. _bytes.Take(2)]; - - int readOffset = 0; - buffer.WriteBothEndian(ref offset, _bytes.ReadSByteBothEndian(ref readOffset)); - ValidateBytes(expected, buffer); - } - - [Fact] - public void WriteCharTest() - { - byte[] buffer = new byte[16]; - int offset = 0; - byte[] expected = [.. _bytes.Take(1)]; - bool write = buffer.Write(ref offset, '\0'); - Assert.True(write); - ValidateBytes(expected, buffer); - } - - [Fact] - public void WriteCharEncodingTest() - { - byte[] buffer = new byte[16]; - int offset = 0; - byte[] expected = [0x00, 0x00]; - bool write = buffer.Write(ref offset, '\0', Encoding.Unicode); - Assert.True(write); - ValidateBytes(expected, buffer); - } - - [Fact] - public void WriteInt16Test() - { - byte[] buffer = new byte[16]; - int offset = 0; - byte[] expected = [.. _bytes.Take(2)]; - bool write = buffer.Write(ref offset, (short)0x0100); - Assert.True(write); - ValidateBytes(expected, buffer); - } - - [Fact] - public void WriteInt16BigEndianTest() - { - byte[] buffer = new byte[16]; - int offset = 0; - byte[] expected = [.. _bytes.Take(2)]; - bool write = buffer.WriteBigEndian(ref offset, (short)0x0001); - Assert.True(write); - ValidateBytes(expected, buffer); - } - - [Fact] - public void WriteInt16LittleEndianTest() - { - byte[] buffer = new byte[16]; - int offset = 0; - byte[] expected = [.. _bytes.Take(2)]; - bool write = buffer.WriteLittleEndian(ref offset, (short)0x0100); - Assert.True(write); - ValidateBytes(expected, buffer); - } - - [Fact] - public void WriteInt16BothEndianTest() - { - byte[] buffer = new byte[16]; - int offset = 0; - byte[] expected = [.. _bytes.Take(4)]; - - int readOffset = 0; - buffer.WriteBothEndian(ref offset, _bytes.ReadInt16BothEndian(ref readOffset)); - ValidateBytes(expected, buffer); - } - - [Fact] - public void WriteUInt16Test() - { - byte[] buffer = new byte[16]; - int offset = 0; - byte[] expected = [.. _bytes.Take(2)]; - bool write = buffer.Write(ref offset, (ushort)0x0100); - Assert.True(write); - ValidateBytes(expected, buffer); - } - - [Fact] - public void WriteUInt16BigEndianTest() - { - byte[] buffer = new byte[16]; - int offset = 0; - byte[] expected = [.. _bytes.Take(2)]; - bool write = buffer.WriteBigEndian(ref offset, (ushort)0x0001); - Assert.True(write); - ValidateBytes(expected, buffer); - } - - [Fact] - public void WriteUInt16LittleEndianTest() - { - byte[] buffer = new byte[16]; - int offset = 0; - byte[] expected = [.. _bytes.Take(2)]; - bool write = buffer.WriteLittleEndian(ref offset, (ushort)0x0100); - Assert.True(write); - ValidateBytes(expected, buffer); - } - - [Fact] - public void WriteUInt16BothEndianTest() - { - byte[] buffer = new byte[16]; - int offset = 0; - byte[] expected = [.. _bytes.Take(4)]; - - int readOffset = 0; - buffer.WriteBothEndian(ref offset, _bytes.ReadUInt16BothEndian(ref readOffset)); - ValidateBytes(expected, buffer); - } - - [Fact] - public void WriteHalfTest() - { - byte[] buffer = new byte[16]; - int offset = 0; - byte[] expected = [.. _bytes.Take(2)]; - bool write = buffer.Write(ref offset, BitConverter.Int16BitsToHalf(0x0100)); - Assert.True(write); - ValidateBytes(expected, buffer); - } - - [Fact] - public void WriteHalfBigEndianTest() - { - byte[] buffer = new byte[16]; - int offset = 0; - byte[] expected = [.. _bytes.Take(2)]; - bool write = buffer.WriteBigEndian(ref offset, BitConverter.Int16BitsToHalf(0x0001)); - Assert.True(write); - ValidateBytes(expected, buffer); - } - - [Fact] - public void WriteHalfLittleEndianTest() - { - byte[] buffer = new byte[16]; - int offset = 0; - byte[] expected = [.. _bytes.Take(2)]; - bool write = buffer.WriteLittleEndian(ref offset, BitConverter.Int16BitsToHalf(0x0100)); - Assert.True(write); - ValidateBytes(expected, buffer); - } - - [Fact] - public void WriteInt24Test() - { - byte[] buffer = new byte[16]; - int offset = 0; - byte[] expected = [.. _bytes.Take(3)]; - bool write = buffer.Write(ref offset, (Int24)0x020100); - Assert.True(write); - ValidateBytes(expected, buffer); - } - - [Fact] - public void WriteInt24BigEndianTest() - { - byte[] buffer = new byte[16]; - int offset = 0; - byte[] expected = [.. _bytes.Take(3)]; - bool write = buffer.WriteBigEndian(ref offset, (Int24)0x000102); - Assert.True(write); - ValidateBytes(expected, buffer); - } - - [Fact] - public void WriteInt24LittleEndianTest() - { - byte[] buffer = new byte[16]; - int offset = 0; - byte[] expected = [.. _bytes.Take(3)]; - bool write = buffer.WriteLittleEndian(ref offset, (Int24)0x020100); - Assert.True(write); - ValidateBytes(expected, buffer); - } - - [Fact] - public void WriteUInt24Test() - { - byte[] buffer = new byte[16]; - int offset = 0; - byte[] expected = [.. _bytes.Take(3)]; - bool write = buffer.Write(ref offset, (UInt24)0x020100); - Assert.True(write); - ValidateBytes(expected, buffer); - } - - [Fact] - public void WriteUInt24BigEndianTest() - { - byte[] buffer = new byte[16]; - int offset = 0; - byte[] expected = [.. _bytes.Take(3)]; - bool write = buffer.WriteBigEndian(ref offset, (UInt24)0x000102); - Assert.True(write); - ValidateBytes(expected, buffer); - } - - [Fact] - public void WriteUInt24LittleEndianTest() - { - byte[] buffer = new byte[16]; - int offset = 0; - byte[] expected = [.. _bytes.Take(3)]; - bool write = buffer.WriteLittleEndian(ref offset, (UInt24)0x020100); - Assert.True(write); - ValidateBytes(expected, buffer); - } - - [Fact] - public void WriteInt32Test() - { - byte[] buffer = new byte[16]; - int offset = 0; - byte[] expected = [.. _bytes.Take(4)]; - bool write = buffer.Write(ref offset, 0x03020100); - Assert.True(write); - ValidateBytes(expected, buffer); - } - - [Fact] - public void WriteInt32BigEndianTest() - { - byte[] buffer = new byte[16]; - int offset = 0; - byte[] expected = [.. _bytes.Take(4)]; - bool write = buffer.WriteBigEndian(ref offset, 0x00010203); - Assert.True(write); - ValidateBytes(expected, buffer); - } - - [Fact] - public void WriteInt32LittleEndianTest() - { - byte[] buffer = new byte[16]; - int offset = 0; - byte[] expected = [.. _bytes.Take(4)]; - bool write = buffer.WriteLittleEndian(ref offset, 0x03020100); - Assert.True(write); - ValidateBytes(expected, buffer); - } - - [Fact] - public void WriteInt32BothEndianTest() - { - byte[] buffer = new byte[16]; - int offset = 0; - byte[] expected = [.. _bytes.Take(8)]; - - int readOffset = 0; - buffer.WriteBothEndian(ref offset, _bytes.ReadInt32BothEndian(ref readOffset)); - ValidateBytes(expected, buffer); - } - - [Fact] - public void WriteUInt32Test() - { - byte[] buffer = new byte[16]; - int offset = 0; - byte[] expected = [.. _bytes.Take(4)]; - bool write = buffer.Write(ref offset, (uint)0x03020100); - Assert.True(write); - ValidateBytes(expected, buffer); - } - - [Fact] - public void WriteUInt32BigEndianTest() - { - byte[] buffer = new byte[16]; - int offset = 0; - byte[] expected = [.. _bytes.Take(4)]; - bool write = buffer.WriteBigEndian(ref offset, (uint)0x00010203); - Assert.True(write); - ValidateBytes(expected, buffer); - } - - [Fact] - public void WriteUInt32LittleEndianTest() - { - byte[] buffer = new byte[16]; - int offset = 0; - byte[] expected = [.. _bytes.Take(4)]; - bool write = buffer.WriteLittleEndian(ref offset, (uint)0x03020100); - Assert.True(write); - ValidateBytes(expected, buffer); - } - - [Fact] - public void WriteUInt32BothEndianTest() - { - byte[] buffer = new byte[16]; - int offset = 0; - byte[] expected = [.. _bytes.Take(8)]; - - int readOffset = 0; - buffer.WriteBothEndian(ref offset, _bytes.ReadUInt32BothEndian(ref readOffset)); - ValidateBytes(expected, buffer); - } - - [Fact] - public void WriteSingleTest() - { - byte[] buffer = new byte[16]; - int offset = 0; - byte[] expected = [.. _bytes.Take(4)]; - bool write = buffer.Write(ref offset, BitConverter.Int32BitsToSingle(0x03020100)); - Assert.True(write); - ValidateBytes(expected, buffer); - } - - [Fact] - public void WriteSingleBigEndianTest() - { - byte[] buffer = new byte[16]; - int offset = 0; - byte[] expected = [.. _bytes.Take(4)]; - bool write = buffer.WriteBigEndian(ref offset, BitConverter.Int32BitsToSingle(0x00010203)); - Assert.True(write); - ValidateBytes(expected, buffer); - } - - [Fact] - public void WriteSingleLittleEndianTest() - { - byte[] buffer = new byte[16]; - int offset = 0; - byte[] expected = [.. _bytes.Take(4)]; - bool write = buffer.WriteLittleEndian(ref offset, BitConverter.Int32BitsToSingle(0x03020100)); - Assert.True(write); - ValidateBytes(expected, buffer); - } - - [Fact] - public void WriteInt48Test() - { - byte[] buffer = new byte[16]; - int offset = 0; - byte[] expected = [.. _bytes.Take(6)]; - bool write = buffer.Write(ref offset, (Int48)0x050403020100); - Assert.True(write); - ValidateBytes(expected, buffer); - } - - [Fact] - public void WriteInt48BigEndianTest() - { - byte[] buffer = new byte[16]; - int offset = 0; - byte[] expected = [.. _bytes.Take(6)]; - bool write = buffer.WriteBigEndian(ref offset, (Int48)0x000102030405); - Assert.True(write); - ValidateBytes(expected, buffer); - } - - [Fact] - public void WriteInt48LittleEndianTest() - { - byte[] buffer = new byte[16]; - int offset = 0; - byte[] expected = [.. _bytes.Take(6)]; - bool write = buffer.WriteLittleEndian(ref offset, (Int48)0x050403020100); - Assert.True(write); - ValidateBytes(expected, buffer); - } - - [Fact] - public void WriteUInt48Test() - { - byte[] buffer = new byte[16]; - int offset = 0; - byte[] expected = [.. _bytes.Take(6)]; - bool write = buffer.Write(ref offset, (UInt48)0x050403020100); - Assert.True(write); - ValidateBytes(expected, buffer); - } - - [Fact] - public void WriteUInt48BigEndianTest() - { - byte[] buffer = new byte[16]; - int offset = 0; - byte[] expected = [.. _bytes.Take(6)]; - bool write = buffer.WriteBigEndian(ref offset, (UInt48)0x000102030405); - Assert.True(write); - ValidateBytes(expected, buffer); - } - - [Fact] - public void WriteUInt48LittleEndianTest() - { - byte[] buffer = new byte[16]; - int offset = 0; - byte[] expected = [.. _bytes.Take(6)]; - bool write = buffer.WriteLittleEndian(ref offset, (UInt48)0x050403020100); - Assert.True(write); - ValidateBytes(expected, buffer); - } - - [Fact] - public void WriteInt64Test() - { - byte[] buffer = new byte[16]; - int offset = 0; - byte[] expected = [.. _bytes.Take(8)]; - bool write = buffer.Write(ref offset, 0x0706050403020100); - Assert.True(write); - ValidateBytes(expected, buffer); - } - - [Fact] - public void WriteInt64BigEndianTest() - { - byte[] buffer = new byte[16]; - int offset = 0; - byte[] expected = [.. _bytes.Take(8)]; - bool write = buffer.WriteBigEndian(ref offset, 0x0001020304050607); - Assert.True(write); - ValidateBytes(expected, buffer); - } - - [Fact] - public void WriteInt64LittleEndianTest() - { - byte[] buffer = new byte[16]; - int offset = 0; - byte[] expected = [.. _bytes.Take(8)]; - bool write = buffer.WriteLittleEndian(ref offset, 0x0706050403020100); - Assert.True(write); - ValidateBytes(expected, buffer); - } - - [Fact] - public void WriteInt64BothEndianTest() - { - byte[] buffer = new byte[16]; - int offset = 0; - byte[] expected = [.. _bytes.Take(16)]; - - int readOffset = 0; - buffer.WriteBothEndian(ref offset, _bytes.ReadInt64BothEndian(ref readOffset)); - ValidateBytes(expected, buffer); - } - - [Fact] - public void WriteUInt64Test() - { - byte[] buffer = new byte[16]; - int offset = 0; - byte[] expected = [.. _bytes.Take(8)]; - bool write = buffer.Write(ref offset, (ulong)0x0706050403020100); - Assert.True(write); - ValidateBytes(expected, buffer); - } - - [Fact] - public void WriteUInt64BigEndianTest() - { - byte[] buffer = new byte[16]; - int offset = 0; - byte[] expected = [.. _bytes.Take(8)]; - bool write = buffer.WriteBigEndian(ref offset, (ulong)0x0001020304050607); - Assert.True(write); - ValidateBytes(expected, buffer); - } - - [Fact] - public void WriteUInt64LittleEndianTest() - { - byte[] buffer = new byte[16]; - int offset = 0; - byte[] expected = [.. _bytes.Take(8)]; - bool write = buffer.WriteLittleEndian(ref offset, (ulong)0x0706050403020100); - Assert.True(write); - ValidateBytes(expected, buffer); - } - - [Fact] - public void WriteUInt64BothEndianTest() - { - byte[] buffer = new byte[16]; - int offset = 0; - byte[] expected = [.. _bytes.Take(16)]; - - int readOffset = 0; - buffer.WriteBothEndian(ref offset, _bytes.ReadUInt64BothEndian(ref readOffset)); - ValidateBytes(expected, buffer); - } - - [Fact] - public void WriteDoubleTest() - { - byte[] buffer = new byte[16]; - int offset = 0; - byte[] expected = [.. _bytes.Take(8)]; - bool write = buffer.Write(ref offset, BitConverter.Int64BitsToDouble(0x0706050403020100)); - Assert.True(write); - ValidateBytes(expected, buffer); - } - - [Fact] - public void WriteDoubleBigEndianTest() - { - byte[] buffer = new byte[16]; - int offset = 0; - byte[] expected = [.. _bytes.Take(8)]; - bool write = buffer.WriteBigEndian(ref offset, BitConverter.Int64BitsToDouble(0x0001020304050607)); - Assert.True(write); - ValidateBytes(expected, buffer); - } - - [Fact] - public void WriteDoubleLittleEndianTest() - { - byte[] buffer = new byte[16]; - int offset = 0; - byte[] expected = [.. _bytes.Take(8)]; - bool write = buffer.WriteLittleEndian(ref offset, BitConverter.Int64BitsToDouble(0x0706050403020100)); - Assert.True(write); - ValidateBytes(expected, buffer); - } - - [Fact] - public void WriteGuidTest() - { - byte[] buffer = new byte[16]; - int offset = 0; - byte[] expected = [.. _bytes.Take(16)]; - bool write = buffer.Write(ref offset, new Guid(_bytes)); - Assert.True(write); - ValidateBytes(expected, buffer); - } - - [Fact] - public void WriteGuidBigEndianTest() - { - byte[] buffer = new byte[16]; - int offset = 0; - byte[] expected = [.. _guidBigEndianbytes.Take(16)]; - bool write = buffer.WriteBigEndian(ref offset, new Guid(_bytes)); - Assert.True(write); - ValidateBytes(expected, buffer); - } - - [Fact] - public void WriteGuidLittleEndianTest() - { - byte[] buffer = new byte[16]; - int offset = 0; - byte[] expected = [.. _bytes.Take(16)]; - bool write = buffer.WriteLittleEndian(ref offset, new Guid(_bytes)); - Assert.True(write); - ValidateBytes(expected, buffer); - } - - [Fact] - public void WriteInt128Test() - { - byte[] buffer = new byte[16]; - int offset = 0; - byte[] expected = [.. _bytes.Take(16)]; - bool write = buffer.Write(ref offset, (Int128)new BigInteger(_bytes)); - Assert.True(write); - ValidateBytes(expected, buffer); - } - - [Fact] - public void WriteInt128BigEndianTest() - { - byte[] buffer = new byte[16]; - int offset = 0; - byte[] expected = [.. _bytes.Take(16)]; - bool write = buffer.WriteBigEndian(ref offset, (Int128)new BigInteger(Enumerable.Reverse(_bytes).ToArray())); - Assert.True(write); - ValidateBytes(expected, buffer); - } - - [Fact] - public void WriteInt128LittleEndianTest() - { - byte[] buffer = new byte[16]; - int offset = 0; - byte[] expected = [.. _bytes.Take(16)]; - bool write = buffer.WriteLittleEndian(ref offset, (Int128)new BigInteger(_bytes)); - Assert.True(write); - ValidateBytes(expected, buffer); - } - - [Fact] - public void WriteUInt128Test() - { - byte[] buffer = new byte[16]; - int offset = 0; - byte[] expected = [.. _bytes.Take(16)]; - bool write = buffer.Write(ref offset, (UInt128)new BigInteger(_bytes)); - Assert.True(write); - ValidateBytes(expected, buffer); - } - - [Fact] - public void WriteUInt128BigEndianTest() - { - byte[] buffer = new byte[16]; - int offset = 0; - byte[] expected = [.. _bytes.Take(16)]; - bool write = buffer.WriteBigEndian(ref offset, (UInt128)new BigInteger(Enumerable.Reverse(_bytes).ToArray())); - Assert.True(write); - ValidateBytes(expected, buffer); - } - - [Fact] - public void WriteUInt128LittleEndianTest() - { - byte[] buffer = new byte[16]; - int offset = 0; - byte[] expected = [.. _bytes.Take(16)]; - bool write = buffer.WriteLittleEndian(ref offset, (UInt128)new BigInteger(_bytes)); - Assert.True(write); - ValidateBytes(expected, buffer); - } - - [Fact] - public void WriteDecimalTest() - { - byte[] buffer = new byte[16]; - int offset = 0; - byte[] expected = [.. _decimalBytes.Take(16)]; - bool write = buffer.Write(ref offset, 0.0123456789M); - Assert.True(write); - ValidateBytes(expected, buffer); - } - - [Fact] - public void WriteDecimalBigEndianTest() - { - byte[] buffer = new byte[16]; - int offset = 0; - byte[] expected = [.. _decimalBytes.Take(16).Reverse()]; - bool write = buffer.WriteBigEndian(ref offset, 0.0123456789M); - Assert.True(write); - ValidateBytes(expected, buffer); - } - - [Fact] - public void WriteDecimalLittleEndianTest() - { - byte[] buffer = new byte[16]; - int offset = 0; - byte[] expected = [.. _decimalBytes.Take(16)]; - bool write = buffer.WriteLittleEndian(ref offset, 0.0123456789M); - Assert.True(write); - ValidateBytes(expected, buffer); - } - [Fact] public void WriteNullTerminatedAnsiStringTest() { diff --git a/SabreTools.IO.Extensions.Test/StreamReaderExtensionsTests.cs b/SabreTools.IO.Extensions.Test/StreamReaderExtensionsTests.cs index a358c4f..8c9e52c 100644 --- a/SabreTools.IO.Extensions.Test/StreamReaderExtensionsTests.cs +++ b/SabreTools.IO.Extensions.Test/StreamReaderExtensionsTests.cs @@ -3,7 +3,6 @@ using System.IO; using System.Linq; using System.Numerics; using System.Text; -using SabreTools.Numerics; using Xunit; namespace SabreTools.IO.Extensions.Test @@ -19,672 +18,6 @@ namespace SabreTools.IO.Extensions.Test 0x08, 0x09, 0x0A, 0x0B, 0x0C, 0x0D, 0x0E, 0x0F, ]; - /// - /// Represents the decimal value 0.0123456789 - /// - private static readonly byte[] _decimalBytes = - [ - 0x15, 0xCD, 0x5B, 0x07, 0x00, 0x00, 0x00, 0x00, - 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x0A, 0x00, - ]; - - /// - /// Test pattern for big-endian GUID created from - /// - private static readonly byte[] _guidBigEndianbytes = - [ - 0x03, 0x02, 0x01, 0x00, 0x05, 0x04, 0x07, 0x06, - 0x08, 0x09, 0x0A, 0x0B, 0x0C, 0x0D, 0x0E, 0x0F, - ]; - - #region Exact Read - - [Fact] - public void ReadByteArrayTest() - { - byte[] arr = new byte[4]; - var stream = new MemoryStream(_bytes); - int read = stream.Read(arr, 0, 4); - Assert.Equal(4, read); - Assert.True(arr.SequenceEqual(_bytes.Take(4))); - } - - [Fact] - public void ReadByteValueTest() - { - var stream = new MemoryStream(_bytes); - byte read = stream.ReadByteValue(); - Assert.Equal(0x00, read); - } - - [Fact] - public void ReadBytesTest() - { - var stream = new MemoryStream(_bytes); - int length = 4; - byte[] read = stream.ReadBytes(length); - Assert.Equal(length, read.Length); - Assert.True(read.SequenceEqual(_bytes.Take(length))); - } - - [Fact] - public void ReadByteBothEndianTest() - { - var stream = new MemoryStream(_bytes); - BothUInt8 read = stream.ReadByteBothEndian(); - Assert.Equal(0x00, read.LittleEndian); - Assert.Equal(0x01, read.BigEndian); - } - - [Fact] - public void ReadSByteTest() - { - var stream = new MemoryStream(_bytes); - sbyte read = stream.ReadSByte(); - Assert.Equal(0x00, read); - } - - [Fact] - public void ReadSByteBothEndianTest() - { - var stream = new MemoryStream(_bytes); - BothInt8 read = stream.ReadSByteBothEndian(); - Assert.Equal(0x00, read.LittleEndian); - Assert.Equal(0x01, read.BigEndian); - } - - [Fact] - public void ReadCharTest() - { - var stream = new MemoryStream(_bytes); - char read = stream.ReadChar(); - Assert.Equal('\0', read); - } - - [Fact] - public void ReadInt16Test() - { - var stream = new MemoryStream(_bytes); - short read = stream.ReadInt16(); - Assert.Equal(0x0100, read); - } - - [Fact] - public void ReadInt16BigEndianTest() - { - var stream = new MemoryStream(_bytes); - short read = stream.ReadInt16BigEndian(); - Assert.Equal(0x0001, read); - } - - [Fact] - public void ReadInt16LittleEndianTest() - { - var stream = new MemoryStream(_bytes); - short read = stream.ReadInt16LittleEndian(); - Assert.Equal(0x0100, read); - } - - [Fact] - public void ReadInt16BothEndianTest() - { - var stream = new MemoryStream(_bytes); - BothInt16 read = stream.ReadInt16BothEndian(); - Assert.Equal(0x0100, read.LittleEndian); - Assert.Equal(0x0203, read.BigEndian); - } - - [Fact] - public void ReadUInt16Test() - { - var stream = new MemoryStream(_bytes); - ushort read = stream.ReadUInt16(); - Assert.Equal(0x0100, read); - } - - [Fact] - public void ReadUInt16BigEndianTest() - { - var stream = new MemoryStream(_bytes); - ushort read = stream.ReadUInt16BigEndian(); - Assert.Equal(0x0001, read); - } - - [Fact] - public void ReadUInt16LittleEndianTest() - { - var stream = new MemoryStream(_bytes); - ushort read = stream.ReadUInt16LittleEndian(); - Assert.Equal(0x0100, read); - } - - [Fact] - public void ReadUInt16BothEndianTest() - { - var stream = new MemoryStream(_bytes); - BothUInt16 read = stream.ReadUInt16BothEndian(); - Assert.Equal(0x0100, read.LittleEndian); - Assert.Equal(0x0203, read.BigEndian); - } - - [Fact] - public void ReadWORDTest() - { - var stream = new MemoryStream(_bytes); - ushort read = stream.ReadWORD(); - Assert.Equal(0x0100, read); - } - - [Fact] - public void ReadWORDBigEndianTest() - { - var stream = new MemoryStream(_bytes); - ushort read = stream.ReadWORDBigEndian(); - Assert.Equal(0x0001, read); - } - - [Fact] - public void ReadWORDLittleEndianTest() - { - var stream = new MemoryStream(_bytes); - ushort read = stream.ReadWORDLittleEndian(); - Assert.Equal(0x0100, read); - } - - [Fact] - public void ReadWORDBothEndianTest() - { - var stream = new MemoryStream(_bytes); - BothUInt16 read = stream.ReadWORDBothEndian(); - Assert.Equal(0x0100, read.LittleEndian); - Assert.Equal(0x0203, read.BigEndian); - } - - [Fact] - public void ReadHalfTest() - { - var stream = new MemoryStream(_bytes); - Half expected = BitConverter.Int16BitsToHalf(0x0100); - Half read = stream.ReadHalf(); - Assert.Equal(expected, read); - } - - [Fact] - public void ReadHalfBigEndianTest() - { - var stream = new MemoryStream(_bytes); - Half expected = BitConverter.Int16BitsToHalf(0x0001); - Half read = stream.ReadHalfBigEndian(); - Assert.Equal(expected, read); - } - - [Fact] - public void ReadHalfLittleEndianTest() - { - var stream = new MemoryStream(_bytes); - Half expected = BitConverter.Int16BitsToHalf(0x0100); - Half read = stream.ReadHalfLittleEndian(); - Assert.Equal(expected, read); - } - - [Fact] - public void ReadInt24Test() - { - var stream = new MemoryStream(_bytes); - Int24 read = stream.ReadInt24(); - Assert.Equal(0x020100, (int)read); - } - - [Fact] - public void ReadInt24BigEndianTest() - { - var stream = new MemoryStream(_bytes); - Int24 read = stream.ReadInt24BigEndian(); - Assert.Equal(0x000102, (int)read); - } - - [Fact] - public void ReadInt24LittleEndianTest() - { - var stream = new MemoryStream(_bytes); - Int24 read = stream.ReadInt24LittleEndian(); - Assert.Equal(0x020100, (int)read); - } - - [Fact] - public void ReadUInt24Test() - { - var stream = new MemoryStream(_bytes); - UInt24 read = stream.ReadUInt24(); - Assert.Equal((uint)0x020100, (uint)read); - } - - [Fact] - public void ReadUInt24BigEndianTest() - { - var stream = new MemoryStream(_bytes); - UInt24 read = stream.ReadUInt24BigEndian(); - Assert.Equal((uint)0x000102, (uint)read); - } - - [Fact] - public void ReadUInt24LittleEndianTest() - { - var stream = new MemoryStream(_bytes); - UInt24 read = stream.ReadUInt24LittleEndian(); - Assert.Equal((uint)0x020100, (uint)read); - } - - [Fact] - public void ReadInt32Test() - { - var stream = new MemoryStream(_bytes); - int read = stream.ReadInt32(); - Assert.Equal(0x03020100, read); - } - - [Fact] - public void ReadInt32BigEndianTest() - { - var stream = new MemoryStream(_bytes); - int read = stream.ReadInt32BigEndian(); - Assert.Equal(0x00010203, read); - } - - [Fact] - public void ReadInt32LittleEndianTest() - { - var stream = new MemoryStream(_bytes); - int read = stream.ReadInt32LittleEndian(); - Assert.Equal(0x03020100, read); - } - - [Fact] - public void ReadInt32BothEndianTest() - { - var stream = new MemoryStream(_bytes); - BothInt32 read = stream.ReadInt32BothEndian(); - Assert.Equal(0x03020100, read.LittleEndian); - Assert.Equal(0x04050607, read.BigEndian); - } - - [Fact] - public void ReadUInt32Test() - { - var stream = new MemoryStream(_bytes); - uint read = stream.ReadUInt32(); - Assert.Equal((uint)0x03020100, read); - } - - [Fact] - public void ReadUInt32BigEndianTest() - { - var stream = new MemoryStream(_bytes); - uint read = stream.ReadUInt32BigEndian(); - Assert.Equal((uint)0x00010203, read); - } - - [Fact] - public void ReadUInt32LittleEndianTest() - { - var stream = new MemoryStream(_bytes); - uint read = stream.ReadUInt32LittleEndian(); - Assert.Equal((uint)0x03020100, read); - } - - [Fact] - public void ReadUInt32BothEndianTest() - { - var stream = new MemoryStream(_bytes); - BothUInt32 read = stream.ReadUInt32BothEndian(); - Assert.Equal((uint)0x03020100, read.LittleEndian); - Assert.Equal((uint)0x04050607, read.BigEndian); - } - - [Fact] - public void ReadDWORDTest() - { - var stream = new MemoryStream(_bytes); - uint read = stream.ReadDWORD(); - Assert.Equal((uint)0x03020100, read); - } - - [Fact] - public void ReadDWORDBigEndianTest() - { - var stream = new MemoryStream(_bytes); - uint read = stream.ReadDWORDBigEndian(); - Assert.Equal((uint)0x00010203, read); - } - - [Fact] - public void ReadDWORDLittleEndianTest() - { - var stream = new MemoryStream(_bytes); - uint read = stream.ReadDWORDLittleEndian(); - Assert.Equal((uint)0x03020100, read); - } - - [Fact] - public void ReadDWORDBothEndianTest() - { - var stream = new MemoryStream(_bytes); - BothUInt32 read = stream.ReadDWORDBothEndian(); - Assert.Equal((uint)0x03020100, read.LittleEndian); - Assert.Equal((uint)0x04050607, read.BigEndian); - } - - [Fact] - public void ReadSingleTest() - { - var stream = new MemoryStream(_bytes); - float expected = BitConverter.Int32BitsToSingle(0x03020100); - float read = stream.ReadSingle(); - Assert.Equal(expected, read); - } - - [Fact] - public void ReadSingleBigEndianTest() - { - var stream = new MemoryStream(_bytes); - float expected = BitConverter.Int32BitsToSingle(0x00010203); - float read = stream.ReadSingleBigEndian(); - Assert.Equal(expected, read); - } - - [Fact] - public void ReadSingleLittleEndianTest() - { - var stream = new MemoryStream(_bytes); - float expected = BitConverter.Int32BitsToSingle(0x03020100); - float read = stream.ReadSingleLittleEndian(); - Assert.Equal(expected, read); - } - - [Fact] - public void ReadInt48Test() - { - MemoryStream? stream = new MemoryStream(_bytes); - Int48 read = stream.ReadInt48(); - Assert.Equal(0x050403020100, (long)read); - } - - [Fact] - public void ReadInt48BigEndianTest() - { - var stream = new MemoryStream(_bytes); - Int48 read = stream.ReadInt48BigEndian(); - Assert.Equal(0x000102030405, (long)read); - } - - [Fact] - public void ReadInt48LittleEndianTest() - { - var stream = new MemoryStream(_bytes); - Int48 read = stream.ReadInt48LittleEndian(); - Assert.Equal(0x050403020100, (long)read); - } - - [Fact] - public void ReadUInt48Test() - { - var stream = new MemoryStream(_bytes); - UInt48 read = stream.ReadUInt48(); - Assert.Equal((ulong)0x050403020100, (ulong)read); - } - - [Fact] - public void ReadUInt48BigEndianTest() - { - var stream = new MemoryStream(_bytes); - UInt48 read = stream.ReadUInt48BigEndian(); - Assert.Equal((ulong)0x000102030405, (ulong)read); - } - - [Fact] - public void ReadUInt48LittleEndianTest() - { - var stream = new MemoryStream(_bytes); - UInt48 read = stream.ReadUInt48LittleEndian(); - Assert.Equal((ulong)0x050403020100, (ulong)read); - } - - [Fact] - public void ReadInt64Test() - { - var stream = new MemoryStream(_bytes); - long read = stream.ReadInt64(); - Assert.Equal(0x0706050403020100, read); - } - - [Fact] - public void ReadInt64BigEndianTest() - { - var stream = new MemoryStream(_bytes); - long read = stream.ReadInt64BigEndian(); - Assert.Equal(0x0001020304050607, read); - } - - [Fact] - public void ReadInt64LittleEndianTest() - { - var stream = new MemoryStream(_bytes); - long read = stream.ReadInt64LittleEndian(); - Assert.Equal(0x0706050403020100, read); - } - - [Fact] - public void ReadInt64BothEndianTest() - { - var stream = new MemoryStream(_bytes); - BothInt64 read = stream.ReadInt64BothEndian(); - Assert.Equal(0x0706050403020100, read.LittleEndian); - Assert.Equal(0x08090A0B0C0D0E0F, read.BigEndian); - } - - [Fact] - public void ReadUInt64Test() - { - var stream = new MemoryStream(_bytes); - ulong read = stream.ReadUInt64(); - Assert.Equal((ulong)0x0706050403020100, read); - } - - [Fact] - public void ReadUInt64BigEndianTest() - { - var stream = new MemoryStream(_bytes); - ulong read = stream.ReadUInt64BigEndian(); - Assert.Equal((ulong)0x0001020304050607, read); - } - - [Fact] - public void ReadUInt64LittleEndianTest() - { - var stream = new MemoryStream(_bytes); - ulong read = stream.ReadUInt64LittleEndian(); - Assert.Equal((ulong)0x0706050403020100, read); - } - - [Fact] - public void ReadUInt64BothEndianTest() - { - var stream = new MemoryStream(_bytes); - BothUInt64 read = stream.ReadUInt64BothEndian(); - Assert.Equal((ulong)0x0706050403020100, read.LittleEndian); - Assert.Equal((ulong)0x08090A0B0C0D0E0F, read.BigEndian); - } - - [Fact] - public void ReadQWORDTest() - { - var stream = new MemoryStream(_bytes); - ulong read = stream.ReadQWORD(); - Assert.Equal((ulong)0x0706050403020100, read); - } - - [Fact] - public void ReadQWORDBigEndianTest() - { - var stream = new MemoryStream(_bytes); - ulong read = stream.ReadQWORDBigEndian(); - Assert.Equal((ulong)0x0001020304050607, read); - } - - [Fact] - public void ReadQWORDLittleEndianTest() - { - var stream = new MemoryStream(_bytes); - ulong read = stream.ReadQWORDLittleEndian(); - Assert.Equal((ulong)0x0706050403020100, read); - } - - [Fact] - public void ReadQWORDBothEndianTest() - { - var stream = new MemoryStream(_bytes); - BothUInt64 read = stream.ReadQWORDBothEndian(); - Assert.Equal((ulong)0x0706050403020100, read.LittleEndian); - Assert.Equal((ulong)0x08090A0B0C0D0E0F, read.BigEndian); - } - - [Fact] - public void ReadDoubleTest() - { - var stream = new MemoryStream(_bytes); - double expected = BitConverter.Int64BitsToDouble(0x0706050403020100); - double read = stream.ReadDouble(); - Assert.Equal(expected, read); - } - - [Fact] - public void ReadDoubleBigEndianTest() - { - var stream = new MemoryStream(_bytes); - double expected = BitConverter.Int64BitsToDouble(0x0001020304050607); - double read = stream.ReadDoubleBigEndian(); - Assert.Equal(expected, read); - } - - [Fact] - public void ReadDoubleLittleEndianTest() - { - var stream = new MemoryStream(_bytes); - double expected = BitConverter.Int64BitsToDouble(0x0706050403020100); - double read = stream.ReadDoubleLittleEndian(); - Assert.Equal(expected, read); - } - - [Fact] - public void ReadGuidTest() - { - var stream = new MemoryStream(_bytes); - var expected = new Guid(_bytes); - Guid read = stream.ReadGuid(); - Assert.Equal(expected, read); - } - - [Fact] - public void ReadGuidBigEndianTest() - { - var stream = new MemoryStream(_bytes); - var expected = new Guid(_guidBigEndianbytes); - Guid read = stream.ReadGuidBigEndian(); - Assert.Equal(expected, read); - } - - [Fact] - public void ReadGuidLittleEndianTest() - { - var stream = new MemoryStream(_bytes); - var expected = new Guid(_bytes); - Guid read = stream.ReadGuidLittleEndian(); - Assert.Equal(expected, read); - } - - [Fact] - public void ReadInt128Test() - { - var stream = new MemoryStream(_bytes); - var expected = (Int128)new BigInteger(_bytes); - Int128 read = stream.ReadInt128(); - Assert.Equal(expected, read); - } - - [Fact] - public void ReadInt128BigEndianTest() - { - var stream = new MemoryStream(_bytes); - var reversed = Enumerable.Reverse(_bytes).ToArray(); - var expected = (Int128)new BigInteger(reversed); - Int128 read = stream.ReadInt128BigEndian(); - Assert.Equal(expected, read); - } - - [Fact] - public void ReadInt128LittleEndianTest() - { - var stream = new MemoryStream(_bytes); - var expected = (Int128)new BigInteger(_bytes); - Int128 read = stream.ReadInt128LittleEndian(); - Assert.Equal(expected, read); - } - - [Fact] - public void ReadUInt128Test() - { - var stream = new MemoryStream(_bytes); - var expected = (UInt128)new BigInteger(_bytes); - UInt128 read = stream.ReadUInt128(); - Assert.Equal(expected, read); - } - - [Fact] - public void ReadUInt128BigEndianTest() - { - var stream = new MemoryStream(_bytes); - var reversed = Enumerable.Reverse(_bytes).ToArray(); - var expected = (UInt128)new BigInteger(reversed); - UInt128 read = stream.ReadUInt128BigEndian(); - Assert.Equal(expected, read); - } - - [Fact] - public void ReadUInt128LittleEndianTest() - { - var stream = new MemoryStream(_bytes); - var expected = (UInt128)new BigInteger(_bytes); - UInt128 read = stream.ReadUInt128LittleEndian(); - Assert.Equal(expected, read); - } - - [Fact] - public void ReadDecimalTest() - { - var stream = new MemoryStream(_decimalBytes); - decimal expected = 0.0123456789M; - decimal read = stream.ReadDecimal(); - Assert.Equal(expected, read); - } - - [Fact] - public void ReadDecimalBigEndianTest() - { - var stream = new MemoryStream([.. Enumerable.Reverse(_decimalBytes)]); - decimal expected = 0.0123456789M; - decimal read = stream.ReadDecimalBigEndian(); - Assert.Equal(expected, read); - } - - [Fact] - public void ReadDecimalLittleEndianTest() - { - var stream = new MemoryStream(_decimalBytes); - decimal expected = 0.0123456789M; - decimal read = stream.ReadDecimalLittleEndian(); - Assert.Equal(expected, read); - } - [Fact] public void ReadNullTerminatedStringTest() { @@ -961,1413 +294,5 @@ namespace SabreTools.IO.Extensions.Test Assert.Equal(expected2.FieldA, read2.FieldA); Assert.Equal(expected2.FieldB, read2.FieldB); } - - #endregion - - #region Peek Read - - [Fact] - public void PeekByteValueTest() - { - var stream = new MemoryStream(_bytes); - byte read = stream.PeekByteValue(); - Assert.Equal(0x00, read); - Assert.Equal(0, stream.Position); - } - - [Fact] - public void PeekByteBothEndianTest() - { - var stream = new MemoryStream(_bytes); - BothUInt8 read = stream.PeekByteBothEndian(); - Assert.Equal(0x00, read.LittleEndian); - Assert.Equal(0x01, read.BigEndian); - Assert.Equal(0, stream.Position); - } - - [Fact] - public void PeekBytesTest() - { - var stream = new MemoryStream(_bytes); - int length = 4; - byte[] read = stream.PeekBytes(length); - Assert.Equal(length, read.Length); - Assert.True(read.SequenceEqual(_bytes.Take(length))); - Assert.Equal(0, stream.Position); - } - - [Fact] - public void PeekSByteTest() - { - var stream = new MemoryStream(_bytes); - sbyte read = stream.PeekSByte(); - Assert.Equal(0x00, read); - Assert.Equal(0, stream.Position); - } - - [Fact] - public void PeekSByteBothEndianTest() - { - var stream = new MemoryStream(_bytes); - BothInt8 read = stream.PeekSByteBothEndian(); - Assert.Equal(0x00, read.LittleEndian); - Assert.Equal(0x01, read.BigEndian); - Assert.Equal(0, stream.Position); - } - - [Fact] - public void PeekCharTest() - { - var stream = new MemoryStream(_bytes); - char read = stream.PeekChar(); - Assert.Equal('\0', read); - Assert.Equal(0, stream.Position); - } - - [Fact] - public void PeekInt16Test() - { - var stream = new MemoryStream(_bytes); - short read = stream.PeekInt16(); - Assert.Equal(0x0100, read); - Assert.Equal(0, stream.Position); - } - - [Fact] - public void PeekInt16BigEndianTest() - { - var stream = new MemoryStream(_bytes); - short read = stream.PeekInt16BigEndian(); - Assert.Equal(0x0001, read); - Assert.Equal(0, stream.Position); - } - - [Fact] - public void PeekInt16LittleEndianTest() - { - var stream = new MemoryStream(_bytes); - short read = stream.PeekInt16LittleEndian(); - Assert.Equal(0x0100, read); - Assert.Equal(0, stream.Position); - } - - [Fact] - public void PeekInt16BothEndianTest() - { - var stream = new MemoryStream(_bytes); - BothInt16 read = stream.PeekInt16BothEndian(); - Assert.Equal(0x0100, read.LittleEndian); - Assert.Equal(0x0203, read.BigEndian); - Assert.Equal(0, stream.Position); - } - - [Fact] - public void PeekUInt16Test() - { - var stream = new MemoryStream(_bytes); - ushort read = stream.PeekUInt16(); - Assert.Equal(0x0100, read); - Assert.Equal(0, stream.Position); - } - - [Fact] - public void PeekUInt16BigEndianTest() - { - var stream = new MemoryStream(_bytes); - ushort read = stream.PeekUInt16BigEndian(); - Assert.Equal(0x0001, read); - Assert.Equal(0, stream.Position); - } - - [Fact] - public void PeekUInt16LittleEndianTest() - { - var stream = new MemoryStream(_bytes); - ushort read = stream.PeekUInt16LittleEndian(); - Assert.Equal(0x0100, read); - Assert.Equal(0, stream.Position); - } - - [Fact] - public void PeekUInt16BothEndianTest() - { - var stream = new MemoryStream(_bytes); - BothUInt16 read = stream.PeekUInt16BothEndian(); - Assert.Equal(0x0100, read.LittleEndian); - Assert.Equal(0x0203, read.BigEndian); - Assert.Equal(0, stream.Position); - } - - [Fact] - public void PeekWORDTest() - { - var stream = new MemoryStream(_bytes); - ushort read = stream.PeekWORD(); - Assert.Equal(0x0100, read); - Assert.Equal(0, stream.Position); - } - - [Fact] - public void PeekWORDBigEndianTest() - { - var stream = new MemoryStream(_bytes); - ushort read = stream.PeekWORDBigEndian(); - Assert.Equal(0x0001, read); - Assert.Equal(0, stream.Position); - } - - [Fact] - public void PeekWORDLittleEndianTest() - { - var stream = new MemoryStream(_bytes); - ushort read = stream.PeekWORDLittleEndian(); - Assert.Equal(0x0100, read); - Assert.Equal(0, stream.Position); - } - - [Fact] - public void PeekWORDBothEndianTest() - { - var stream = new MemoryStream(_bytes); - BothUInt16 read = stream.PeekWORDBothEndian(); - Assert.Equal(0x0100, read.LittleEndian); - Assert.Equal(0x0203, read.BigEndian); - Assert.Equal(0, stream.Position); - } - - [Fact] - public void PeekHalfTest() - { - var stream = new MemoryStream(_bytes); - Half expected = BitConverter.Int16BitsToHalf(0x0100); - Half read = stream.PeekHalf(); - Assert.Equal(expected, read); - Assert.Equal(0, stream.Position); - } - - [Fact] - public void PeekHalfBigEndianTest() - { - var stream = new MemoryStream(_bytes); - Half expected = BitConverter.Int16BitsToHalf(0x0001); - Half read = stream.PeekHalfBigEndian(); - Assert.Equal(expected, read); - Assert.Equal(0, stream.Position); - } - - [Fact] - public void PeekHalfLittleEndianTest() - { - var stream = new MemoryStream(_bytes); - Half expected = BitConverter.Int16BitsToHalf(0x0100); - Half read = stream.PeekHalfLittleEndian(); - Assert.Equal(expected, read); - Assert.Equal(0, stream.Position); - } - - [Fact] - public void PeekInt24Test() - { - var stream = new MemoryStream(_bytes); - Int24 read = stream.PeekInt24(); - Assert.Equal(0x020100, (int)read); - Assert.Equal(0, stream.Position); - } - - [Fact] - public void PeekInt24BigEndianTest() - { - var stream = new MemoryStream(_bytes); - Int24 read = stream.PeekInt24BigEndian(); - Assert.Equal(0x000102, (int)read); - Assert.Equal(0, stream.Position); - } - - [Fact] - public void PeekInt24LittleEndianTest() - { - var stream = new MemoryStream(_bytes); - Int24 read = stream.PeekInt24LittleEndian(); - Assert.Equal(0x020100, (int)read); - Assert.Equal(0, stream.Position); - } - - [Fact] - public void PeekUInt24Test() - { - var stream = new MemoryStream(_bytes); - UInt24 read = stream.PeekUInt24(); - Assert.Equal((uint)0x020100, (uint)read); - Assert.Equal(0, stream.Position); - } - - [Fact] - public void PeekUInt24BigEndianTest() - { - var stream = new MemoryStream(_bytes); - UInt24 read = stream.PeekUInt24BigEndian(); - Assert.Equal((uint)0x000102, (uint)read); - Assert.Equal(0, stream.Position); - } - - [Fact] - public void PeekUInt24LittleEndianTest() - { - var stream = new MemoryStream(_bytes); - UInt24 read = stream.PeekUInt24LittleEndian(); - Assert.Equal((uint)0x020100, (uint)read); - Assert.Equal(0, stream.Position); - } - - [Fact] - public void PeekInt32Test() - { - var stream = new MemoryStream(_bytes); - int read = stream.PeekInt32(); - Assert.Equal(0x03020100, read); - Assert.Equal(0, stream.Position); - } - - [Fact] - public void PeekInt32BigEndianTest() - { - var stream = new MemoryStream(_bytes); - int read = stream.PeekInt32BigEndian(); - Assert.Equal(0x00010203, read); - Assert.Equal(0, stream.Position); - } - - [Fact] - public void PeekInt32LittleEndianTest() - { - var stream = new MemoryStream(_bytes); - int read = stream.PeekInt32LittleEndian(); - Assert.Equal(0x03020100, read); - Assert.Equal(0, stream.Position); - } - - [Fact] - public void PeekInt32BothEndianTest() - { - var stream = new MemoryStream(_bytes); - BothInt32 read = stream.PeekInt32BothEndian(); - Assert.Equal(0x03020100, read.LittleEndian); - Assert.Equal(0x04050607, read.BigEndian); - Assert.Equal(0, stream.Position); - } - - [Fact] - public void PeekUInt32Test() - { - var stream = new MemoryStream(_bytes); - uint read = stream.PeekUInt32(); - Assert.Equal((uint)0x03020100, read); - Assert.Equal(0, stream.Position); - } - - [Fact] - public void PeekUInt32BigEndianTest() - { - var stream = new MemoryStream(_bytes); - uint read = stream.PeekUInt32BigEndian(); - Assert.Equal((uint)0x00010203, read); - Assert.Equal(0, stream.Position); - } - - [Fact] - public void PeekUInt32LittleEndianTest() - { - var stream = new MemoryStream(_bytes); - uint read = stream.PeekUInt32LittleEndian(); - Assert.Equal((uint)0x03020100, read); - Assert.Equal(0, stream.Position); - } - - [Fact] - public void PeekUInt32BothEndianTest() - { - var stream = new MemoryStream(_bytes); - BothUInt32 read = stream.PeekUInt32BothEndian(); - Assert.Equal((uint)0x03020100, read.LittleEndian); - Assert.Equal((uint)0x04050607, read.BigEndian); - Assert.Equal(0, stream.Position); - } - - [Fact] - public void PeekDWORDTest() - { - var stream = new MemoryStream(_bytes); - uint read = stream.PeekDWORD(); - Assert.Equal((uint)0x03020100, read); - Assert.Equal(0, stream.Position); - } - - [Fact] - public void PeekDWORDBigEndianTest() - { - var stream = new MemoryStream(_bytes); - uint read = stream.PeekDWORDBigEndian(); - Assert.Equal((uint)0x00010203, read); - Assert.Equal(0, stream.Position); - } - - [Fact] - public void PeekDWORDLittleEndianTest() - { - var stream = new MemoryStream(_bytes); - uint read = stream.PeekDWORDLittleEndian(); - Assert.Equal((uint)0x03020100, read); - Assert.Equal(0, stream.Position); - } - - [Fact] - public void PeekDWORDBothEndianTest() - { - var stream = new MemoryStream(_bytes); - BothUInt32 read = stream.PeekDWORDBothEndian(); - Assert.Equal((uint)0x03020100, read.LittleEndian); - Assert.Equal((uint)0x04050607, read.BigEndian); - Assert.Equal(0, stream.Position); - } - - [Fact] - public void PeekSingleTest() - { - var stream = new MemoryStream(_bytes); - float expected = BitConverter.Int32BitsToSingle(0x03020100); - float read = stream.PeekSingle(); - Assert.Equal(expected, read); - Assert.Equal(0, stream.Position); - } - - [Fact] - public void PeekSingleBigEndianTest() - { - var stream = new MemoryStream(_bytes); - float expected = BitConverter.Int32BitsToSingle(0x00010203); - float read = stream.PeekSingleBigEndian(); - Assert.Equal(expected, read); - Assert.Equal(0, stream.Position); - } - - [Fact] - public void PeekSingleLittleEndianTest() - { - var stream = new MemoryStream(_bytes); - float expected = BitConverter.Int32BitsToSingle(0x03020100); - float read = stream.PeekSingleLittleEndian(); - Assert.Equal(expected, read); - Assert.Equal(0, stream.Position); - } - - [Fact] - public void PeekInt48Test() - { - var stream = new MemoryStream(_bytes); - Int48 read = stream.PeekInt48(); - Assert.Equal(0x050403020100, (long)read); - Assert.Equal(0, stream.Position); - } - - [Fact] - public void PeekInt48BigEndianTest() - { - var stream = new MemoryStream(_bytes); - Int48 read = stream.PeekInt48BigEndian(); - Assert.Equal(0x000102030405, (long)read); - Assert.Equal(0, stream.Position); - } - - [Fact] - public void PeekInt48LittleEndianTest() - { - var stream = new MemoryStream(_bytes); - Int48 read = stream.PeekInt48LittleEndian(); - Assert.Equal(0x050403020100, (long)read); - Assert.Equal(0, stream.Position); - } - - [Fact] - public void PeekUInt48Test() - { - var stream = new MemoryStream(_bytes); - UInt48 read = stream.PeekUInt48(); - Assert.Equal((ulong)0x050403020100, (ulong)read); - Assert.Equal(0, stream.Position); - } - - [Fact] - public void PeekUInt48BigEndianTest() - { - var stream = new MemoryStream(_bytes); - UInt48 read = stream.PeekUInt48BigEndian(); - Assert.Equal((ulong)0x000102030405, (ulong)read); - Assert.Equal(0, stream.Position); - } - - [Fact] - public void PeekUInt48LittleEndianTest() - { - var stream = new MemoryStream(_bytes); - UInt48 read = stream.PeekUInt48LittleEndian(); - Assert.Equal((ulong)0x050403020100, (ulong)read); - Assert.Equal(0, stream.Position); - } - - [Fact] - public void PeekInt64Test() - { - var stream = new MemoryStream(_bytes); - long read = stream.PeekInt64(); - Assert.Equal(0x0706050403020100, read); - Assert.Equal(0, stream.Position); - } - - [Fact] - public void PeekInt64BigEndianTest() - { - var stream = new MemoryStream(_bytes); - long read = stream.PeekInt64BigEndian(); - Assert.Equal(0x0001020304050607, read); - Assert.Equal(0, stream.Position); - } - - [Fact] - public void PeekInt64LittleEndianTest() - { - var stream = new MemoryStream(_bytes); - long read = stream.PeekInt64LittleEndian(); - Assert.Equal(0x0706050403020100, read); - Assert.Equal(0, stream.Position); - } - - [Fact] - public void PeekInt64BothEndianTest() - { - var stream = new MemoryStream(_bytes); - BothInt64 read = stream.PeekInt64BothEndian(); - Assert.Equal(0x0706050403020100, read.LittleEndian); - Assert.Equal(0x08090A0B0C0D0E0F, read.BigEndian); - Assert.Equal(0, stream.Position); - } - - [Fact] - public void PeekUInt64Test() - { - var stream = new MemoryStream(_bytes); - ulong read = stream.PeekUInt64(); - Assert.Equal((ulong)0x0706050403020100, read); - Assert.Equal(0, stream.Position); - } - - [Fact] - public void PeekUInt64BigEndianTest() - { - var stream = new MemoryStream(_bytes); - ulong read = stream.PeekUInt64BigEndian(); - Assert.Equal((ulong)0x0001020304050607, read); - Assert.Equal(0, stream.Position); - } - - [Fact] - public void PeekUInt64LittleEndianTest() - { - var stream = new MemoryStream(_bytes); - ulong read = stream.PeekUInt64LittleEndian(); - Assert.Equal((ulong)0x0706050403020100, read); - Assert.Equal(0, stream.Position); - } - - [Fact] - public void PeekUInt64BothEndianTest() - { - var stream = new MemoryStream(_bytes); - BothUInt64 read = stream.PeekUInt64BothEndian(); - Assert.Equal((ulong)0x0706050403020100, read.LittleEndian); - Assert.Equal((ulong)0x08090A0B0C0D0E0F, read.BigEndian); - Assert.Equal(0, stream.Position); - } - - [Fact] - public void PeekQWORDTest() - { - var stream = new MemoryStream(_bytes); - ulong read = stream.PeekQWORD(); - Assert.Equal((ulong)0x0706050403020100, read); - Assert.Equal(0, stream.Position); - } - - [Fact] - public void PeekQWORDBigEndianTest() - { - var stream = new MemoryStream(_bytes); - ulong read = stream.PeekQWORDBigEndian(); - Assert.Equal((ulong)0x0001020304050607, read); - Assert.Equal(0, stream.Position); - } - - [Fact] - public void PeekQWORDLittleEndianTest() - { - var stream = new MemoryStream(_bytes); - ulong read = stream.PeekQWORDLittleEndian(); - Assert.Equal((ulong)0x0706050403020100, read); - Assert.Equal(0, stream.Position); - } - - [Fact] - public void PeekQWORDBothEndianTest() - { - var stream = new MemoryStream(_bytes); - BothUInt64 read = stream.PeekQWORDBothEndian(); - Assert.Equal((ulong)0x0706050403020100, read.LittleEndian); - Assert.Equal((ulong)0x08090A0B0C0D0E0F, read.BigEndian); - Assert.Equal(0, stream.Position); - } - - [Fact] - public void PeekDoubleTest() - { - var stream = new MemoryStream(_bytes); - double expected = BitConverter.Int64BitsToDouble(0x0706050403020100); - double read = stream.PeekDouble(); - Assert.Equal(expected, read); - Assert.Equal(0, stream.Position); - } - - [Fact] - public void PeekDoubleBigEndianTest() - { - var stream = new MemoryStream(_bytes); - double expected = BitConverter.Int64BitsToDouble(0x0001020304050607); - double read = stream.PeekDoubleBigEndian(); - Assert.Equal(expected, read); - Assert.Equal(0, stream.Position); - } - - [Fact] - public void PeekDoubleLittleEndianTest() - { - var stream = new MemoryStream(_bytes); - double expected = BitConverter.Int64BitsToDouble(0x0706050403020100); - double read = stream.PeekDoubleLittleEndian(); - Assert.Equal(expected, read); - Assert.Equal(0, stream.Position); - } - - [Fact] - public void PeekGuidTest() - { - var stream = new MemoryStream(_bytes); - var expected = new Guid(_bytes); - Guid read = stream.PeekGuid(); - Assert.Equal(expected, read); - Assert.Equal(0, stream.Position); - } - - [Fact] - public void PeekGuidBigEndianTest() - { - var stream = new MemoryStream(_bytes); - var expected = new Guid(_guidBigEndianbytes); - Guid read = stream.PeekGuidBigEndian(); - Assert.Equal(expected, read); - Assert.Equal(0, stream.Position); - } - - [Fact] - public void PeekGuidLittleEndianTest() - { - var stream = new MemoryStream(_bytes); - var expected = new Guid(_bytes); - Guid read = stream.PeekGuidLittleEndian(); - Assert.Equal(expected, read); - Assert.Equal(0, stream.Position); - } - - [Fact] - public void PeekInt128Test() - { - var stream = new MemoryStream(_bytes); - var expected = (Int128)new BigInteger(_bytes); - Int128 read = stream.PeekInt128(); - Assert.Equal(expected, read); - Assert.Equal(0, stream.Position); - } - - [Fact] - public void PeekInt128BigEndianTest() - { - var stream = new MemoryStream(_bytes); - var reversed = Enumerable.Reverse(_bytes).ToArray(); - var expected = (Int128)new BigInteger(reversed); - Int128 read = stream.PeekInt128BigEndian(); - Assert.Equal(expected, read); - Assert.Equal(0, stream.Position); - } - - [Fact] - public void PeekInt128LittleEndianTest() - { - var stream = new MemoryStream(_bytes); - var expected = (Int128)new BigInteger(_bytes); - Int128 read = stream.PeekInt128LittleEndian(); - Assert.Equal(expected, read); - Assert.Equal(0, stream.Position); - } - - [Fact] - public void PeekUInt128Test() - { - var stream = new MemoryStream(_bytes); - var expected = (UInt128)new BigInteger(_bytes); - UInt128 read = stream.PeekUInt128(); - Assert.Equal(expected, read); - Assert.Equal(0, stream.Position); - } - - [Fact] - public void PeekUInt128BigEndianTest() - { - var stream = new MemoryStream(_bytes); - var reversed = Enumerable.Reverse(_bytes).ToArray(); - var expected = (UInt128)new BigInteger(reversed); - UInt128 read = stream.PeekUInt128BigEndian(); - Assert.Equal(expected, read); - Assert.Equal(0, stream.Position); - } - - [Fact] - public void PeekUInt128LittleEndianTest() - { - var stream = new MemoryStream(_bytes); - var expected = (UInt128)new BigInteger(_bytes); - UInt128 read = stream.PeekUInt128LittleEndian(); - Assert.Equal(expected, read); - Assert.Equal(0, stream.Position); - } - - [Fact] - public void PeekDecimalTest() - { - var stream = new MemoryStream(_decimalBytes); - decimal expected = 0.0123456789M; - decimal read = stream.PeekDecimal(); - Assert.Equal(expected, read); - Assert.Equal(0, stream.Position); - } - - [Fact] - public void PeekDecimalBigEndianTest() - { - var stream = new MemoryStream([.. Enumerable.Reverse(_decimalBytes)]); - decimal expected = 0.0123456789M; - decimal read = stream.PeekDecimalBigEndian(); - Assert.Equal(expected, read); - Assert.Equal(0, stream.Position); - } - - [Fact] - public void PeekDecimalLittleEndianTest() - { - var stream = new MemoryStream(_decimalBytes); - decimal expected = 0.0123456789M; - decimal read = stream.PeekDecimalLittleEndian(); - Assert.Equal(expected, read); - Assert.Equal(0, stream.Position); - } - - #endregion - - #region Try Read - - [Fact] - public void TryReadByteValueTest() - { - var stream = new MemoryStream([]); - bool actual = stream.TryReadByteValue(out byte read); - Assert.False(actual); - Assert.Equal(default, read); - } - - [Fact] - public void TryReadBytesTest() - { - var stream = new MemoryStream([]); - int length = 4; - bool actual = stream.TryReadBytes(length, out byte[] read); - Assert.False(actual); - Assert.Empty(read); - } - - [Fact] - public void TryReadByteBothEndianTest() - { - var stream = new MemoryStream([]); - bool actual = stream.TryReadByteBothEndian(out BothUInt8 read); - Assert.False(actual); - Assert.Equal(default, read.LittleEndian); - Assert.Equal(default, read.BigEndian); - } - - [Fact] - public void TryReadSByteTest() - { - var stream = new MemoryStream([]); - bool actual = stream.TryReadSByte(out sbyte read); - Assert.False(actual); - Assert.Equal(default, read); - } - - [Fact] - public void TryReadSByteBothEndianTest() - { - var stream = new MemoryStream([]); - bool actual = stream.TryReadSByteBothEndian(out BothInt8 read); - Assert.False(actual); - Assert.Equal(default, read.LittleEndian); - Assert.Equal(default, read.BigEndian); - } - - [Fact] - public void TryReadCharTest() - { - var stream = new MemoryStream([]); - bool actual = stream.TryReadChar(out char read); - Assert.False(actual); - Assert.Equal(default, read); - } - - [Fact] - public void TryReadInt16Test() - { - var stream = new MemoryStream([]); - bool actual = stream.TryReadInt16(out short read); - Assert.False(actual); - Assert.Equal(default, read); - } - - [Fact] - public void TryReadInt16BigEndianTest() - { - var stream = new MemoryStream([]); - bool actual = stream.TryReadInt16BigEndian(out short read); - Assert.False(actual); - Assert.Equal(default, read); - } - - [Fact] - public void TryReadInt16LittleEndianTest() - { - var stream = new MemoryStream([]); - bool actual = stream.TryReadInt16LittleEndian(out short read); - Assert.False(actual); - Assert.Equal(default, read); - } - - [Fact] - public void TryReadInt16BothEndianTest() - { - var stream = new MemoryStream([]); - bool actual = stream.TryReadInt16BothEndian(out BothInt16 read); - Assert.False(actual); - Assert.Equal(default, read.LittleEndian); - Assert.Equal(default, read.BigEndian); - } - - [Fact] - public void TryReadUInt16Test() - { - var stream = new MemoryStream([]); - bool actual = stream.TryReadUInt16(out ushort read); - Assert.False(actual); - Assert.Equal(default, read); - } - - [Fact] - public void TryReadUInt16BigEndianTest() - { - var stream = new MemoryStream([]); - bool actual = stream.TryReadUInt16BigEndian(out ushort read); - Assert.False(actual); - Assert.Equal(default, read); - } - - [Fact] - public void TryReadUInt16LittleEndianTest() - { - var stream = new MemoryStream([]); - bool actual = stream.TryReadUInt16LittleEndian(out ushort read); - Assert.False(actual); - Assert.Equal(default, read); - } - - [Fact] - public void TryReadUInt16BothEndianTest() - { - var stream = new MemoryStream([]); - bool actual = stream.TryReadUInt16BothEndian(out BothUInt16 read); - Assert.False(actual); - Assert.Equal(default, read.LittleEndian); - Assert.Equal(default, read.BigEndian); - } - - [Fact] - public void TryReadWORDTest() - { - var stream = new MemoryStream([]); - bool actual = stream.TryReadWORD(out ushort read); - Assert.False(actual); - Assert.Equal(default, read); - } - - [Fact] - public void TryReadWORDBigEndianTest() - { - var stream = new MemoryStream([]); - bool actual = stream.TryReadWORDBigEndian(out ushort read); - Assert.False(actual); - Assert.Equal(default, read); - } - - [Fact] - public void TryReadWORDLittleEndianTest() - { - var stream = new MemoryStream([]); - bool actual = stream.TryReadWORDLittleEndian(out ushort read); - Assert.False(actual); - Assert.Equal(default, read); - } - - [Fact] - public void TryReadWORDBothEndianTest() - { - var stream = new MemoryStream([]); - bool actual = stream.TryReadWORDBothEndian(out BothUInt16 read); - Assert.False(actual); - Assert.Equal(default, read.LittleEndian); - Assert.Equal(default, read.BigEndian); - } - - [Fact] - public void TryReadHalfTest() - { - var stream = new MemoryStream([]); - bool actual = stream.TryReadHalf(out Half read); - Assert.False(actual); - Assert.Equal(default, read); - } - - [Fact] - public void TryReadHalfBigEndianTest() - { - var stream = new MemoryStream([]); - bool actual = stream.TryReadHalfBigEndian(out Half read); - Assert.False(actual); - Assert.Equal(default, read); - } - - [Fact] - public void TryReadHalfLittleEndianTest() - { - var stream = new MemoryStream([]); - bool actual = stream.TryReadHalfLittleEndian(out Half read); - Assert.False(actual); - Assert.Equal(default, read); - } - - [Fact] - public void TryReadInt24Test() - { - var stream = new MemoryStream([]); - bool actual = stream.TryReadInt24(out Int24 read); - Assert.False(actual); - Assert.Equal(default, (int)read); - } - - [Fact] - public void TryReadInt24BigEndianTest() - { - var stream = new MemoryStream([]); - bool actual = stream.TryReadInt24BigEndian(out Int24 read); - Assert.False(actual); - Assert.Equal(default, (int)read); - } - - [Fact] - public void TryReadInt24LittleEndianTest() - { - var stream = new MemoryStream([]); - bool actual = stream.TryReadInt24LittleEndian(out Int24 read); - Assert.False(actual); - Assert.Equal(default, (int)read); - } - - [Fact] - public void TryReadUInt24Test() - { - var stream = new MemoryStream([]); - bool actual = stream.TryReadUInt24(out UInt24 read); - Assert.False(actual); - Assert.Equal(default, (uint)read); - } - - [Fact] - public void TryReadUInt24BigEndianTest() - { - var stream = new MemoryStream([]); - bool actual = stream.TryReadUInt24BigEndian(out UInt24 read); - Assert.False(actual); - Assert.Equal(default, (uint)read); - } - - [Fact] - public void TryReadUInt24LittleEndianTest() - { - var stream = new MemoryStream([]); - bool actual = stream.TryReadUInt24LittleEndian(out UInt24 read); - Assert.False(actual); - Assert.Equal(default, (uint)read); - } - - [Fact] - public void TryReadInt32Test() - { - var stream = new MemoryStream([]); - bool actual = stream.TryReadInt32(out int read); - Assert.False(actual); - Assert.Equal(default, read); - } - - [Fact] - public void TryReadInt32BigEndianTest() - { - var stream = new MemoryStream([]); - bool actual = stream.TryReadInt32BigEndian(out int read); - Assert.False(actual); - Assert.Equal(default, read); - } - - [Fact] - public void TryReadInt32LittleEndianTest() - { - var stream = new MemoryStream([]); - bool actual = stream.TryReadInt32LittleEndian(out int read); - Assert.False(actual); - Assert.Equal(default, read); - } - - [Fact] - public void TryReadInt32BothEndianTest() - { - var stream = new MemoryStream([]); - bool actual = stream.TryReadInt32BothEndian(out BothInt32 read); - Assert.False(actual); - Assert.Equal(default, read.LittleEndian); - Assert.Equal(default, read.BigEndian); - } - - [Fact] - public void TryReadUInt32Test() - { - var stream = new MemoryStream([]); - bool actual = stream.TryReadUInt32(out uint read); - Assert.False(actual); - Assert.Equal(default, read); - } - - [Fact] - public void TryReadUInt32BigEndianTest() - { - var stream = new MemoryStream([]); - bool actual = stream.TryReadUInt32BigEndian(out uint read); - Assert.False(actual); - Assert.Equal(default, read); - } - - [Fact] - public void TryReadUInt32LittleEndianTest() - { - var stream = new MemoryStream([]); - bool actual = stream.TryReadUInt32LittleEndian(out uint read); - Assert.False(actual); - Assert.Equal(default, read); - } - - [Fact] - public void TryReadUInt32BothEndianTest() - { - var stream = new MemoryStream([]); - bool actual = stream.TryReadUInt32BothEndian(out BothUInt32 read); - Assert.False(actual); - Assert.Equal(default, read.LittleEndian); - Assert.Equal(default, read.BigEndian); - } - - [Fact] - public void TryReadDWORDTest() - { - var stream = new MemoryStream([]); - bool actual = stream.TryReadDWORD(out uint read); - Assert.False(actual); - Assert.Equal(default, read); - } - - [Fact] - public void TryReadDWORDBigEndianTest() - { - var stream = new MemoryStream([]); - bool actual = stream.TryReadDWORDBigEndian(out uint read); - Assert.False(actual); - Assert.Equal(default, read); - } - - [Fact] - public void TryReadDWORDLittleEndianTest() - { - var stream = new MemoryStream([]); - bool actual = stream.TryReadDWORDLittleEndian(out uint read); - Assert.False(actual); - Assert.Equal(default, read); - } - - [Fact] - public void TryReadDWORDBothEndianTest() - { - var stream = new MemoryStream([]); - bool actual = stream.TryReadDWORDBothEndian(out BothUInt32 read); - Assert.False(actual); - Assert.Equal(default, read.LittleEndian); - Assert.Equal(default, read.BigEndian); - } - - [Fact] - public void TryReadSingleTest() - { - var stream = new MemoryStream([]); - bool actual = stream.TryReadSingle(out float read); - Assert.False(actual); - Assert.Equal(default, read); - } - - [Fact] - public void TryReadSingleBigEndianTest() - { - var stream = new MemoryStream([]); - bool actual = stream.TryReadSingleBigEndian(out float read); - Assert.False(actual); - Assert.Equal(default, read); - } - - [Fact] - public void TryReadSingleLittleEndianTest() - { - var stream = new MemoryStream([]); - bool actual = stream.TryReadSingleLittleEndian(out float read); - Assert.False(actual); - Assert.Equal(default, read); - } - - [Fact] - public void TryReadInt48Test() - { - var stream = new MemoryStream([]); - bool actual = stream.TryReadInt48(out Int48 read); - Assert.False(actual); - Assert.Equal(default, (long)read); - } - - [Fact] - public void TryReadInt48BigEndianTest() - { - var stream = new MemoryStream([]); - bool actual = stream.TryReadInt48BigEndian(out Int48 read); - Assert.False(actual); - Assert.Equal(default, (long)read); - } - - [Fact] - public void TryReadInt48LittleEndianTest() - { - var stream = new MemoryStream([]); - bool actual = stream.TryReadInt48LittleEndian(out Int48 read); - Assert.False(actual); - Assert.Equal(default, (long)read); - } - - [Fact] - public void TryReadUInt48Test() - { - var stream = new MemoryStream([]); - bool actual = stream.TryReadUInt48(out UInt48 read); - Assert.False(actual); - Assert.Equal(default, (ulong)read); - } - - [Fact] - public void TryReadUInt48BigEndianTest() - { - var stream = new MemoryStream([]); - bool actual = stream.TryReadUInt48BigEndian(out UInt48 read); - Assert.False(actual); - Assert.Equal(default, (ulong)read); - } - - [Fact] - public void TryReadUInt48LittleEndianTest() - { - var stream = new MemoryStream([]); - bool actual = stream.TryReadUInt48LittleEndian(out UInt48 read); - Assert.False(actual); - Assert.Equal(default, (ulong)read); - } - - [Fact] - public void TryReadInt64Test() - { - var stream = new MemoryStream([]); - bool actual = stream.TryReadInt64(out long read); - Assert.False(actual); - Assert.Equal(default, read); - } - - [Fact] - public void TryReadInt64BigEndianTest() - { - var stream = new MemoryStream([]); - bool actual = stream.TryReadInt64BigEndian(out long read); - Assert.False(actual); - Assert.Equal(default, read); - } - - [Fact] - public void TryReadInt64LittleEndianTest() - { - var stream = new MemoryStream([]); - bool actual = stream.TryReadInt64LittleEndian(out long read); - Assert.False(actual); - Assert.Equal(default, read); - } - - [Fact] - public void TryReadInt64BothEndianTest() - { - var stream = new MemoryStream([]); - bool actual = stream.TryReadInt64BothEndian(out BothInt64 read); - Assert.False(actual); - Assert.Equal(default, read.LittleEndian); - Assert.Equal(default, read.BigEndian); - } - - [Fact] - public void TryReadUInt64Test() - { - var stream = new MemoryStream([]); - bool actual = stream.TryReadUInt64(out ulong read); - Assert.False(actual); - Assert.Equal(default, read); - } - - [Fact] - public void TryReadUInt64BigEndianTest() - { - var stream = new MemoryStream([]); - bool actual = stream.TryReadUInt64BigEndian(out ulong read); - Assert.False(actual); - Assert.Equal(default, read); - } - - [Fact] - public void TryReadUInt64LittleEndianTest() - { - var stream = new MemoryStream([]); - bool actual = stream.TryReadUInt64LittleEndian(out ulong read); - Assert.False(actual); - Assert.Equal(default, read); - } - - [Fact] - public void TryReadUInt64BothEndianTest() - { - var stream = new MemoryStream([]); - bool actual = stream.TryReadUInt64BothEndian(out BothUInt64 read); - Assert.False(actual); - Assert.Equal(default, read.LittleEndian); - Assert.Equal(default, read.BigEndian); - } - - [Fact] - public void TryReadQWORDTest() - { - var stream = new MemoryStream([]); - bool actual = stream.TryReadQWORD(out ulong read); - Assert.False(actual); - Assert.Equal(default, read); - } - - [Fact] - public void TryReadQWORDBigEndianTest() - { - var stream = new MemoryStream([]); - bool actual = stream.TryReadQWORDBigEndian(out ulong read); - Assert.False(actual); - Assert.Equal(default, read); - } - - [Fact] - public void TryReadQWORDLittleEndianTest() - { - var stream = new MemoryStream([]); - bool actual = stream.TryReadQWORDLittleEndian(out ulong read); - Assert.False(actual); - Assert.Equal(default, read); - } - - [Fact] - public void TryReadQWORDBothEndianTest() - { - var stream = new MemoryStream([]); - bool actual = stream.TryReadQWORDBothEndian(out BothUInt64 read); - Assert.False(actual); - Assert.Equal(default, read.LittleEndian); - Assert.Equal(default, read.BigEndian); - } - - [Fact] - public void TryReadDoubleTest() - { - var stream = new MemoryStream([]); - bool actual = stream.TryReadDouble(out double read); - Assert.False(actual); - Assert.Equal(default, read); - } - - [Fact] - public void TryReadDoubleBigEndianTest() - { - var stream = new MemoryStream([]); - bool actual = stream.TryReadDoubleBigEndian(out double read); - Assert.False(actual); - Assert.Equal(default, read); - } - - [Fact] - public void TryReadDoubleLittleEndianTest() - { - var stream = new MemoryStream([]); - bool actual = stream.TryReadDoubleLittleEndian(out double read); - Assert.False(actual); - Assert.Equal(default, read); - } - - [Fact] - public void TryReadGuidTest() - { - var stream = new MemoryStream([]); - bool actual = stream.TryReadGuid(out Guid read); - Assert.False(actual); - Assert.Equal(default, read); - } - - [Fact] - public void TryReadGuidBigEndianTest() - { - var stream = new MemoryStream([]); - bool actual = stream.TryReadGuidBigEndian(out Guid read); - Assert.False(actual); - Assert.Equal(default, read); - } - - [Fact] - public void TryReadGuidLittleEndianTest() - { - var stream = new MemoryStream([]); - bool actual = stream.TryReadGuidLittleEndian(out Guid read); - Assert.False(actual); - Assert.Equal(default, read); - } - - [Fact] - public void TryReadInt128Test() - { - var stream = new MemoryStream([]); - bool actual = stream.TryReadInt128(out Int128 read); - Assert.False(actual); - Assert.Equal(default, read); - } - - [Fact] - public void TryReadInt128BigEndianTest() - { - var stream = new MemoryStream([]); - bool actual = stream.TryReadInt128BigEndian(out Int128 read); - Assert.False(actual); - Assert.Equal(default, read); - } - - [Fact] - public void TryReadInt128LittleEndianTest() - { - var stream = new MemoryStream([]); - bool actual = stream.TryReadInt128LittleEndian(out Int128 read); - Assert.False(actual); - Assert.Equal(default, read); - } - - [Fact] - public void TryReadUInt128Test() - { - var stream = new MemoryStream([]); - bool actual = stream.TryReadUInt128(out UInt128 read); - Assert.False(actual); - Assert.Equal(default, read); - } - - [Fact] - public void TryReadUInt128BigEndianTest() - { - var stream = new MemoryStream([]); - bool actual = stream.TryReadUInt128BigEndian(out UInt128 read); - Assert.False(actual); - Assert.Equal(default, read); - } - - [Fact] - public void TryReadUInt128LittleEndianTest() - { - var stream = new MemoryStream([]); - bool actual = stream.TryReadUInt128LittleEndian(out UInt128 read); - Assert.False(actual); - Assert.Equal(default, read); - } - - [Fact] - public void TryReadDecimalTest() - { - var stream = new MemoryStream([]); - bool actual = stream.TryReadDecimal(out decimal read); - Assert.False(actual); - Assert.Equal(default, read); - } - - [Fact] - public void TryReadDecimalBigEndianTest() - { - var stream = new MemoryStream([]); - bool actual = stream.TryReadDecimalBigEndian(out decimal read); - Assert.False(actual); - Assert.Equal(default, read); - } - - [Fact] - public void TryReadDecimalLittleEndianTest() - { - var stream = new MemoryStream([]); - bool actual = stream.TryReadDecimalLittleEndian(out decimal read); - Assert.False(actual); - Assert.Equal(default, read); - } - - #endregion } } diff --git a/SabreTools.IO.Extensions.Test/StreamWriterExtensionsTests.cs b/SabreTools.IO.Extensions.Test/StreamWriterExtensionsTests.cs index cc655db..96f9f0c 100644 --- a/SabreTools.IO.Extensions.Test/StreamWriterExtensionsTests.cs +++ b/SabreTools.IO.Extensions.Test/StreamWriterExtensionsTests.cs @@ -2,8 +2,6 @@ using System; using System.IO; using System.Linq; using System.Numerics; -using System.Text; -using SabreTools.Numerics; using Xunit; namespace SabreTools.IO.Extensions.Test @@ -19,680 +17,6 @@ namespace SabreTools.IO.Extensions.Test 0x08, 0x09, 0x0A, 0x0B, 0x0C, 0x0D, 0x0E, 0x0F, ]; - /// - /// Represents the decimal value 0.0123456789 - /// - private static readonly byte[] _decimalBytes = - [ - 0x15, 0xCD, 0x5B, 0x07, 0x00, 0x00, 0x00, 0x00, - 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x0A, 0x00, - ]; - - /// - /// Test pattern for big-endian GUID created from - /// - private static readonly byte[] _guidBigEndianbytes = - [ - 0x03, 0x02, 0x01, 0x00, 0x05, 0x04, 0x07, 0x06, - 0x08, 0x09, 0x0A, 0x0B, 0x0C, 0x0D, 0x0E, 0x0F, - ]; - - [Fact] - public void WriteByteValueTest() - { - var stream = new MemoryStream(new byte[16], 0, 16, true, true); - byte[] expected = [.. _bytes.Take(1)]; - bool write = stream.Write((byte)0x00); - Assert.True(write); - ValidateBytes(expected, stream.GetBuffer()); - } - - [Fact] - public void WriteByteBothEndianTest() - { - var stream = new MemoryStream(new byte[16], 0, 16, true, true); - byte[] expected = [.. _bytes.Take(2)]; - - int offset = 0; - stream.WriteBothEndian(_bytes.ReadByteBothEndian(ref offset)); - ValidateBytes(expected, stream.GetBuffer()); - } - - [Fact] - public void WriteBytesTest() - { - var stream = new MemoryStream(new byte[16], 0, 16, true, true); - byte[] expected = [.. _bytes.Take(4)]; - bool write = StreamWriterExtensions.Write(stream, [0x00, 0x01, 0x02, 0x03]); - Assert.True(write); - ValidateBytes(expected, stream.GetBuffer()); - } - - [Fact] - public void WriteBytesBigEndianTest() - { - var stream = new MemoryStream(new byte[16], 0, 16, true, true); - byte[] expected = [.. _bytes.Take(4)]; - stream.WriteBigEndian([0x03, 0x02, 0x01, 0x00]); - ValidateBytes(expected, stream.GetBuffer()); - } - - [Fact] - public void WriteSByteTest() - { - var stream = new MemoryStream(new byte[16], 0, 16, true, true); - byte[] expected = [.. _bytes.Take(1)]; - bool write = stream.Write((sbyte)0x00); - Assert.True(write); - ValidateBytes(expected, stream.GetBuffer()); - } - - [Fact] - public void WriteSByteBothEndianTest() - { - var stream = new MemoryStream(new byte[16], 0, 16, true, true); - byte[] expected = [.. _bytes.Take(2)]; - - int offset = 0; - stream.WriteBothEndian(_bytes.ReadSByteBothEndian(ref offset)); - ValidateBytes(expected, stream.GetBuffer()); - } - - [Fact] - public void WriteCharTest() - { - var stream = new MemoryStream(new byte[16], 0, 16, true, true); - byte[] expected = [.. _bytes.Take(1)]; - bool write = stream.Write('\0'); - Assert.True(write); - ValidateBytes(expected, stream.GetBuffer()); - } - - [Fact] - public void WriteCharEncodingTest() - { - var stream = new MemoryStream(new byte[16], 0, 16, true, true); - byte[] expected = [0x00, 0x00]; - stream.Write('\0', Encoding.Unicode); - ValidateBytes(expected, stream.GetBuffer()); - } - - [Fact] - public void WriteInt16Test() - { - var stream = new MemoryStream(new byte[16], 0, 16, true, true); - byte[] expected = [.. _bytes.Take(2)]; - bool write = stream.Write((short)0x0100); - Assert.True(write); - ValidateBytes(expected, stream.GetBuffer()); - } - - [Fact] - public void WriteInt16BigEndianTest() - { - var stream = new MemoryStream(new byte[16], 0, 16, true, true); - byte[] expected = [.. _bytes.Take(2)]; - bool write = stream.WriteBigEndian((short)0x0001); - Assert.True(write); - ValidateBytes(expected, stream.GetBuffer()); - } - - [Fact] - public void WriteInt16LittleEndianTest() - { - var stream = new MemoryStream(new byte[16], 0, 16, true, true); - byte[] expected = [.. _bytes.Take(2)]; - bool write = stream.WriteLittleEndian((short)0x0100); - Assert.True(write); - ValidateBytes(expected, stream.GetBuffer()); - } - - [Fact] - public void WriteInt16BothEndianTest() - { - var stream = new MemoryStream(new byte[16], 0, 16, true, true); - byte[] expected = [.. _bytes.Take(4)]; - - int offset = 0; - stream.WriteBothEndian(_bytes.ReadInt16BothEndian(ref offset)); - ValidateBytes(expected, stream.GetBuffer()); - } - - [Fact] - public void WriteUInt16Test() - { - var stream = new MemoryStream(new byte[16], 0, 16, true, true); - byte[] expected = [.. _bytes.Take(2)]; - bool write = stream.Write((ushort)0x0100); - Assert.True(write); - ValidateBytes(expected, stream.GetBuffer()); - } - - [Fact] - public void WriteUInt16BigEndianTest() - { - var stream = new MemoryStream(new byte[16], 0, 16, true, true); - byte[] expected = [.. _bytes.Take(2)]; - bool write = stream.WriteBigEndian((ushort)0x0001); - Assert.True(write); - ValidateBytes(expected, stream.GetBuffer()); - } - - [Fact] - public void WriteUInt16LittleEndianTest() - { - var stream = new MemoryStream(new byte[16], 0, 16, true, true); - byte[] expected = [.. _bytes.Take(2)]; - bool write = stream.WriteLittleEndian((ushort)0x0100); - Assert.True(write); - ValidateBytes(expected, stream.GetBuffer()); - } - - [Fact] - public void WriteUInt16BothEndianTest() - { - var stream = new MemoryStream(new byte[16], 0, 16, true, true); - byte[] expected = [.. _bytes.Take(4)]; - - int offset = 0; - stream.WriteBothEndian(_bytes.ReadUInt16BothEndian(ref offset)); - ValidateBytes(expected, stream.GetBuffer()); - } - - [Fact] - public void WriteHalfTest() - { - var stream = new MemoryStream(new byte[16], 0, 16, true, true); - byte[] expected = [.. _bytes.Take(2)]; - bool write = stream.Write(BitConverter.Int16BitsToHalf(0x0100)); - Assert.True(write); - ValidateBytes(expected, stream.GetBuffer()); - } - - [Fact] - public void WriteHalfBigEndianTest() - { - var stream = new MemoryStream(new byte[16], 0, 16, true, true); - byte[] expected = [.. _bytes.Take(2)]; - bool write = stream.WriteBigEndian(BitConverter.Int16BitsToHalf(0x0001)); - Assert.True(write); - ValidateBytes(expected, stream.GetBuffer()); - } - - [Fact] - public void WriteHalfLittleEndianTest() - { - var stream = new MemoryStream(new byte[16], 0, 16, true, true); - byte[] expected = [.. _bytes.Take(2)]; - bool write = stream.WriteLittleEndian(BitConverter.Int16BitsToHalf(0x0100)); - Assert.True(write); - ValidateBytes(expected, stream.GetBuffer()); - } - - [Fact] - public void WriteInt24Test() - { - var stream = new MemoryStream(new byte[16], 0, 16, true, true); - byte[] expected = [.. _bytes.Take(3)]; - bool write = stream.Write((Int24)0x020100); - Assert.True(write); - ValidateBytes(expected, stream.GetBuffer()); - } - - [Fact] - public void WriteInt24BigEndianTest() - { - var stream = new MemoryStream(new byte[16], 0, 16, true, true); - byte[] expected = [.. _bytes.Take(3)]; - bool write = stream.WriteBigEndian((Int24)0x000102); - Assert.True(write); - ValidateBytes(expected, stream.GetBuffer()); - } - - [Fact] - public void WriteInt24LittleEndianTest() - { - var stream = new MemoryStream(new byte[16], 0, 16, true, true); - byte[] expected = [.. _bytes.Take(3)]; - bool write = stream.WriteLittleEndian((Int24)0x020100); - Assert.True(write); - ValidateBytes(expected, stream.GetBuffer()); - } - - [Fact] - public void WriteUInt24Test() - { - var stream = new MemoryStream(new byte[16], 0, 16, true, true); - byte[] expected = [.. _bytes.Take(3)]; - bool write = stream.Write((UInt24)0x020100); - Assert.True(write); - ValidateBytes(expected, stream.GetBuffer()); - } - - [Fact] - public void WriteUInt24BigEndianTest() - { - var stream = new MemoryStream(new byte[16], 0, 16, true, true); - byte[] expected = [.. _bytes.Take(3)]; - bool write = stream.WriteBigEndian((UInt24)0x000102); - Assert.True(write); - ValidateBytes(expected, stream.GetBuffer()); - } - - [Fact] - public void WriteUInt24LittleEndianTest() - { - var stream = new MemoryStream(new byte[16], 0, 16, true, true); - byte[] expected = [.. _bytes.Take(3)]; - bool write = stream.WriteLittleEndian((UInt24)0x020100); - Assert.True(write); - ValidateBytes(expected, stream.GetBuffer()); - } - - [Fact] - public void WriteInt32Test() - { - var stream = new MemoryStream(new byte[16], 0, 16, true, true); - byte[] expected = [.. _bytes.Take(4)]; - bool write = stream.Write(0x03020100); - Assert.True(write); - ValidateBytes(expected, stream.GetBuffer()); - } - - [Fact] - public void WriteInt32BigEndianTest() - { - var stream = new MemoryStream(new byte[16], 0, 16, true, true); - byte[] expected = [.. _bytes.Take(4)]; - bool write = stream.WriteBigEndian(0x00010203); - Assert.True(write); - ValidateBytes(expected, stream.GetBuffer()); - } - - [Fact] - public void WriteInt32LittleEndianTest() - { - var stream = new MemoryStream(new byte[16], 0, 16, true, true); - byte[] expected = [.. _bytes.Take(4)]; - bool write = stream.WriteLittleEndian(0x03020100); - Assert.True(write); - ValidateBytes(expected, stream.GetBuffer()); - } - - [Fact] - public void WriteInt32BothEndianTest() - { - var stream = new MemoryStream(new byte[16], 0, 16, true, true); - byte[] expected = [.. _bytes.Take(8)]; - - int offset = 0; - stream.WriteBothEndian(_bytes.ReadInt32BothEndian(ref offset)); - ValidateBytes(expected, stream.GetBuffer()); - } - - [Fact] - public void WriteUInt32Test() - { - var stream = new MemoryStream(new byte[16], 0, 16, true, true); - byte[] expected = [.. _bytes.Take(4)]; - bool write = stream.Write((uint)0x03020100); - Assert.True(write); - ValidateBytes(expected, stream.GetBuffer()); - } - - [Fact] - public void WriteUInt32BigEndianTest() - { - var stream = new MemoryStream(new byte[16], 0, 16, true, true); - byte[] expected = [.. _bytes.Take(4)]; - bool write = stream.WriteBigEndian((uint)0x00010203); - Assert.True(write); - ValidateBytes(expected, stream.GetBuffer()); - } - - [Fact] - public void WriteUInt32LittleEndianTest() - { - var stream = new MemoryStream(new byte[16], 0, 16, true, true); - byte[] expected = [.. _bytes.Take(4)]; - bool write = stream.WriteLittleEndian((uint)0x03020100); - Assert.True(write); - ValidateBytes(expected, stream.GetBuffer()); - } - - [Fact] - public void WriteUInt32BothEndianTest() - { - var stream = new MemoryStream(new byte[16], 0, 16, true, true); - byte[] expected = [.. _bytes.Take(8)]; - - int offset = 0; - stream.WriteBothEndian(_bytes.ReadUInt32BothEndian(ref offset)); - ValidateBytes(expected, stream.GetBuffer()); - } - - [Fact] - public void WriteSingleTest() - { - var stream = new MemoryStream(new byte[16], 0, 16, true, true); - byte[] expected = [.. _bytes.Take(4)]; - bool write = stream.Write(BitConverter.Int32BitsToSingle(0x03020100)); - Assert.True(write); - ValidateBytes(expected, stream.GetBuffer()); - } - - [Fact] - public void WriteSingleBigEndianTest() - { - var stream = new MemoryStream(new byte[16], 0, 16, true, true); - byte[] expected = [.. _bytes.Take(4)]; - bool write = stream.WriteBigEndian(BitConverter.Int32BitsToSingle(0x00010203)); - Assert.True(write); - ValidateBytes(expected, stream.GetBuffer()); - } - - [Fact] - public void WriteSingleLittleEndianTest() - { - var stream = new MemoryStream(new byte[16], 0, 16, true, true); - byte[] expected = [.. _bytes.Take(4)]; - bool write = stream.WriteLittleEndian(BitConverter.Int32BitsToSingle(0x03020100)); - Assert.True(write); - ValidateBytes(expected, stream.GetBuffer()); - } - - [Fact] - public void WriteInt48Test() - { - var stream = new MemoryStream(new byte[16], 0, 16, true, true); - byte[] expected = [.. _bytes.Take(6)]; - bool write = stream.Write((Int48)0x050403020100); - Assert.True(write); - ValidateBytes(expected, stream.GetBuffer()); - } - - [Fact] - public void WriteInt48BigEndianTest() - { - var stream = new MemoryStream(new byte[16], 0, 16, true, true); - byte[] expected = [.. _bytes.Take(6)]; - bool write = stream.WriteBigEndian((Int48)0x000102030405); - Assert.True(write); - ValidateBytes(expected, stream.GetBuffer()); - } - - [Fact] - public void WriteInt48LittleEndianTest() - { - var stream = new MemoryStream(new byte[16], 0, 16, true, true); - byte[] expected = [.. _bytes.Take(6)]; - bool write = stream.WriteLittleEndian((Int48)0x050403020100); - Assert.True(write); - ValidateBytes(expected, stream.GetBuffer()); - } - - [Fact] - public void WriteUInt48Test() - { - var stream = new MemoryStream(new byte[16], 0, 16, true, true); - byte[] expected = [.. _bytes.Take(6)]; - bool write = stream.Write((UInt48)0x050403020100); - Assert.True(write); - ValidateBytes(expected, stream.GetBuffer()); - } - - [Fact] - public void WriteUInt48BigEndianTest() - { - var stream = new MemoryStream(new byte[16], 0, 16, true, true); - byte[] expected = [.. _bytes.Take(6)]; - bool write = stream.WriteBigEndian((UInt48)0x000102030405); - Assert.True(write); - ValidateBytes(expected, stream.GetBuffer()); - } - - [Fact] - public void WriteUInt48LittleEndianTest() - { - var stream = new MemoryStream(new byte[16], 0, 16, true, true); - byte[] expected = [.. _bytes.Take(6)]; - bool write = stream.WriteLittleEndian((UInt48)0x050403020100); - Assert.True(write); - ValidateBytes(expected, stream.GetBuffer()); - } - - [Fact] - public void WriteInt64Test() - { - var stream = new MemoryStream(new byte[16], 0, 16, true, true); - byte[] expected = [.. _bytes.Take(8)]; - bool write = stream.Write(0x0706050403020100); - Assert.True(write); - ValidateBytes(expected, stream.GetBuffer()); - } - - [Fact] - public void WriteInt64BigEndianTest() - { - var stream = new MemoryStream(new byte[16], 0, 16, true, true); - byte[] expected = [.. _bytes.Take(8)]; - bool write = stream.WriteBigEndian(0x0001020304050607); - Assert.True(write); - ValidateBytes(expected, stream.GetBuffer()); - } - - [Fact] - public void WriteInt64LittleEndianTest() - { - var stream = new MemoryStream(new byte[16], 0, 16, true, true); - byte[] expected = [.. _bytes.Take(8)]; - bool write = stream.WriteLittleEndian(0x0706050403020100); - Assert.True(write); - ValidateBytes(expected, stream.GetBuffer()); - } - - [Fact] - public void WriteInt64BothEndianTest() - { - var stream = new MemoryStream(new byte[16], 0, 16, true, true); - byte[] expected = [.. _bytes.Take(16)]; - - int offset = 0; - stream.WriteBothEndian(_bytes.ReadInt64BothEndian(ref offset)); - ValidateBytes(expected, stream.GetBuffer()); - } - - [Fact] - public void WriteUInt64Test() - { - var stream = new MemoryStream(new byte[16], 0, 16, true, true); - byte[] expected = [.. _bytes.Take(8)]; - bool write = stream.Write((ulong)0x0706050403020100); - Assert.True(write); - ValidateBytes(expected, stream.GetBuffer()); - } - - [Fact] - public void WriteUInt64BigEndianTest() - { - var stream = new MemoryStream(new byte[16], 0, 16, true, true); - byte[] expected = [.. _bytes.Take(8)]; - bool write = stream.WriteBigEndian((ulong)0x0001020304050607); - Assert.True(write); - ValidateBytes(expected, stream.GetBuffer()); - } - - [Fact] - public void WriteUInt64LittleEndianTest() - { - var stream = new MemoryStream(new byte[16], 0, 16, true, true); - byte[] expected = [.. _bytes.Take(8)]; - bool write = stream.WriteLittleEndian((ulong)0x0706050403020100); - Assert.True(write); - ValidateBytes(expected, stream.GetBuffer()); - } - - [Fact] - public void WriteUInt64BothEndianTest() - { - var stream = new MemoryStream(new byte[16], 0, 16, true, true); - byte[] expected = [.. _bytes.Take(16)]; - - int offset = 0; - stream.WriteBothEndian(_bytes.ReadUInt64BothEndian(ref offset)); - ValidateBytes(expected, stream.GetBuffer()); - } - - [Fact] - public void WriteDoubleTest() - { - var stream = new MemoryStream(new byte[16], 0, 16, true, true); - byte[] expected = [.. _bytes.Take(8)]; - bool write = stream.Write(BitConverter.Int64BitsToDouble(0x0706050403020100)); - Assert.True(write); - ValidateBytes(expected, stream.GetBuffer()); - } - - [Fact] - public void WriteDoubleBigEndianTest() - { - var stream = new MemoryStream(new byte[16], 0, 16, true, true); - byte[] expected = [.. _bytes.Take(8)]; - bool write = stream.WriteBigEndian(BitConverter.Int64BitsToDouble(0x0001020304050607)); - Assert.True(write); - ValidateBytes(expected, stream.GetBuffer()); - } - - [Fact] - public void WriteDoubleLittleEndianTest() - { - var stream = new MemoryStream(new byte[16], 0, 16, true, true); - byte[] expected = [.. _bytes.Take(8)]; - bool write = stream.WriteLittleEndian(BitConverter.Int64BitsToDouble(0x0706050403020100)); - Assert.True(write); - ValidateBytes(expected, stream.GetBuffer()); - } - - [Fact] - public void WriteGuidTest() - { - var stream = new MemoryStream(new byte[16], 0, 16, true, true); - byte[] expected = [.. _bytes.Take(16)]; - bool write = stream.Write(new Guid(_bytes)); - Assert.True(write); - ValidateBytes(expected, stream.GetBuffer()); - } - - [Fact] - public void WriteGuidBigEndianTest() - { - var stream = new MemoryStream(new byte[16], 0, 16, true, true); - byte[] expected = [.. _guidBigEndianbytes.Take(16)]; - bool write = stream.WriteBigEndian(new Guid(_bytes)); - Assert.True(write); - ValidateBytes(expected, stream.GetBuffer()); - } - - [Fact] - public void WriteGuidLittleEndianTest() - { - var stream = new MemoryStream(new byte[16], 0, 16, true, true); - byte[] expected = [.. _bytes.Take(16)]; - bool write = stream.WriteLittleEndian(new Guid(_bytes)); - Assert.True(write); - ValidateBytes(expected, stream.GetBuffer()); - } - - [Fact] - public void WriteInt128Test() - { - var stream = new MemoryStream(new byte[16], 0, 16, true, true); - byte[] expected = [.. _bytes.Take(16)]; - bool write = stream.Write((Int128)new BigInteger(_bytes)); - Assert.True(write); - ValidateBytes(expected, stream.GetBuffer()); - } - - [Fact] - public void WriteInt128BigEndianTest() - { - var stream = new MemoryStream(new byte[16], 0, 16, true, true); - byte[] expected = [.. _bytes.Take(16)]; - bool write = stream.WriteBigEndian((Int128)new BigInteger(Enumerable.Reverse(_bytes).ToArray())); - Assert.True(write); - ValidateBytes(expected, stream.GetBuffer()); - } - - [Fact] - public void WriteInt128LittleEndianTest() - { - var stream = new MemoryStream(new byte[16], 0, 16, true, true); - byte[] expected = [.. _bytes.Take(16)]; - bool write = stream.WriteLittleEndian((Int128)new BigInteger(_bytes)); - Assert.True(write); - ValidateBytes(expected, stream.GetBuffer()); - } - - [Fact] - public void WriteUInt128Test() - { - var stream = new MemoryStream(new byte[16], 0, 16, true, true); - byte[] expected = [.. _bytes.Take(16)]; - bool write = stream.Write((UInt128)new BigInteger(_bytes)); - Assert.True(write); - ValidateBytes(expected, stream.GetBuffer()); - } - - [Fact] - public void WriteUInt128BigEndianTest() - { - var stream = new MemoryStream(new byte[16], 0, 16, true, true); - byte[] expected = [.. _bytes.Take(16)]; - bool write = stream.WriteBigEndian((UInt128)new BigInteger(Enumerable.Reverse(_bytes).ToArray())); - Assert.True(write); - ValidateBytes(expected, stream.GetBuffer()); - } - - [Fact] - public void WriteUInt128LittleEndianTest() - { - var stream = new MemoryStream(new byte[16], 0, 16, true, true); - byte[] expected = [.. _bytes.Take(16)]; - bool write = stream.WriteLittleEndian((UInt128)new BigInteger(_bytes)); - Assert.True(write); - ValidateBytes(expected, stream.GetBuffer()); - } - - [Fact] - public void WriteDecimalTest() - { - var stream = new MemoryStream(new byte[16], 0, 16, true, true); - byte[] expected = [.. _decimalBytes.Take(16)]; - bool write = stream.Write(0.0123456789M); - Assert.True(write); - ValidateBytes(expected, stream.GetBuffer()); - } - - [Fact] - public void WriteDecimalBigEndianTest() - { - var stream = new MemoryStream(new byte[16], 0, 16, true, true); - byte[] expected = [.. _decimalBytes.Take(16).Reverse()]; - bool write = stream.WriteBigEndian(0.0123456789M); - Assert.True(write); - ValidateBytes(expected, stream.GetBuffer()); - } - - [Fact] - public void WriteDecimalLittleEndianTest() - { - var stream = new MemoryStream(new byte[16], 0, 16, true, true); - byte[] expected = [.. _decimalBytes.Take(16)]; - bool write = stream.WriteLittleEndian(0.0123456789M); - Assert.True(write); - ValidateBytes(expected, stream.GetBuffer()); - } - [Fact] public void WriteNullTerminatedAnsiStringTest() { diff --git a/SabreTools.IO.Extensions/BinaryReaderExtensions.cs b/SabreTools.IO.Extensions/BinaryReaderExtensions.cs index e6ebc56..ed48af8 100644 --- a/SabreTools.IO.Extensions/BinaryReaderExtensions.cs +++ b/SabreTools.IO.Extensions/BinaryReaderExtensions.cs @@ -4,7 +4,7 @@ using System.IO; using System.Reflection; using System.Runtime.InteropServices; using System.Text; -using SabreTools.Numerics; +using SabreTools.Numerics.Extensions; namespace SabreTools.IO.Extensions { @@ -13,562 +13,6 @@ namespace SabreTools.IO.Extensions /// public static class BinaryReaderExtensions { - #region Exact Read - - /// - /// Reads in both-endian format - public static BothUInt8 ReadByteBothEndian(this BinaryReader reader) - { - byte le = reader.ReadByte(); - byte be = reader.ReadByte(); - return new BothUInt8(le, be); - } - - /// - /// Reads in both-endian format - public static BothInt8 ReadSByteBothEndian(this BinaryReader reader) - { - sbyte le = reader.ReadSByte(); - sbyte be = reader.ReadSByte(); - return new BothInt8(le, be); - } - - /// - /// Reads in big-endian format - public static short ReadInt16BigEndian(this BinaryReader reader) - { - byte[] buffer = reader.ReadBytes(2); - return buffer.ToInt16BigEndian(); - } - - /// - /// Reads in little-endian format - public static short ReadInt16LittleEndian(this BinaryReader reader) - { - byte[] buffer = reader.ReadBytes(2); - return buffer.ToInt16LittleEndian(); - } - - /// - /// Reads in both-endian format - public static BothInt16 ReadInt16BothEndian(this BinaryReader reader) - { - short le = reader.ReadInt16LittleEndian(); - short be = reader.ReadInt16BigEndian(); - return new BothInt16(le, be); - } - - /// - /// Reads in big-endian format - public static ushort ReadUInt16BigEndian(this BinaryReader reader) - { - byte[] buffer = reader.ReadBytes(2); - return buffer.ToUInt16BigEndian(); - } - - /// - /// Reads in little-endian format - public static ushort ReadUInt16LittleEndian(this BinaryReader reader) - { - byte[] buffer = reader.ReadBytes(2); - return buffer.ToUInt16LittleEndian(); - } - - /// - /// Reads in both-endian format - public static BothUInt16 ReadUInt16BothEndian(this BinaryReader reader) - { - ushort le = reader.ReadUInt16LittleEndian(); - ushort be = reader.ReadUInt16BigEndian(); - return new BothUInt16(le, be); - } - - /// - /// Read a WORD (2-byte) from the base stream - /// - public static ushort ReadWORD(this BinaryReader reader) - => reader.ReadUInt16(); - - /// - /// Read a WORD (2-byte) from the base stream - /// - /// Reads in big-endian format - public static ushort ReadWORDBigEndian(this BinaryReader reader) - => reader.ReadUInt16BigEndian(); - - /// - /// Read a WORD (2-byte) from the base stream - /// - /// Reads in little-endian format - public static ushort ReadWORDLittleEndian(this BinaryReader reader) - => reader.ReadUInt16LittleEndian(); - - /// - /// Read a WORD (2-byte) from the base stream - /// - /// Reads in both-endian format - public static BothUInt16 ReadWORDBothEndian(this BinaryReader reader) - => reader.ReadUInt16BothEndian(); - -#if NET5_0_OR_GREATER - /// - /// Read a Half from the underlying stream - /// - /// Reads in machine native format - public static Half ReadHalf(this BinaryReader reader) - { - if (BitConverter.IsLittleEndian) - return reader.ReadHalfLittleEndian(); - else - return reader.ReadHalfBigEndian(); - } - - /// - /// Reads in big-endian format - public static Half ReadHalfBigEndian(this BinaryReader reader) - { - byte[] buffer = reader.ReadBytes(2); - return buffer.ToHalfBigEndian(); - } - - /// - /// Reads in little-endian format - public static Half ReadHalfLittleEndian(this BinaryReader reader) - { - byte[] buffer = reader.ReadBytes(2); - return buffer.ToHalfLittleEndian(); - } -#endif - - /// - /// Read an Int24 from the base stream - /// - /// Reads in machine native format - public static Int24 ReadInt24(this BinaryReader reader) - { - if (BitConverter.IsLittleEndian) - return reader.ReadInt24LittleEndian(); - else - return reader.ReadInt24BigEndian(); - } - - /// - /// Read an Int24 from the base stream - /// - /// Reads in big-endian format - public static Int24 ReadInt24BigEndian(this BinaryReader reader) - { - byte[] buffer = reader.ReadBytes(3); - return buffer.ToInt24BigEndian(); - } - - /// - /// Read an Int24 from the base stream - /// - /// Reads in little-endian format - public static Int24 ReadInt24LittleEndian(this BinaryReader reader) - { - byte[] buffer = reader.ReadBytes(3); - return buffer.ToInt24LittleEndian(); - } - - /// - /// Read a UInt24 from the base stream - /// - /// Reads in machine native format - public static UInt24 ReadUInt24(this BinaryReader reader) - { - if (BitConverter.IsLittleEndian) - return reader.ReadUInt24LittleEndian(); - else - return reader.ReadUInt24BigEndian(); - } - - /// - /// Read a UInt24 from the base stream - /// - /// Reads in big-endian format - public static UInt24 ReadUInt24BigEndian(this BinaryReader reader) - { - byte[] buffer = reader.ReadBytes(3); - return buffer.ToUInt24BigEndian(); - } - - /// - /// Read a UInt24 from the base stream - /// - /// Reads in little-endian format - public static UInt24 ReadUInt24LittleEndian(this BinaryReader reader) - { - byte[] buffer = reader.ReadBytes(3); - return buffer.ToUInt24LittleEndian(); - } - - /// - /// Reads in big-endian format - public static int ReadInt32BigEndian(this BinaryReader reader) - { - byte[] buffer = reader.ReadBytes(4); - return buffer.ToInt32BigEndian(); - } - - /// - /// Reads in little-endian format - public static int ReadInt32LittleEndian(this BinaryReader reader) - { - byte[] buffer = reader.ReadBytes(4); - return buffer.ToInt32LittleEndian(); - } - - /// - /// Reads in both-endian format - public static BothInt32 ReadInt32BothEndian(this BinaryReader reader) - { - int le = reader.ReadInt32LittleEndian(); - int be = reader.ReadInt32BigEndian(); - return new BothInt32(le, be); - } - - /// - /// Reads in big-endian format - public static uint ReadUInt32BigEndian(this BinaryReader reader) - { - byte[] buffer = reader.ReadBytes(4); - return buffer.ToUInt32BigEndian(); - } - - /// - /// Reads in little-endian format - public static uint ReadUInt32LittleEndian(this BinaryReader reader) - { - byte[] buffer = reader.ReadBytes(4); - return buffer.ToUInt32LittleEndian(); - } - - /// - /// Reads in both-endian format - public static BothUInt32 ReadUInt32BothEndian(this BinaryReader reader) - { - uint le = reader.ReadUInt32LittleEndian(); - uint be = reader.ReadUInt32BigEndian(); - return new BothUInt32(le, be); - } - - /// - /// Read a DWORD (4-byte) from the base stream - /// - public static uint ReadDWORD(this BinaryReader reader) - => reader.ReadUInt32(); - - /// - /// Read a DWORD (4-byte) from the base stream - /// - /// Reads in big-endian format - public static uint ReadDWORDBigEndian(this BinaryReader reader) - => reader.ReadUInt32BigEndian(); - - /// - /// Read a DWORD (4-byte) from the base stream - /// - /// Reads in little-endian format - public static uint ReadDWORDLittleEndian(this BinaryReader reader) - => reader.ReadUInt32LittleEndian(); - - /// - /// Read a DWORD (4-byte) from the base stream - /// - /// Reads in both-endian format - public static BothUInt32 ReadDWORDBothEndian(this BinaryReader reader) - => reader.ReadUInt32BothEndian(); - - /// - /// Reads in big-endian format - public static float ReadSingleBigEndian(this BinaryReader reader) - { - byte[] buffer = reader.ReadBytes(4); - return buffer.ToSingleBigEndian(); - } - - /// - /// Reads in little-endian format - public static float ReadSingleLittleEndian(this BinaryReader reader) - { - byte[] buffer = reader.ReadBytes(4); - return buffer.ToSingleLittleEndian(); - } - - /// - /// Read an Int48 from the base stream - /// - /// Reads in machine native format - public static Int48 ReadInt48(this BinaryReader reader) - { - if (BitConverter.IsLittleEndian) - return reader.ReadInt48LittleEndian(); - else - return reader.ReadInt48BigEndian(); - } - - /// - /// Read an Int48 from the base stream - /// - /// Reads in big-endian format - public static Int48 ReadInt48BigEndian(this BinaryReader reader) - { - byte[] buffer = reader.ReadBytes(6); - return buffer.ToInt48BigEndian(); - } - - /// - /// Read an Int48 from the base stream - /// - /// Reads in little-endian format - public static Int48 ReadInt48LittleEndian(this BinaryReader reader) - { - byte[] buffer = reader.ReadBytes(6); - return buffer.ToInt48LittleEndian(); - } - - /// - /// Read a UInt48 from the base stream - /// - /// Reads in machine native format - public static UInt48 ReadUInt48(this BinaryReader reader) - { - if (BitConverter.IsLittleEndian) - return reader.ReadUInt48LittleEndian(); - else - return reader.ReadUInt48BigEndian(); - } - - /// - /// Read a UInt48 from the base stream - /// - /// Reads in big-endian format - public static UInt48 ReadUInt48BigEndian(this BinaryReader reader) - { - byte[] buffer = reader.ReadBytes(6); - return buffer.ToUInt48BigEndian(); - } - - /// - /// Read an UInt48 from the base stream - /// - /// Reads in little-endian format - public static UInt48 ReadUInt48LittleEndian(this BinaryReader reader) - { - byte[] buffer = reader.ReadBytes(6); - return buffer.ToUInt48LittleEndian(); - } - - /// - /// Reads in big-endian format - public static long ReadInt64BigEndian(this BinaryReader reader) - { - byte[] buffer = reader.ReadBytes(8); - return buffer.ToInt64BigEndian(); - } - - /// - /// Reads in little-endian format - public static long ReadInt64LittleEndian(this BinaryReader reader) - { - byte[] buffer = reader.ReadBytes(8); - return buffer.ToInt64LittleEndian(); - } - - /// - /// Reads in both-endian format - public static BothInt64 ReadInt64BothEndian(this BinaryReader reader) - { - long le = reader.ReadInt64LittleEndian(); - long be = reader.ReadInt64BigEndian(); - return new BothInt64(le, be); - } - - /// - /// Reads in big-endian format - public static ulong ReadUInt64BigEndian(this BinaryReader reader) - { - byte[] buffer = reader.ReadBytes(8); - return buffer.ToUInt64BigEndian(); - } - - /// - /// Reads in little-endian format - public static ulong ReadUInt64LittleEndian(this BinaryReader reader) - { - byte[] buffer = reader.ReadBytes(8); - return buffer.ToUInt64LittleEndian(); - } - - /// - /// Reads in both-endian format - public static BothUInt64 ReadUInt64BothEndian(this BinaryReader reader) - { - ulong le = reader.ReadUInt64LittleEndian(); - ulong be = reader.ReadUInt64BigEndian(); - return new BothUInt64(le, be); - } - - /// - /// Read a QWORD (8-byte) from the base stream - /// - /// Reads in machine native format - public static ulong ReadQWORD(this BinaryReader reader) - => reader.ReadUInt64(); - - /// - /// Read a QWORD (8-byte) from the base stream - /// - /// Reads in big-endian format - public static ulong ReadQWORDBigEndian(this BinaryReader reader) - => reader.ReadUInt64BigEndian(); - - /// - /// Read a QWORD (8-byte) from the base stream - /// - /// Reads in little-endian format - public static ulong ReadQWORDLittleEndian(this BinaryReader reader) - => reader.ReadUInt64LittleEndian(); - - /// - /// Read a QWORD (8-byte) from the base stream - /// - /// Reads in both-endian format - public static BothUInt64 ReadQWORDBothEndian(this BinaryReader reader) - => reader.ReadUInt64BothEndian(); - - /// - /// Reads in big-endian format - public static double ReadDoubleBigEndian(this BinaryReader reader) - { - byte[] buffer = reader.ReadBytes(8); - return buffer.ToDoubleBigEndian(); - } - - /// - /// Reads in little-endian format - public static double ReadDoubleLittleEndian(this BinaryReader reader) - { - byte[] buffer = reader.ReadBytes(8); - return buffer.ToDoubleLittleEndian(); - } - - /// - /// Read a Guid from the underlying stream - /// - /// Reads in machine native format - public static Guid ReadGuid(this BinaryReader reader) - { - if (BitConverter.IsLittleEndian) - return reader.ReadGuidLittleEndian(); - else - return reader.ReadGuidBigEndian(); - } - - /// - /// Read a Guid from the underlying stream - /// - /// Reads in big-endian format - public static Guid ReadGuidBigEndian(this BinaryReader reader) - { - byte[] buffer = reader.ReadBytes(16); - return buffer.ToGuidBigEndian(); - } - - /// - /// Read a Guid from the underlying stream - /// - /// Reads in little-endian format - public static Guid ReadGuidLittleEndian(this BinaryReader reader) - { - byte[] buffer = reader.ReadBytes(16); - return buffer.ToGuidLittleEndian(); - } - -#if NET7_0_OR_GREATER - /// - /// Read an Int128 from the underlying stream - /// - /// Reads in machine native format - public static Int128 ReadInt128(this BinaryReader reader) - { - if (BitConverter.IsLittleEndian) - return reader.ReadInt128LittleEndian(); - else - return reader.ReadInt128BigEndian(); - } - - /// - /// Read an Int128 from the underlying stream - /// - /// Reads in big-endian format - public static Int128 ReadInt128BigEndian(this BinaryReader reader) - { - byte[] buffer = reader.ReadBytes(16); - return buffer.ToInt128BigEndian(); - } - - /// - /// Read an Int128 from the underlying stream - /// - /// Reads in little-endian format - public static Int128 ReadInt128LittleEndian(this BinaryReader reader) - { - byte[] buffer = reader.ReadBytes(16); - return buffer.ToInt128LittleEndian(); - } - - /// - /// Read a UInt128 from the underlying stream - /// - /// Reads in machine native format - public static UInt128 ReadUInt128(this BinaryReader reader) - { - if (BitConverter.IsLittleEndian) - return reader.ReadUInt128LittleEndian(); - else - return reader.ReadUInt128BigEndian(); - } - - /// - /// Read a UInt128 from the underlying stream - /// - /// Reads in big-endian format - public static UInt128 ReadUInt128BigEndian(this BinaryReader reader) - { - byte[] buffer = reader.ReadBytes(16); - return buffer.ToUInt128BigEndian(); - } - - /// - /// Read a UInt128 from the underlying stream - /// - /// Reads in little-endian format - public static UInt128 ReadUInt128LittleEndian(this BinaryReader reader) - { - byte[] buffer = reader.ReadBytes(16); - return buffer.ToUInt128LittleEndian(); - } -#endif - - /// - /// Reads in big-endian format - public static decimal ReadDecimalBigEndian(this BinaryReader reader) - { - byte[] buffer = reader.ReadBytes(16); - return buffer.ToDecimalBigEndian(); - } - - /// - /// Reads in little-endian format - public static decimal ReadDecimalLittleEndian(this BinaryReader reader) - { - byte[] buffer = reader.ReadBytes(16); - return buffer.ToDecimalLittleEndian(); - } - /// /// Read a null-terminated string from the underlying stream /// @@ -1004,1904 +448,5 @@ namespace SabreTools.IO.Extensions return [.. bytes]; } - - #endregion - - #region Peek Read - - /// - /// Peek a UInt8 from the base stream - /// - /// Only works properly on seekable streams - public static byte PeekByte(this BinaryReader reader) - { - byte value = reader.ReadByte(); - reader.BaseStream.SeekIfPossible(-1, SeekOrigin.Current); - return value; - } - - /// - /// Peek a UInt8 from the base stream - /// - /// Only works properly on seekable streams - public static byte PeekByteValue(this BinaryReader reader) - => reader.PeekByte(); - - /// - /// Peek a UInt8 from the base stream - /// - /// Reads in both-endian format - /// Only works properly on seekable streams - public static BothUInt8 PeekByteBothEndian(this BinaryReader reader) - { - BothUInt8 value = reader.ReadByteBothEndian(); - reader.BaseStream.SeekIfPossible(-2, SeekOrigin.Current); - return value; - } - - /// - /// Peek a UInt8[] from the base stream - /// - /// Only works properly on seekable streams - public static byte[] PeekBytes(this BinaryReader reader, int count) - { - byte[] value = reader.ReadBytes(count); - reader.BaseStream.SeekIfPossible(-count, SeekOrigin.Current); - return value; - } - - /// - /// Peek an Int8 from the base stream - /// - /// Only works properly on seekable streams - public static sbyte PeekSByte(this BinaryReader reader) - { - sbyte value = reader.ReadSByte(); - reader.BaseStream.SeekIfPossible(-1, SeekOrigin.Current); - return value; - } - - /// - /// Peek a Int8 from the base stream - /// - /// Reads in both-endian format - /// Only works properly on seekable streams - public static BothInt8 PeekSByteBothEndian(this BinaryReader reader) - { - BothInt8 value = reader.ReadSByteBothEndian(); - reader.BaseStream.SeekIfPossible(-2, SeekOrigin.Current); - return value; - } - - /// - /// Peek an Int16 from the base stream - /// - /// Reads in machine native format - /// Only works properly on seekable streams - public static short PeekInt16(this BinaryReader reader) - { - if (BitConverter.IsLittleEndian) - return reader.PeekInt16LittleEndian(); - else - return reader.PeekInt16BigEndian(); - } - - /// - /// Peek an Int16 from the base stream - /// - /// Reads in big-endian format - /// Only works properly on seekable streams - public static short PeekInt16BigEndian(this BinaryReader reader) - { - short value = reader.ReadInt16BigEndian(); - reader.BaseStream.SeekIfPossible(-2, SeekOrigin.Current); - return value; - } - - /// - /// Peek an Int16 from the base stream - /// - /// Reads in little-endian format - /// Only works properly on seekable streams - public static short PeekInt16LittleEndian(this BinaryReader reader) - { - short value = reader.ReadInt16LittleEndian(); - reader.BaseStream.SeekIfPossible(-2, SeekOrigin.Current); - return value; - } - - /// - /// Peek a Int16 from the base stream - /// - /// Reads in both-endian format - /// Only works properly on seekable streams - public static BothInt16 PeekInt16BothEndian(this BinaryReader reader) - { - BothInt16 value = reader.ReadInt16BothEndian(); - reader.BaseStream.SeekIfPossible(-4, SeekOrigin.Current); - return value; - } - - /// - /// Peek a UInt16 from the base stream - /// - /// Reads in machine native format - /// Only works properly on seekable streams - public static ushort PeekUInt16(this BinaryReader reader) - { - if (BitConverter.IsLittleEndian) - return reader.PeekUInt16LittleEndian(); - else - return reader.PeekUInt16BigEndian(); - } - - /// - /// Peek a UInt16 from the base stream - /// - /// Reads in big-endian format - /// Only works properly on seekable streams - public static ushort PeekUInt16BigEndian(this BinaryReader reader) - { - ushort value = reader.ReadUInt16BigEndian(); - reader.BaseStream.SeekIfPossible(-2, SeekOrigin.Current); - return value; - } - - /// - /// Peek a UInt16 from the base stream - /// - /// Reads in little-endian format - /// Only works properly on seekable streams - public static ushort PeekUInt16LittleEndian(this BinaryReader reader) - { - ushort value = reader.ReadUInt16LittleEndian(); - reader.BaseStream.SeekIfPossible(-2, SeekOrigin.Current); - return value; - } - - /// - /// Peek a UInt16 from the base stream - /// - /// Reads in both-endian format - /// Only works properly on seekable streams - public static BothUInt16 PeekUInt16BothEndian(this BinaryReader reader) - { - BothUInt16 value = reader.ReadUInt16BothEndian(); - reader.BaseStream.SeekIfPossible(-4, SeekOrigin.Current); - return value; - } - - /// - /// Peek a WORD (2-byte) from the base stream - /// - /// Reads in machine native format - /// Only works properly on seekable streams - public static ushort PeekWORD(this BinaryReader reader) - => reader.PeekUInt16(); - - /// - /// Peek a WORD (2-byte) from the base stream - /// - /// Reads in big-endian format - /// Only works properly on seekable streams - public static ushort PeekWORDBigEndian(this BinaryReader reader) - => reader.PeekUInt16BigEndian(); - - /// - /// Peek a WORD (2-byte) from the base stream - /// - /// Reads in little-endian format - /// Only works properly on seekable streams - public static ushort PeekWORDLittleEndian(this BinaryReader reader) - => reader.PeekUInt16LittleEndian(); - - /// - /// Peek a WORD (2-byte) from the base stream - /// - /// Reads in both-endian format - /// Only works properly on seekable streams - public static BothUInt16 PeekWORDBothEndian(this BinaryReader reader) - => reader.PeekUInt16BothEndian(); - -#if NET5_0_OR_GREATER - /// - /// Peek a Half from the base stream - /// - /// Reads in machine native format - /// Only works properly on seekable streams - public static Half PeekHalf(this BinaryReader reader) - { - if (BitConverter.IsLittleEndian) - return reader.PeekHalfLittleEndian(); - else - return reader.PeekHalfBigEndian(); - } - - /// - /// Peek a Half from the base stream - /// - /// Reads in big-endian format - /// Only works properly on seekable streams - public static Half PeekHalfBigEndian(this BinaryReader reader) - { - Half value = reader.ReadHalfBigEndian(); - reader.BaseStream.SeekIfPossible(-2, SeekOrigin.Current); - return value; - } - - /// - /// Peek a Half from the base stream - /// - /// Reads in little-endian format - /// Only works properly on seekable streams - public static Half PeekHalfLittleEndian(this BinaryReader reader) - { - Half value = reader.ReadHalfLittleEndian(); - reader.BaseStream.SeekIfPossible(-2, SeekOrigin.Current); - return value; - } -#endif - - /// - /// Peek an Int24 from the base stream - /// - /// Reads in machine native format - /// Only works properly on seekable streams - public static Int24 PeekInt24(this BinaryReader reader) - { - if (BitConverter.IsLittleEndian) - return reader.PeekInt24LittleEndian(); - else - return reader.PeekInt24BigEndian(); - } - - /// - /// Peek an Int24 from the base stream - /// - /// Reads in big-endian format - /// Only works properly on seekable streams - public static Int24 PeekInt24BigEndian(this BinaryReader reader) - { - Int24 value = reader.ReadInt24BigEndian(); - reader.BaseStream.SeekIfPossible(-3, SeekOrigin.Current); - return value; - } - - /// - /// Peek an Int24 from the base stream - /// - /// Reads in little-endian format - /// Only works properly on seekable streams - public static Int24 PeekInt24LittleEndian(this BinaryReader reader) - { - Int24 value = reader.ReadInt24LittleEndian(); - reader.BaseStream.SeekIfPossible(-3, SeekOrigin.Current); - return value; - } - - /// - /// Peek a UInt24 from the base stream - /// - /// Reads in machine native format - /// Only works properly on seekable streams - public static UInt24 PeekUInt24(this BinaryReader reader) - { - if (BitConverter.IsLittleEndian) - return reader.PeekUInt24LittleEndian(); - else - return reader.PeekUInt24BigEndian(); - } - - /// - /// Peek a UInt24 from the base stream - /// - /// Reads in big-endian format - /// Only works properly on seekable streams - public static UInt24 PeekUInt24BigEndian(this BinaryReader reader) - { - UInt24 value = reader.ReadUInt24BigEndian(); - reader.BaseStream.SeekIfPossible(-3, SeekOrigin.Current); - return value; - } - - /// - /// Peek a UInt24 from the base stream - /// - /// Reads in little-endian format - /// Only works properly on seekable streams - public static UInt24 PeekUInt24LittleEndian(this BinaryReader reader) - { - UInt24 value = reader.ReadUInt24LittleEndian(); - reader.BaseStream.SeekIfPossible(-3, SeekOrigin.Current); - return value; - } - - /// - /// Peek an Int32 from the base stream - /// - /// Reads in machine native format - /// Only works properly on seekable streams - public static int PeekInt32(this BinaryReader reader) - { - if (BitConverter.IsLittleEndian) - return reader.PeekInt32LittleEndian(); - else - return reader.PeekInt32BigEndian(); - } - - /// - /// Peek an Int32 from the base stream - /// - /// Reads in big-endian format - /// Only works properly on seekable streams - public static int PeekInt32BigEndian(this BinaryReader reader) - { - int value = reader.ReadInt32BigEndian(); - reader.BaseStream.SeekIfPossible(-4, SeekOrigin.Current); - return value; - } - - /// - /// Peek an Int32 from the base stream - /// - /// Reads in little-endian format - /// Only works properly on seekable streams - public static int PeekInt32LittleEndian(this BinaryReader reader) - { - int value = reader.ReadInt32LittleEndian(); - reader.BaseStream.SeekIfPossible(-4, SeekOrigin.Current); - return value; - } - - /// - /// Peek a Int32 from the base stream - /// - /// Reads in both-endian format - /// Only works properly on seekable streams - public static BothInt32 PeekInt32BothEndian(this BinaryReader reader) - { - BothInt32 value = reader.ReadInt32BothEndian(); - reader.BaseStream.SeekIfPossible(-8, SeekOrigin.Current); - return value; - } - - /// - /// Peek a UInt32 from the base stream - /// - /// Reads in machine native format - /// Only works properly on seekable streams - public static uint PeekUInt32(this BinaryReader reader) - { - if (BitConverter.IsLittleEndian) - return reader.PeekUInt32LittleEndian(); - else - return reader.PeekUInt32BigEndian(); - } - - /// - /// Peek a UInt32 from the base stream - /// - /// Reads in big-endian format - /// Only works properly on seekable streams - public static uint PeekUInt32BigEndian(this BinaryReader reader) - { - uint value = reader.ReadUInt32BigEndian(); - reader.BaseStream.SeekIfPossible(-4, SeekOrigin.Current); - return value; - } - - /// - /// Peek a UInt32 from the base stream - /// - /// Reads in little-endian format - /// Only works properly on seekable streams - public static uint PeekUInt32LittleEndian(this BinaryReader reader) - { - uint value = reader.ReadUInt32LittleEndian(); - reader.BaseStream.SeekIfPossible(-4, SeekOrigin.Current); - return value; - } - - /// - /// Peek a UInt32 from the base stream - /// - /// Reads in both-endian format - /// Only works properly on seekable streams - public static BothUInt32 PeekUInt32BothEndian(this BinaryReader reader) - { - BothUInt32 value = reader.ReadUInt32BothEndian(); - reader.BaseStream.SeekIfPossible(-8, SeekOrigin.Current); - return value; - } - - /// - /// Peek a DWORD (4-byte) from the base stream - /// - /// Reads in machine native format - /// Only works properly on seekable streams - public static uint PeekDWORD(this BinaryReader reader) - => reader.PeekUInt32(); - - /// - /// Peek a DWORD (4-byte) from the base stream - /// - /// Reads in big-endian format - /// Only works properly on seekable streams - public static uint PeekDWORDBigEndian(this BinaryReader reader) - => reader.PeekUInt32BigEndian(); - - /// - /// Peek a DWORD (4-byte) from the base stream - /// - /// Reads in little-endian format - /// Only works properly on seekable streams - public static uint PeekDWORDLittleEndian(this BinaryReader reader) - => reader.PeekUInt32LittleEndian(); - - /// - /// Peek a DWORD (4-byte) from the base stream - /// - /// Reads in both-endian format - /// Only works properly on seekable streams - public static BothUInt32 PeekDWORDBothEndian(this BinaryReader reader) - => reader.PeekUInt32BothEndian(); - - /// - /// Peek a Single from the base stream - /// - /// Reads in machine native format - /// Only works properly on seekable streams - public static float PeekSingle(this BinaryReader reader) - { - if (BitConverter.IsLittleEndian) - return reader.PeekSingleLittleEndian(); - else - return reader.PeekSingleBigEndian(); - } - - /// - /// Peek a Single from the base stream - /// - /// Reads in big-endian format - /// Only works properly on seekable streams - public static float PeekSingleBigEndian(this BinaryReader reader) - { - float value = reader.ReadSingleBigEndian(); - reader.BaseStream.SeekIfPossible(-4, SeekOrigin.Current); - return value; - } - - /// - /// Peek a Single from the base stream - /// - /// Reads in little-endian format - /// Only works properly on seekable streams - public static float PeekSingleLittleEndian(this BinaryReader reader) - { - float value = reader.ReadSingleLittleEndian(); - reader.BaseStream.SeekIfPossible(-4, SeekOrigin.Current); - return value; - } - - /// - /// Peek an Int48 from the base stream - /// - /// Reads in machine native format - /// Only works properly on seekable streams - public static Int48 PeekInt48(this BinaryReader reader) - { - if (BitConverter.IsLittleEndian) - return reader.PeekInt48LittleEndian(); - else - return reader.PeekInt48BigEndian(); - } - - /// - /// Peek an Int48 from the base stream - /// - /// Reads in big-endian format - /// Only works properly on seekable streams - public static Int48 PeekInt48BigEndian(this BinaryReader reader) - { - Int48 value = reader.ReadInt48BigEndian(); - reader.BaseStream.SeekIfPossible(-6, SeekOrigin.Current); - return value; - } - - /// - /// Peek an Int48 from the base stream - /// - /// Reads in little-endian format - /// Only works properly on seekable streams - public static Int48 PeekInt48LittleEndian(this BinaryReader reader) - { - Int48 value = reader.ReadInt48LittleEndian(); - reader.BaseStream.SeekIfPossible(-6, SeekOrigin.Current); - return value; - } - - /// - /// Peek a UInt48 from the base stream - /// - /// Reads in machine native format - /// Only works properly on seekable streams - public static UInt48 PeekUInt48(this BinaryReader reader) - { - if (BitConverter.IsLittleEndian) - return reader.PeekUInt48LittleEndian(); - else - return reader.PeekUInt48BigEndian(); - } - - /// - /// Peek an UInt48 from the base stream - /// - /// Reads in big-endian format - /// Only works properly on seekable streams - public static UInt48 PeekUInt48BigEndian(this BinaryReader reader) - { - UInt48 value = reader.ReadUInt48BigEndian(); - reader.BaseStream.SeekIfPossible(-6, SeekOrigin.Current); - return value; - } - - /// - /// Peek an UInt48 from the base stream - /// - /// Reads in little-endian format - /// Only works properly on seekable streams - public static UInt48 PeekUInt48LittleEndian(this BinaryReader reader) - { - UInt48 value = reader.ReadUInt48LittleEndian(); - reader.BaseStream.SeekIfPossible(-6, SeekOrigin.Current); - return value; - } - - /// - /// Peek an Int64 from the base stream - /// - /// Reads in machine native format - /// Only works properly on seekable streams - public static long PeekInt64(this BinaryReader reader) - { - if (BitConverter.IsLittleEndian) - return reader.PeekInt64LittleEndian(); - else - return reader.PeekInt64BigEndian(); - } - - /// - /// Peek an Int64 from the base stream - /// - /// Reads in big-endian format - /// Only works properly on seekable streams - public static long PeekInt64BigEndian(this BinaryReader reader) - { - long value = reader.ReadInt64BigEndian(); - reader.BaseStream.SeekIfPossible(-8, SeekOrigin.Current); - return value; - } - - /// - /// Peek an Int64 from the base stream - /// - /// Reads in big-endian format - /// Only works properly on seekable streams - public static long PeekInt64LittleEndian(this BinaryReader reader) - { - long value = reader.ReadInt64LittleEndian(); - reader.BaseStream.SeekIfPossible(-8, SeekOrigin.Current); - return value; - } - - /// - /// Peek a Int64 from the base stream - /// - /// Reads in both-endian format - /// Only works properly on seekable streams - public static BothInt64 PeekInt64BothEndian(this BinaryReader reader) - { - BothInt64 value = reader.ReadInt64BothEndian(); - reader.BaseStream.SeekIfPossible(-16, SeekOrigin.Current); - return value; - } - - /// - /// Peek a UInt64 from the base stream - /// - /// Reads in machine native format - /// Only works properly on seekable streams - public static ulong PeekUInt64(this BinaryReader reader) - { - if (BitConverter.IsLittleEndian) - return reader.PeekUInt64LittleEndian(); - else - return reader.PeekUInt64BigEndian(); - } - - /// - /// Peek a UInt64 from the base stream - /// - /// Reads in big-endian format - /// Only works properly on seekable streams - public static ulong PeekUInt64BigEndian(this BinaryReader reader) - { - ulong value = reader.ReadUInt64BigEndian(); - reader.BaseStream.SeekIfPossible(-8, SeekOrigin.Current); - return value; - } - - /// - /// Peek a UInt64 from the base stream - /// - /// Reads in little-endian format - /// Only works properly on seekable streams - public static ulong PeekUInt64LittleEndian(this BinaryReader reader) - { - ulong value = reader.ReadUInt64LittleEndian(); - reader.BaseStream.SeekIfPossible(-8, SeekOrigin.Current); - return value; - } - - /// - /// Peek a UInt64 from the base stream - /// - /// Reads in both-endian format - /// Only works properly on seekable streams - public static BothUInt64 PeekUInt64BothEndian(this BinaryReader reader) - { - BothUInt64 value = reader.ReadUInt64BothEndian(); - reader.BaseStream.SeekIfPossible(-16, SeekOrigin.Current); - return value; - } - - /// - /// Peek a QWORD (8-byte) from the base stream - /// - /// Reads in machine native format - /// Only works properly on seekable streams - public static ulong PeekQWORD(this BinaryReader reader) - => reader.PeekUInt64(); - - /// - /// Peek a QWORD (8-byte) from the base stream - /// - /// Reads in big-endian format - /// Only works properly on seekable streams - public static ulong PeekQWORDBigEndian(this BinaryReader reader) - => reader.PeekUInt64BigEndian(); - - /// - /// Peek a QWORD (8-byte) from the base stream - /// - /// Reads in little-endian format - /// Only works properly on seekable streams - public static ulong PeekQWORDLittleEndian(this BinaryReader reader) - => reader.PeekUInt64LittleEndian(); - - /// - /// Peek a QWORD (8-byte) from the base stream - /// - /// Reads in both-endian format - /// Only works properly on seekable streams - public static BothUInt64 PeekQWORDBothEndian(this BinaryReader reader) - => reader.PeekUInt64BothEndian(); - - /// - /// Peek a Double from the base stream - /// - /// Reads in machine native format - /// Only works properly on seekable streams - public static double PeekDouble(this BinaryReader reader) - { - if (BitConverter.IsLittleEndian) - return reader.PeekDoubleLittleEndian(); - else - return reader.PeekDoubleBigEndian(); - } - - /// - /// Peek a Double from the base stream - /// - /// Reads in big-endian format - /// Only works properly on seekable streams - public static double PeekDoubleBigEndian(this BinaryReader reader) - { - double value = reader.ReadDoubleBigEndian(); - reader.BaseStream.SeekIfPossible(-8, SeekOrigin.Current); - return value; - } - - /// - /// Peek a Double from the base stream - /// - /// Reads in little-endian format - /// Only works properly on seekable streams - public static double PeekDoubleLittleEndian(this BinaryReader reader) - { - double value = reader.ReadDoubleLittleEndian(); - reader.BaseStream.SeekIfPossible(-8, SeekOrigin.Current); - return value; - } - - /// - /// Peek a Guid from the base stream - /// - /// Reads in machine native format - /// Only works properly on seekable streams - public static Guid PeekGuid(this BinaryReader reader) - { - if (BitConverter.IsLittleEndian) - return reader.PeekGuidLittleEndian(); - else - return reader.PeekGuidBigEndian(); - } - - /// - /// Peek a Guid from the base stream - /// - /// Reads in big-endian format - /// Only works properly on seekable streams - public static Guid PeekGuidBigEndian(this BinaryReader reader) - { - Guid value = reader.ReadGuidBigEndian(); - reader.BaseStream.SeekIfPossible(-16, SeekOrigin.Current); - return value; - } - - /// - /// Peek a Guid from the base stream - /// - /// Reads in little-endian format - /// Only works properly on seekable streams - public static Guid PeekGuidLittleEndian(this BinaryReader reader) - { - Guid value = reader.ReadGuidLittleEndian(); - reader.BaseStream.SeekIfPossible(-16, SeekOrigin.Current); - return value; - } - -#if NET7_0_OR_GREATER - /// - /// Peek an Int128 from the base stream - /// - /// Reads in machine native format - /// Only works properly on seekable streams - public static Int128 PeekInt128(this BinaryReader reader) - { - if (BitConverter.IsLittleEndian) - return reader.PeekInt128LittleEndian(); - else - return reader.PeekInt128BigEndian(); - } - - /// - /// Peek an Int128 from the base stream - /// - /// Reads in big-endian format - /// Only works properly on seekable streams - public static Int128 PeekInt128BigEndian(this BinaryReader reader) - { - Int128 value = reader.ReadInt128BigEndian(); - reader.BaseStream.SeekIfPossible(-16, SeekOrigin.Current); - return value; - } - - /// - /// Peek an Int128 from the base stream - /// - /// Reads in little-endian format - /// Only works properly on seekable streams - public static Int128 PeekInt128LittleEndian(this BinaryReader reader) - { - Int128 value = reader.ReadInt128LittleEndian(); - reader.BaseStream.SeekIfPossible(-16, SeekOrigin.Current); - return value; - } - - /// - /// Peek a UInt128 from the base stream - /// - /// Reads in machine native format - /// Only works properly on seekable streams - public static UInt128 PeekUInt128(this BinaryReader reader) - { - if (BitConverter.IsLittleEndian) - return reader.PeekUInt128LittleEndian(); - else - return reader.PeekUInt128BigEndian(); - } - - /// - /// Peek a UInt128 from the base stream - /// - /// Reads in big-endian format - /// Only works properly on seekable streams - public static UInt128 PeekUInt128BigEndian(this BinaryReader reader) - { - UInt128 value = reader.ReadUInt128BigEndian(); - reader.BaseStream.SeekIfPossible(-16, SeekOrigin.Current); - return value; - } - - /// - /// Peek a UInt128 from the base stream - /// - /// Reads in little-endian format - /// Only works properly on seekable streams - public static UInt128 PeekUInt128LittleEndian(this BinaryReader reader) - { - UInt128 value = reader.ReadUInt128LittleEndian(); - reader.BaseStream.SeekIfPossible(-16, SeekOrigin.Current); - return value; - } -#endif - - /// - /// Peek a Decimal from the base stream - /// - /// Reads in machine native format - /// Only works properly on seekable streams - public static decimal PeekDecimal(this BinaryReader reader) - { - if (BitConverter.IsLittleEndian) - return reader.PeekDecimalLittleEndian(); - else - return reader.PeekDecimalBigEndian(); - } - - /// - /// Peek a Decimal from the base stream - /// - /// Reads in big-endian format - /// Only works properly on seekable streams - public static decimal PeekDecimalBigEndian(this BinaryReader reader) - { - decimal value = reader.ReadDecimalBigEndian(); - reader.BaseStream.SeekIfPossible(-16, SeekOrigin.Current); - return value; - } - - /// - /// Peek a Decimal from the base stream - /// - /// Reads in little-endian format - /// Only works properly on seekable streams - public static decimal PeekDecimalLittleEndian(this BinaryReader reader) - { - decimal value = reader.ReadDecimalLittleEndian(); - reader.BaseStream.SeekIfPossible(-16, SeekOrigin.Current); - return value; - } - - #endregion - - #region Try Read - - /// - /// Read a UInt8 from the base stream - /// - public static bool TryReadByteValue(this BinaryReader reader, out byte value) - { - if (reader.BaseStream.Position > reader.BaseStream.Length - 1) - { - value = default; - return false; - } - - value = reader.ReadByte(); - return true; - } - - /// - /// Read a UInt8 from the base stream - /// - /// Reads in both-endian format - public static bool TryReadByteBothEndian(this BinaryReader reader, out BothUInt8 value) - { - if (reader.BaseStream.Position > reader.BaseStream.Length - 2) - { - value = default(byte); - return false; - } - - value = reader.ReadByteBothEndian(); - return true; - } - - /// - /// Read a UInt8[] from the base stream - /// - public static bool TryReadBytes(this BinaryReader reader, int count, out byte[] value) - { - if (reader.BaseStream.Position > reader.BaseStream.Length - count) - { - value = []; - return false; - } - - value = reader.ReadBytes(count); - return true; - } - - /// - /// Read an Int8 from the base stream - /// - public static bool TryReadSByte(this BinaryReader reader, out sbyte value) - { - if (reader.BaseStream.Position > reader.BaseStream.Length - 1) - { - value = default; - return false; - } - - value = reader.ReadSByte(); - return true; - } - - /// - /// Read a Int8 from the base stream - /// - /// Reads in both-endian format - public static bool TryReadSByteBothEndian(this BinaryReader reader, out BothInt8 value) - { - if (reader.BaseStream.Position > reader.BaseStream.Length - 2) - { - value = default(sbyte); - return false; - } - - value = reader.ReadSByteBothEndian(); - return true; - } - - /// - /// Read a Char from the base stream - /// - public static bool TryReadChar(this BinaryReader reader, out char value) - { - if (reader.BaseStream.Position > reader.BaseStream.Length - 1) - { - value = default; - return false; - } - - value = reader.ReadChar(); - return true; - } - - /// - /// Read an Int16 from the base stream - /// - /// Reads in machine native format - public static bool TryReadInt16(this BinaryReader reader, out short value) - { - if (BitConverter.IsLittleEndian) - return reader.TryReadInt16LittleEndian(out value); - else - return reader.TryReadInt16BigEndian(out value); - } - - /// - /// Read an Int16 from the base stream - /// - /// Reads in big-endian format - public static bool TryReadInt16BigEndian(this BinaryReader reader, out short value) - { - if (reader.BaseStream.Position > reader.BaseStream.Length - 2) - { - value = default; - return false; - } - - value = reader.ReadInt16BigEndian(); - return true; - } - - /// - /// Read an Int16 from the base stream - /// - /// Reads in little-endian format - public static bool TryReadInt16LittleEndian(this BinaryReader reader, out short value) - { - if (reader.BaseStream.Position > reader.BaseStream.Length - 2) - { - value = default; - return false; - } - - value = reader.ReadInt16LittleEndian(); - return true; - } - - /// - /// Read a Int16 from the base stream - /// - /// Reads in both-endian format - public static bool TryReadInt16BothEndian(this BinaryReader reader, out BothInt16 value) - { - if (reader.BaseStream.Position > reader.BaseStream.Length - 4) - { - value = default(short); - return false; - } - - value = reader.ReadInt16BothEndian(); - return true; - } - - /// - /// Read a UInt16 from the base stream - /// - /// Reads in machine native format - public static bool TryReadUInt16(this BinaryReader reader, out ushort value) - { - if (BitConverter.IsLittleEndian) - return reader.TryReadUInt16LittleEndian(out value); - else - return reader.TryReadUInt16BigEndian(out value); - } - - /// - /// Read a UInt16 from the base stream - /// - /// Reads in big-endian format - public static bool TryReadUInt16BigEndian(this BinaryReader reader, out ushort value) - { - if (reader.BaseStream.Position > reader.BaseStream.Length - 2) - { - value = default; - return false; - } - - value = reader.ReadUInt16BigEndian(); - return true; - } - - /// - /// Read a UInt16 from the base stream - /// - /// Reads in little-endian format - public static bool TryReadUInt16LittleEndian(this BinaryReader reader, out ushort value) - { - if (reader.BaseStream.Position > reader.BaseStream.Length - 2) - { - value = default; - return false; - } - - value = reader.ReadUInt16LittleEndian(); - return true; - } - - /// - /// Read a UInt16 from the base stream - /// - /// Reads in both-endian format - public static bool TryReadUInt16BothEndian(this BinaryReader reader, out BothUInt16 value) - { - if (reader.BaseStream.Position > reader.BaseStream.Length - 4) - { - value = default(ushort); - return false; - } - - value = reader.ReadUInt16BothEndian(); - return true; - } - - /// - /// Read a WORD (2-byte) from the base stream - /// - /// Reads in machine native format - public static bool TryReadWORD(this BinaryReader reader, out ushort value) - => reader.TryReadUInt16(out value); - - /// - /// Read a WORD (2-byte) from the base stream - /// - /// Reads in big-endian format - public static bool TryReadWORDBigEndian(this BinaryReader reader, out ushort value) - => reader.TryReadUInt16BigEndian(out value); - - /// - /// Read a WORD (2-byte) from the base stream - /// - /// Reads in little-endian format - public static bool TryReadWORDLittleEndian(this BinaryReader reader, out ushort value) - => reader.TryReadUInt16LittleEndian(out value); - - /// - /// Read a WORD (2-byte) from the base stream - /// - /// Reads in both-endian format - public static bool TryReadWORDBothEndian(this BinaryReader reader, out BothUInt16 value) - => reader.TryReadUInt16BothEndian(out value); - -#if NET5_0_OR_GREATER - /// - /// Read a Half from the base stream - /// - /// Reads in machine native format - public static bool TryReadHalf(this BinaryReader reader, out Half value) - { - if (BitConverter.IsLittleEndian) - return reader.TryReadHalfLittleEndian(out value); - else - return reader.TryReadHalfBigEndian(out value); - } - - /// - /// Read a Half from the base stream - /// - /// Reads in big-endian format - public static bool TryReadHalfBigEndian(this BinaryReader reader, out Half value) - { - if (reader.BaseStream.Position > reader.BaseStream.Length - 2) - { - value = default; - return false; - } - - value = reader.ReadHalfBigEndian(); - return true; - } - - /// - /// Read a Half from the base stream - /// - /// Reads in little-endian format - public static bool TryReadHalfLittleEndian(this BinaryReader reader, out Half value) - { - if (reader.BaseStream.Position > reader.BaseStream.Length - 2) - { - value = default; - return false; - } - - value = reader.ReadHalfLittleEndian(); - return true; - } -#endif - - /// - /// Read an Int24 from the base stream - /// - /// Reads in machine native format - public static bool TryReadInt24(this BinaryReader reader, out Int24 value) - { - if (BitConverter.IsLittleEndian) - return reader.TryReadInt24LittleEndian(out value); - else - return reader.TryReadInt24BigEndian(out value); - } - - /// - /// Read an Int24 from the base stream - /// - /// Reads in big-endian format - public static bool TryReadInt24BigEndian(this BinaryReader reader, out Int24 value) - { - if (reader.BaseStream.Position > reader.BaseStream.Length - 3) - { - value = new Int24(); - return false; - } - - value = reader.ReadInt24BigEndian(); - return true; - } - - /// - /// Read an Int24 from the base stream - /// - /// Reads in little-endian format - public static bool TryReadInt24LittleEndian(this BinaryReader reader, out Int24 value) - { - if (reader.BaseStream.Position > reader.BaseStream.Length - 3) - { - value = new Int24(); - return false; - } - - value = reader.ReadInt24LittleEndian(); - return true; - } - - /// - /// Read a UInt24 from the base stream - /// - /// Reads in machine native format - public static bool TryReadUInt24(this BinaryReader reader, out UInt24 value) - { - if (BitConverter.IsLittleEndian) - return reader.TryReadUInt24LittleEndian(out value); - else - return reader.TryReadUInt24BigEndian(out value); - } - - /// - /// Read a UInt24 from the base stream - /// - /// Reads in big-endian format - public static bool TryReadUInt24BigEndian(this BinaryReader reader, out UInt24 value) - { - if (reader.BaseStream.Position > reader.BaseStream.Length - 3) - { - value = new UInt24(); - return false; - } - - value = reader.ReadUInt24BigEndian(); - return true; - } - - /// - /// Read a UInt24 from the base stream - /// - /// Reads in little-endian format - public static bool TryReadUInt24LittleEndian(this BinaryReader reader, out UInt24 value) - { - if (reader.BaseStream.Position > reader.BaseStream.Length - 3) - { - value = new UInt24(); - return false; - } - - value = reader.ReadUInt24LittleEndian(); - return true; - } - - /// - /// Read an Int32 from the base stream - /// - /// Reads in machine native format - public static bool TryReadInt32(this BinaryReader reader, out int value) - { - if (BitConverter.IsLittleEndian) - return reader.TryReadInt32LittleEndian(out value); - else - return reader.TryReadInt32BigEndian(out value); - } - - /// - /// Read an Int32 from the base stream - /// - /// Reads in big-endian format - public static bool TryReadInt32BigEndian(this BinaryReader reader, out int value) - { - if (reader.BaseStream.Position > reader.BaseStream.Length - 4) - { - value = default; - return false; - } - - value = reader.ReadInt32BigEndian(); - return true; - } - - /// - /// Read an Int32 from the base stream - /// - /// Reads in little-endian format - public static bool TryReadInt32LittleEndian(this BinaryReader reader, out int value) - { - if (reader.BaseStream.Position > reader.BaseStream.Length - 4) - { - value = default; - return false; - } - - value = reader.ReadInt32LittleEndian(); - return true; - } - - /// - /// Read a Int32 from the base stream - /// - /// Reads in both-endian format - public static bool TryReadInt32BothEndian(this BinaryReader reader, out BothInt32 value) - { - if (reader.BaseStream.Position > reader.BaseStream.Length - 8) - { - value = default(int); - return false; - } - - value = reader.ReadInt32BothEndian(); - return true; - } - - /// - /// Read a UInt32 from the base stream - /// - /// Reads in machine native format - public static bool TryReadUInt32(this BinaryReader reader, out uint value) - { - if (BitConverter.IsLittleEndian) - return reader.TryReadUInt32LittleEndian(out value); - else - return reader.TryReadUInt32BigEndian(out value); - } - - /// - /// Read a UInt32 from the base stream - /// - /// Reads in big-endian format - public static bool TryReadUInt32BigEndian(this BinaryReader reader, out uint value) - { - if (reader.BaseStream.Position > reader.BaseStream.Length - 4) - { - value = default; - return false; - } - - value = reader.ReadUInt32BigEndian(); - return true; - } - - /// - /// Read a UInt32 from the base stream - /// - /// Reads in little-endian format - public static bool TryReadUInt32LittleEndian(this BinaryReader reader, out uint value) - { - if (reader.BaseStream.Position > reader.BaseStream.Length - 4) - { - value = default; - return false; - } - - value = reader.ReadUInt32LittleEndian(); - return true; - } - - /// - /// Read a UInt32 from the base stream - /// - /// Reads in both-endian format - public static bool TryReadUInt32BothEndian(this BinaryReader reader, out BothUInt32 value) - { - if (reader.BaseStream.Position > reader.BaseStream.Length - 8) - { - value = default(uint); - return false; - } - - value = reader.ReadUInt32BothEndian(); - return true; - } - - /// - /// Read a DWORD (4-byte) from the base stream - /// - /// Reads in machine native format - public static bool TryReadDWORD(this BinaryReader reader, out uint value) - => reader.TryReadUInt32(out value); - - /// - /// Read a DWORD (4-byte) from the base stream - /// - /// Reads in big-endian format - public static bool TryReadDWORDBigEndian(this BinaryReader reader, out uint value) - => reader.TryReadUInt32BigEndian(out value); - - /// - /// Read a DWORD (4-byte) from the base stream - /// - /// Reads in little-endian format - public static bool TryReadDWORDLittleEndian(this BinaryReader reader, out uint value) - => reader.TryReadUInt32LittleEndian(out value); - - /// - /// Read a DWORD (4-byte) from the base stream - /// - /// Reads in both-endian format - public static bool TryReadDWORDBothEndian(this BinaryReader reader, out BothUInt32 value) - => reader.TryReadUInt32BothEndian(out value); - - /// - /// Read a Single from the base stream - /// - /// Reads in machine native format - public static bool TryReadSingle(this BinaryReader reader, out float value) - { - if (BitConverter.IsLittleEndian) - return reader.TryReadSingleLittleEndian(out value); - else - return reader.TryReadSingleBigEndian(out value); - } - - /// - /// Read a Single from the base stream - /// - /// Reads in big-endian format - public static bool TryReadSingleBigEndian(this BinaryReader reader, out float value) - { - if (reader.BaseStream.Position > reader.BaseStream.Length - 4) - { - value = default; - return false; - } - - value = reader.ReadSingleBigEndian(); - return true; - } - - /// - /// Read a Single from the base stream - /// - /// Reads in little-endian format - public static bool TryReadSingleLittleEndian(this BinaryReader reader, out float value) - { - if (reader.BaseStream.Position > reader.BaseStream.Length - 4) - { - value = default; - return false; - } - - value = reader.ReadSingleLittleEndian(); - return true; - } - - /// - /// Read an Int48 from the base stream - /// - /// Reads in machine native format - public static bool TryReadInt48(this BinaryReader reader, out Int48 value) - { - if (BitConverter.IsLittleEndian) - return reader.TryReadInt48LittleEndian(out value); - else - return reader.TryReadInt48BigEndian(out value); - } - - /// - /// Read an Int48 from the base stream - /// - /// Reads in big-endian format - public static bool TryReadInt48BigEndian(this BinaryReader reader, out Int48 value) - { - if (reader.BaseStream.Position > reader.BaseStream.Length - 6) - { - value = new Int48(); - return false; - } - - value = reader.ReadInt48BigEndian(); - return true; - } - - /// - /// Read an Int48 from the base stream - /// - /// Reads in little-endian format - public static bool TryReadInt48LittleEndian(this BinaryReader reader, out Int48 value) - { - if (reader.BaseStream.Position > reader.BaseStream.Length - 6) - { - value = new Int48(); - return false; - } - - value = reader.ReadInt48LittleEndian(); - return true; - } - - /// - /// Read a UInt48 from the base stream - /// - /// Reads in machine native format - public static bool TryReadUInt48(this BinaryReader reader, out UInt48 value) - { - if (BitConverter.IsLittleEndian) - return reader.TryReadUInt48LittleEndian(out value); - else - return reader.TryReadUInt48BigEndian(out value); - } - - /// - /// Read a UInt48 from the base stream - /// - /// Reads in big-endian format - public static bool TryReadUInt48BigEndian(this BinaryReader reader, out UInt48 value) - { - if (reader.BaseStream.Position > reader.BaseStream.Length - 6) - { - value = new UInt48(); - return false; - } - - value = reader.ReadUInt48BigEndian(); - return true; - } - - /// - /// Read an UInt48 from the base stream - /// - /// Reads in little-endian format - public static bool TryReadUInt48LittleEndian(this BinaryReader reader, out UInt48 value) - { - if (reader.BaseStream.Position > reader.BaseStream.Length - 6) - { - value = new UInt48(); - return false; - } - - value = reader.ReadUInt48LittleEndian(); - return true; - } - - /// - /// Read an Int64 from the base stream - /// - /// Reads in machine native format - public static bool TryReadInt64(this BinaryReader reader, out long value) - { - if (BitConverter.IsLittleEndian) - return reader.TryReadInt64LittleEndian(out value); - else - return reader.TryReadInt64BigEndian(out value); - } - - /// - /// Read an Int64 from the base stream - /// - /// Reads in big-endian format - public static bool TryReadInt64BigEndian(this BinaryReader reader, out long value) - { - if (reader.BaseStream.Position > reader.BaseStream.Length - 8) - { - value = default; - return false; - } - - value = reader.ReadInt64BigEndian(); - return true; - } - - /// - /// Read an Int64 from the base stream - /// - /// Reads in little-endian format - public static bool TryReadInt64LittleEndian(this BinaryReader reader, out long value) - { - if (reader.BaseStream.Position > reader.BaseStream.Length - 8) - { - value = default; - return false; - } - - value = reader.ReadInt64BigEndian(); - return true; - } - - /// - /// Read a Int64 from the base stream - /// - /// Reads in both-endian format - public static bool TryReadInt64BothEndian(this BinaryReader reader, out BothInt64 value) - { - if (reader.BaseStream.Position > reader.BaseStream.Length - 16) - { - value = default(long); - return false; - } - - value = reader.ReadInt64BothEndian(); - return true; - } - - /// - /// Read a UInt64 from the base stream - /// - /// Reads in machine native format - public static bool TryReadUInt64(this BinaryReader reader, out ulong value) - { - if (BitConverter.IsLittleEndian) - return reader.TryReadUInt64LittleEndian(out value); - else - return reader.TryReadUInt64BigEndian(out value); - } - - /// - /// Read a UInt64 from the base stream - /// - /// Reads in big-endian format - public static bool TryReadUInt64BigEndian(this BinaryReader reader, out ulong value) - { - if (reader.BaseStream.Position > reader.BaseStream.Length - 8) - { - value = default; - return false; - } - - value = reader.ReadUInt64BigEndian(); - return true; - } - - /// - /// Read a UInt64 from the base stream - /// - /// Reads in little-endian format - public static bool TryReadUInt64LittleEndian(this BinaryReader reader, out ulong value) - { - if (reader.BaseStream.Position > reader.BaseStream.Length - 8) - { - value = default; - return false; - } - - value = reader.ReadUInt64LittleEndian(); - return true; - } - - /// - /// Read a UInt64 from the base stream - /// - /// Reads in both-endian format - public static bool TryReadUInt64BothEndian(this BinaryReader reader, out BothUInt64 value) - { - if (reader.BaseStream.Position > reader.BaseStream.Length - 16) - { - value = default(ulong); - return false; - } - - value = reader.ReadUInt64BothEndian(); - return true; - } - - /// - /// Read a QWORD (8-byte) from the base stream - /// - /// Reads in machine native format - public static bool TryReadQWORD(this BinaryReader reader, out ulong value) - => reader.TryReadUInt64(out value); - - /// - /// Read a QWORD (8-byte) from the base stream - /// - /// Reads in big-endian format - public static bool TryReadQWORDBigEndian(this BinaryReader reader, out ulong value) - => reader.TryReadUInt64BigEndian(out value); - - /// - /// Read a QWORD (8-byte) from the base stream - /// - /// Reads in little-endian format - public static bool TryReadQWORDLittleEndian(this BinaryReader reader, out ulong value) - => reader.TryReadUInt64LittleEndian(out value); - - /// - /// Read a QWORD (8-byte) from the base stream - /// - /// Reads in both-endian format - public static bool TryReadQWORDBothEndian(this BinaryReader reader, out BothUInt64 value) - => reader.TryReadUInt64BothEndian(out value); - - /// - /// Read a Double from the base stream - /// - /// Reads in machine native format - public static bool TryReadDouble(this BinaryReader reader, out double value) - { - if (BitConverter.IsLittleEndian) - return reader.TryReadDoubleLittleEndian(out value); - else - return reader.TryReadDoubleBigEndian(out value); - } - - /// - /// Read a Double from the base stream - /// - /// Reads in big-endian format - public static bool TryReadDoubleBigEndian(this BinaryReader reader, out double value) - { - if (reader.BaseStream.Position > reader.BaseStream.Length - 8) - { - value = default; - return false; - } - - value = reader.ReadDoubleBigEndian(); - return true; - } - - /// - /// Read a Double from the base stream - /// - /// Reads in little-endian format - public static bool TryReadDoubleLittleEndian(this BinaryReader reader, out double value) - { - if (reader.BaseStream.Position > reader.BaseStream.Length - 8) - { - value = default; - return false; - } - - value = reader.ReadDoubleLittleEndian(); - return true; - } - - /// - /// Read a Guid from the base stream - /// - /// Reads in machine native format - public static bool TryReadGuid(this BinaryReader reader, out Guid value) - { - if (BitConverter.IsLittleEndian) - return reader.TryReadGuidLittleEndian(out value); - else - return reader.TryReadGuidBigEndian(out value); - } - - /// - /// Read a Guid from the base stream - /// - /// Reads in big-endian format - public static bool TryReadGuidBigEndian(this BinaryReader reader, out Guid value) - { - if (reader.BaseStream.Position > reader.BaseStream.Length - 16) - { - value = default; - return false; - } - - value = reader.ReadGuidBigEndian(); - return true; - } - - /// - /// Read a Guid from the base stream - /// - /// Reads in big-endian format - public static bool TryReadGuidLittleEndian(this BinaryReader reader, out Guid value) - { - if (reader.BaseStream.Position > reader.BaseStream.Length - 16) - { - value = default; - return false; - } - - value = reader.ReadGuidLittleEndian(); - return true; - } - -#if NET7_0_OR_GREATER - /// - /// Read an Int128 from the base stream - /// - /// Reads in machine native format - public static bool TryReadInt128(this BinaryReader reader, out Int128 value) - { - if (BitConverter.IsLittleEndian) - return reader.TryReadInt128LittleEndian(out value); - else - return reader.TryReadInt128BigEndian(out value); - } - - /// - /// Read an Int128 from the base stream - /// - /// Reads in big-endian format - public static bool TryReadInt128BigEndian(this BinaryReader reader, out Int128 value) - { - if (reader.BaseStream.Position > reader.BaseStream.Length - 16) - { - value = default; - return false; - } - - value = reader.ReadInt128BigEndian(); - return true; - } - - /// - /// Read an Int128 from the base stream - /// - /// Reads in little-endian format - public static bool TryReadInt128LittleEndian(this BinaryReader reader, out Int128 value) - { - if (reader.BaseStream.Position > reader.BaseStream.Length - 16) - { - value = default; - return false; - } - - value = reader.ReadInt128LittleEndian(); - return true; - } - - /// - /// Read a UInt128 from the base stream - /// - /// Reads in machine native format - public static bool TryReadUInt128(this BinaryReader reader, out UInt128 value) - { - if (BitConverter.IsLittleEndian) - return reader.TryReadUInt128LittleEndian(out value); - else - return reader.TryReadUInt128BigEndian(out value); - } - - /// - /// Read a UInt128 from the base stream - /// - /// Reads in big-endian format - public static bool TryReadUInt128BigEndian(this BinaryReader reader, out UInt128 value) - { - if (reader.BaseStream.Position > reader.BaseStream.Length - 16) - { - value = default; - return false; - } - - value = reader.ReadUInt128BigEndian(); - return true; - } - - /// - /// Read a UInt128 from the base stream - /// - /// Reads in little-endian format - public static bool TryReadUInt128LittleEndian(this BinaryReader reader, out UInt128 value) - { - if (reader.BaseStream.Position > reader.BaseStream.Length - 16) - { - value = default; - return false; - } - - value = reader.ReadUInt128LittleEndian(); - return true; - } -#endif - - /// - /// Read a Decimal from the base stream - /// - /// Reads in machine native format - public static bool TryReadDecimal(this BinaryReader reader, out decimal value) - { - if (BitConverter.IsLittleEndian) - return reader.TryReadDecimalLittleEndian(out value); - else - return reader.TryReadDecimalBigEndian(out value); - } - - /// - /// Read a Decimal from the base stream - /// - /// Reads in big-endian format - public static bool TryReadDecimalBigEndian(this BinaryReader reader, out decimal value) - { - if (reader.BaseStream.Position > reader.BaseStream.Length - 16) - { - value = default; - return false; - } - - value = reader.ReadDecimalBigEndian(); - return true; - } - - /// - /// Read a Decimal from the base stream - /// - /// Reads in little-endian format - public static bool TryReadDecimalLittleEndian(this BinaryReader reader, out decimal value) - { - if (reader.BaseStream.Position > reader.BaseStream.Length - 16) - { - value = default; - return false; - } - - value = reader.ReadDecimalLittleEndian(); - return true; - } - - #endregion } } diff --git a/SabreTools.IO.Extensions/BinaryWriterExtensions.cs b/SabreTools.IO.Extensions/BinaryWriterExtensions.cs index 0539dd8..c74de23 100644 --- a/SabreTools.IO.Extensions/BinaryWriterExtensions.cs +++ b/SabreTools.IO.Extensions/BinaryWriterExtensions.cs @@ -3,7 +3,7 @@ using System.IO; using System.Reflection; using System.Runtime.InteropServices; using System.Text; -using SabreTools.Numerics; +using SabreTools.Numerics.Extensions; namespace SabreTools.IO.Extensions { @@ -12,493 +12,6 @@ namespace SabreTools.IO.Extensions /// public static class BinaryWriterExtensions { - /// - /// Writes in both-endian format - public static bool WriteBothEndian(this BinaryWriter writer, BothUInt8 value) - { - writer.Write(value.LittleEndian); - writer.Write(value.BigEndian); - return true; - } - - /// - /// Writes in both-endian format - public static bool WriteBothEndian(this BinaryWriter writer, BothInt8 value) - { - writer.Write(value.LittleEndian); - writer.Write(value.BigEndian); - return true; - } - - /// - /// Writes in big-endian format - public static bool WriteBigEndian(this BinaryWriter writer, byte[] value) - { - Array.Reverse(value); - return WriteFromBuffer(writer, value); - } - - /// - public static bool Write(this BinaryWriter writer, char value, Encoding encoding) - { - byte[] buffer = encoding.GetBytes($"{value}"); - return WriteFromBuffer(writer, buffer); - } - - /// - /// Writes in big-endian format - public static bool WriteBigEndian(this BinaryWriter writer, short value) - { - byte[] buffer = value.GetBytesBigEndian(); - return WriteFromBuffer(writer, buffer); - } - - /// - /// Writes in little-endian format - public static bool WriteLittleEndian(this BinaryWriter writer, short value) - { - byte[] buffer = value.GetBytesLittleEndian(); - return WriteFromBuffer(writer, buffer); - } - - /// - /// Writes in both-endian format - public static bool WriteBothEndian(this BinaryWriter writer, BothInt16 value) - { - writer.WriteLittleEndian(value.LittleEndian); - writer.WriteBigEndian(value.BigEndian); - return true; - } - - /// - /// Writes in big-endian format - public static bool WriteBigEndian(this BinaryWriter writer, ushort value) - { - byte[] buffer = value.GetBytesBigEndian(); - return WriteFromBuffer(writer, buffer); - } - - /// - /// Writes in little-endian format - public static bool WriteLittleEndian(this BinaryWriter writer, ushort value) - { - byte[] buffer = value.GetBytesLittleEndian(); - return WriteFromBuffer(writer, buffer); - } - - /// - /// Writes in both-endian format - public static bool WriteBothEndian(this BinaryWriter writer, BothUInt16 value) - { - writer.WriteLittleEndian(value.LittleEndian); - writer.WriteBigEndian(value.BigEndian); - return true; - } - -#if NET5_0_OR_GREATER - /// - /// Write a Half to the underlying stream - /// - /// Writes in machine native format - public static bool Write(this BinaryWriter writer, Half value) - { - if (BitConverter.IsLittleEndian) - return writer.WriteLittleEndian(value); - else - return writer.WriteBigEndian(value); - } - - /// - /// Writes in big-endian format - public static bool WriteBigEndian(this BinaryWriter writer, Half value) - { - byte[] buffer = value.GetBytesBigEndian(); - return WriteFromBuffer(writer, buffer); - } - - /// - /// Writes in little-endian format - public static bool WriteLittleEndian(this BinaryWriter writer, Half value) - { - byte[] buffer = value.GetBytesLittleEndian(); - return WriteFromBuffer(writer, buffer); - } -#endif - - /// - /// Write an Int24 to the underlying stream - /// - /// Writes in machine native format - public static bool Write(this BinaryWriter writer, Int24 value) - { - if (BitConverter.IsLittleEndian) - return writer.WriteLittleEndian(value); - else - return writer.WriteBigEndian(value); - } - - /// - /// Write an Int24 to the underlying stream - /// - /// Writes in big-endian format - public static bool WriteBigEndian(this BinaryWriter writer, Int24 value) - { - byte[] buffer = value.GetBytesBigEndian(); - return WriteFromBuffer(writer, buffer); - } - - /// - /// Write an Int24 to the underlying stream - /// - /// Writes in little-endian format - public static bool WriteLittleEndian(this BinaryWriter writer, Int24 value) - { - byte[] buffer = value.GetBytesLittleEndian(); - return WriteFromBuffer(writer, buffer); - } - - /// - /// Write a UInt24 to the underlying stream - /// - /// Writes in machine native format - public static bool Write(this BinaryWriter writer, UInt24 value) - { - if (BitConverter.IsLittleEndian) - return writer.WriteLittleEndian(value); - else - return writer.WriteBigEndian(value); - } - - /// - /// Write a UInt24 to the underlying stream - /// - /// Writes in big-endian format - public static bool WriteBigEndian(this BinaryWriter writer, UInt24 value) - { - byte[] buffer = value.GetBytesBigEndian(); - return WriteFromBuffer(writer, buffer); - } - - /// - /// Write a UInt24 to the underlying stream - /// - /// Writes in little-endian format - public static bool WriteLittleEndian(this BinaryWriter writer, UInt24 value) - { - byte[] buffer = value.GetBytesLittleEndian(); - return WriteFromBuffer(writer, buffer); - } - - /// - /// Writes in big-endian format - public static bool WriteBigEndian(this BinaryWriter writer, int value) - { - byte[] buffer = value.GetBytesBigEndian(); - return WriteFromBuffer(writer, buffer); - } - - /// - /// Writes in little-endian format - public static bool WriteLittleEndian(this BinaryWriter writer, int value) - { - byte[] buffer = value.GetBytesLittleEndian(); - return WriteFromBuffer(writer, buffer); - } - - /// - /// Writes in both-endian format - public static bool WriteBothEndian(this BinaryWriter writer, BothInt32 value) - { - writer.WriteLittleEndian(value.LittleEndian); - writer.WriteBigEndian(value.BigEndian); - return true; - } - - /// - /// Writes in big-endian format - public static bool WriteBigEndian(this BinaryWriter writer, uint value) - { - byte[] buffer = value.GetBytesBigEndian(); - return WriteFromBuffer(writer, buffer); - } - - /// - /// Writes in little-endian format - public static bool WriteLittleEndian(this BinaryWriter writer, uint value) - { - byte[] buffer = value.GetBytesLittleEndian(); - return WriteFromBuffer(writer, buffer); - } - - /// - /// Writes in both-endian format - public static bool WriteBothEndian(this BinaryWriter writer, BothUInt32 value) - { - writer.WriteLittleEndian(value.LittleEndian); - writer.WriteBigEndian(value.BigEndian); - return true; - } - - /// - /// Writes in big-endian format - public static bool WriteBigEndian(this BinaryWriter writer, float value) - { - byte[] buffer = value.GetBytesBigEndian(); - return WriteFromBuffer(writer, buffer); - } - - /// - /// Writes in little-endian format - public static bool WriteLittleEndian(this BinaryWriter writer, float value) - { - byte[] buffer = value.GetBytesLittleEndian(); - return WriteFromBuffer(writer, buffer); - } - - /// - /// Write an Int48 to the underlying stream - /// - /// Writes in machine native format - public static bool Write(this BinaryWriter writer, Int48 value) - { - if (BitConverter.IsLittleEndian) - return writer.WriteLittleEndian(value); - else - return writer.WriteBigEndian(value); - } - - /// - /// Write an Int48 to the underlying stream - /// - /// Writes in big-endian format - public static bool WriteBigEndian(this BinaryWriter writer, Int48 value) - { - byte[] buffer = value.GetBytesBigEndian(); - return WriteFromBuffer(writer, buffer); - } - - /// - /// Write an Int48 to the underlying stream - /// - /// Writes in little-endian format - public static bool WriteLittleEndian(this BinaryWriter writer, Int48 value) - { - byte[] buffer = value.GetBytesLittleEndian(); - return WriteFromBuffer(writer, buffer); - } - - /// - /// Write a UInt48 to the underlying stream - /// - /// Writes in machine native format - public static bool Write(this BinaryWriter writer, UInt48 value) - { - if (BitConverter.IsLittleEndian) - return writer.WriteLittleEndian(value); - else - return writer.WriteBigEndian(value); - } - - /// - /// Write a UInt48 to the underlying stream - /// - /// Writes in big-endian format - public static bool WriteBigEndian(this BinaryWriter writer, UInt48 value) - { - byte[] buffer = value.GetBytesBigEndian(); - return WriteFromBuffer(writer, buffer); - } - - /// - /// Write a UInt48 to the underlying stream - /// - /// Writes in little-endian format - public static bool WriteLittleEndian(this BinaryWriter writer, UInt48 value) - { - byte[] buffer = value.GetBytesLittleEndian(); - return WriteFromBuffer(writer, buffer); - } - - /// - /// Writes in big-endian format - public static bool WriteBigEndian(this BinaryWriter writer, long value) - { - byte[] buffer = value.GetBytesBigEndian(); - return WriteFromBuffer(writer, buffer); - } - - /// - /// Writes in little-endian format - public static bool WriteLittleEndian(this BinaryWriter writer, long value) - { - byte[] buffer = value.GetBytesLittleEndian(); - return WriteFromBuffer(writer, buffer); - } - - /// - /// Writes in both-endian format - public static bool WriteBothEndian(this BinaryWriter writer, BothInt64 value) - { - writer.WriteLittleEndian(value.LittleEndian); - writer.WriteBigEndian(value.BigEndian); - return true; - } - - /// - /// Writes in big-endian format - public static bool WriteBigEndian(this BinaryWriter writer, ulong value) - { - byte[] buffer = value.GetBytesBigEndian(); - return WriteFromBuffer(writer, buffer); - } - - /// - /// Writes in little-endian format - public static bool WriteLittleEndian(this BinaryWriter writer, ulong value) - { - byte[] buffer = value.GetBytesLittleEndian(); - return WriteFromBuffer(writer, buffer); - } - - /// - /// Writes in both-endian format - public static bool WriteBothEndian(this BinaryWriter writer, BothUInt64 value) - { - writer.WriteLittleEndian(value.LittleEndian); - writer.WriteBigEndian(value.BigEndian); - return true; - } - - /// - /// Writes in big-endian format - public static bool WriteBigEndian(this BinaryWriter writer, double value) - { - byte[] buffer = value.GetBytesBigEndian(); - return WriteFromBuffer(writer, buffer); - } - - /// - /// Writes in little-endian format - public static bool WriteLittleEndian(this BinaryWriter writer, double value) - { - byte[] buffer = value.GetBytesLittleEndian(); - return WriteFromBuffer(writer, buffer); - } - - /// - /// Write a Guid - /// - /// Writes in machine native format - public static bool Write(this BinaryWriter writer, Guid value) - { - if (BitConverter.IsLittleEndian) - return writer.WriteLittleEndian(value); - else - return writer.WriteBigEndian(value); - } - - /// - /// Write a Guid - /// - /// Writes in big-endian format - public static bool WriteBigEndian(this BinaryWriter writer, Guid value) - { - byte[] buffer = value.GetBytesBigEndian(); - return WriteFromBuffer(writer, buffer); - } - - /// - /// Write a Guid - /// - /// Writes in big-endian format - public static bool WriteLittleEndian(this BinaryWriter writer, Guid value) - { - byte[] buffer = value.GetBytesLittleEndian(); - return WriteFromBuffer(writer, buffer); - } - -#if NET7_0_OR_GREATER - /// - /// Write an Int128 to the underlying stream - /// - /// Writes in machine native format - public static bool Write(this BinaryWriter writer, Int128 value) - { - if (BitConverter.IsLittleEndian) - return writer.WriteLittleEndian(value); - else - return writer.WriteBigEndian(value); - } - - /// - /// Write an Int128 to the underlying stream - /// - /// Writes in big-endian format - public static bool WriteBigEndian(this BinaryWriter writer, Int128 value) - { - byte[] buffer = value.GetBytesBigEndian(); - return WriteFromBuffer(writer, buffer); - } - - /// - /// Write an Int128 to the underlying stream - /// - /// Writes in little-endian format - public static bool WriteLittleEndian(this BinaryWriter writer, Int128 value) - { - byte[] buffer = value.GetBytesLittleEndian(); - return WriteFromBuffer(writer, buffer); - } - - /// - /// Write a UInt128 to the underlying stream - /// - /// Writes in machine native format - public static bool Write(this BinaryWriter writer, UInt128 value) - { - if (BitConverter.IsLittleEndian) - return writer.WriteLittleEndian(value); - else - return writer.WriteBigEndian(value); - } - - /// - /// Write a UInt128 to the underlying stream - /// - /// Writes in big-endian format - public static bool WriteBigEndian(this BinaryWriter writer, UInt128 value) - { - byte[] buffer = value.GetBytesBigEndian(); - return WriteFromBuffer(writer, buffer); - } - - /// - /// Write a UInt128 to the underlying stream - /// - /// Writes in little-endian format - public static bool WriteLittleEndian(this BinaryWriter writer, UInt128 value) - { - byte[] buffer = value.GetBytesLittleEndian(); - return WriteFromBuffer(writer, buffer); - } -#endif - - /// - /// Writes in big-endian format - public static bool WriteBigEndian(this BinaryWriter writer, decimal value) - { - byte[] buffer = value.GetBytesBigEndian(); - return WriteFromBuffer(writer, buffer); - } - - /// - /// Writes in little-endian format - public static bool WriteLittleEndian(this BinaryWriter writer, decimal value) - { - byte[] buffer = value.GetBytesLittleEndian(); - return WriteFromBuffer(writer, buffer); - } - /// /// Write a null-terminated string to the underlying stream /// diff --git a/SabreTools.IO.Extensions/ByteArrayExtensions.cs b/SabreTools.IO.Extensions/ByteArrayExtensions.cs index 125c363..8c3ce0e 100644 --- a/SabreTools.IO.Extensions/ByteArrayExtensions.cs +++ b/SabreTools.IO.Extensions/ByteArrayExtensions.cs @@ -3,6 +3,7 @@ using System.Collections.Generic; using System.IO; using System.Text; using SabreTools.Matching; +using SabreTools.Numerics.Extensions; namespace SabreTools.IO.Extensions { diff --git a/SabreTools.IO.Extensions/ByteArrayReaderExtensions.cs b/SabreTools.IO.Extensions/ByteArrayReaderExtensions.cs index 8e0dee8..47008f3 100644 --- a/SabreTools.IO.Extensions/ByteArrayReaderExtensions.cs +++ b/SabreTools.IO.Extensions/ByteArrayReaderExtensions.cs @@ -3,7 +3,7 @@ using System.Collections.Generic; using System.Reflection; using System.Runtime.InteropServices; using System.Text; -using SabreTools.Numerics; +using SabreTools.Numerics.Extensions; namespace SabreTools.IO.Extensions { @@ -12,767 +12,6 @@ namespace SabreTools.IO.Extensions /// public static class ByteArrayReaderExtensions { - #region Exact Read - - /// - /// Read a UInt8 and increment the pointer to an array - /// - public static byte ReadByte(this byte[] content, ref int offset) - { - byte[] buffer = ReadExactlyToBuffer(content, ref offset, 1); - return buffer[0]; - } - - /// - /// Read a UInt8 and increment the pointer to an array - /// - public static byte ReadByteValue(this byte[] content, ref int offset) - => content.ReadByte(ref offset); - - /// - /// Read a UInt8 and increment the pointer to an array - /// - /// Reads in both-endian format - public static BothUInt8 ReadByteBothEndian(this byte[] content, ref int offset) - { - byte le = content.ReadByte(ref offset); - byte be = content.ReadByte(ref offset); - return new BothUInt8(le, be); - } - - /// - /// Read a UInt8[] and increment the pointer to an array - /// - public static byte[] ReadBytes(this byte[] content, ref int offset, int count) - => ReadExactlyToBuffer(content, ref offset, count); - - /// - /// Read an Int8 and increment the pointer to an array - /// - public static sbyte ReadSByte(this byte[] content, ref int offset) - { - byte[] buffer = ReadExactlyToBuffer(content, ref offset, 1); - return (sbyte)buffer[0]; - } - - /// - /// Read a Int8 and increment the pointer to an array - /// - /// Reads in both-endian format - public static BothInt8 ReadSByteBothEndian(this byte[] content, ref int offset) - { - sbyte le = content.ReadSByte(ref offset); - sbyte be = content.ReadSByte(ref offset); - return new BothInt8(le, be); - } - - /// - /// Read a Char and increment the pointer to an array - /// - public static char ReadChar(this byte[] content, ref int offset) - { - byte[] buffer = ReadExactlyToBuffer(content, ref offset, 1); - return (char)buffer[0]; - } - - /// - /// Read an Int16 and increment the pointer to an array - /// - /// Reads in machine native format - public static short ReadInt16(this byte[] content, ref int offset) - { - if (BitConverter.IsLittleEndian) - return content.ReadInt16LittleEndian(ref offset); - else - return content.ReadInt16BigEndian(ref offset); - } - - /// - /// Read an Int16 and increment the pointer to an array - /// - /// Reads in big-endian format - public static short ReadInt16BigEndian(this byte[] content, ref int offset) - { - byte[] buffer = ReadExactlyToBuffer(content, ref offset, 2); - return buffer.ToInt16BigEndian(); - } - - /// - /// Read an Int16 and increment the pointer to an array - /// - /// Reads in little-endian format - public static short ReadInt16LittleEndian(this byte[] content, ref int offset) - { - byte[] buffer = ReadExactlyToBuffer(content, ref offset, 2); - return buffer.ToInt16LittleEndian(); - } - - /// - /// Read a Int16 and increment the pointer to an array - /// - /// Reads in both-endian format - public static BothInt16 ReadInt16BothEndian(this byte[] content, ref int offset) - { - short le = content.ReadInt16LittleEndian(ref offset); - short be = content.ReadInt16BigEndian(ref offset); - return new BothInt16(le, be); - } - - /// - /// Read a UInt16 and increment the pointer to an array - /// - /// Reads in machine native format - public static ushort ReadUInt16(this byte[] content, ref int offset) - { - if (BitConverter.IsLittleEndian) - return content.ReadUInt16LittleEndian(ref offset); - else - return content.ReadUInt16BigEndian(ref offset); - } - - /// - /// Read a UInt16 and increment the pointer to an array - /// - /// Reads in big-endian format - public static ushort ReadUInt16BigEndian(this byte[] content, ref int offset) - { - byte[] buffer = ReadExactlyToBuffer(content, ref offset, 2); - return buffer.ToUInt16BigEndian(); - } - - /// - /// Read a UInt16 and increment the pointer to an array - /// - /// Reads in little-endian format - public static ushort ReadUInt16LittleEndian(this byte[] content, ref int offset) - { - byte[] buffer = ReadExactlyToBuffer(content, ref offset, 2); - return buffer.ToUInt16LittleEndian(); - } - - /// - /// Read a UInt16 and increment the pointer to an array - /// - /// Reads in both-endian format - public static BothUInt16 ReadUInt16BothEndian(this byte[] content, ref int offset) - { - ushort le = content.ReadUInt16LittleEndian(ref offset); - ushort be = content.ReadUInt16BigEndian(ref offset); - return new BothUInt16(le, be); - } - - /// - /// Read a WORD (2-byte) and increment the pointer to an array - /// - /// Reads in machine native format - public static ushort ReadWORD(this byte[] content, ref int offset) - => content.ReadUInt16(ref offset); - - /// - /// Read a WORD (2-byte) and increment the pointer to an array - /// - /// Reads in big-endian format - public static ushort ReadWORDBigEndian(this byte[] content, ref int offset) - => content.ReadUInt16BigEndian(ref offset); - - /// - /// Read a WORD (2-byte) and increment the pointer to an array - /// - /// Reads in little-endian format - public static ushort ReadWORDLittleEndian(this byte[] content, ref int offset) - => content.ReadUInt16LittleEndian(ref offset); - - /// - /// Read a WORD (2-byte) and increment the pointer to an array - /// - /// Reads in both-endian format - public static BothUInt16 ReadWORDBothEndian(this byte[] content, ref int offset) - => content.ReadUInt16BothEndian(ref offset); - -#if NET5_0_OR_GREATER - /// - /// Read a Half and increment the pointer to an array - /// - /// Reads in machine native format - public static Half ReadHalf(this byte[] content, ref int offset) - { - if (BitConverter.IsLittleEndian) - return content.ReadHalfLittleEndian(ref offset); - else - return content.ReadHalfBigEndian(ref offset); - } - - /// - /// Read a Half and increment the pointer to an array - /// - /// Reads in big-endian format - public static Half ReadHalfBigEndian(this byte[] content, ref int offset) - { - byte[] buffer = ReadExactlyToBuffer(content, ref offset, 2); - return buffer.ToHalfBigEndian(); - } - - /// - /// Read a Half and increment the pointer to an array - /// - /// Reads in little-endian format - public static Half ReadHalfLittleEndian(this byte[] content, ref int offset) - { - byte[] buffer = ReadExactlyToBuffer(content, ref offset, 2); - return buffer.ToHalfLittleEndian(); - } -#endif - - /// - /// Read an Int24 and increment the pointer to an array - /// - /// Reads in machine native format - public static Int24 ReadInt24(this byte[] content, ref int offset) - { - if (BitConverter.IsLittleEndian) - return content.ReadInt24LittleEndian(ref offset); - else - return content.ReadInt24BigEndian(ref offset); - } - - /// - /// Read an Int24 and increment the pointer to an array - /// - /// Reads in big-endian format - public static Int24 ReadInt24BigEndian(this byte[] content, ref int offset) - { - byte[] buffer = ReadExactlyToBuffer(content, ref offset, 3); - return buffer.ToInt24BigEndian(); - } - - /// - /// Read an Int24 and increment the pointer to an array - /// - /// Reads in little-endian format - public static Int24 ReadInt24LittleEndian(this byte[] content, ref int offset) - { - byte[] buffer = ReadExactlyToBuffer(content, ref offset, 3); - return buffer.ToInt24LittleEndian(); - } - - /// - /// Read a UInt24 and increment the pointer to an array - /// - /// Reads in machine native format - public static UInt24 ReadUInt24(this byte[] content, ref int offset) - { - if (BitConverter.IsLittleEndian) - return content.ReadUInt24LittleEndian(ref offset); - else - return content.ReadUInt24BigEndian(ref offset); - } - - /// - /// Read a UInt24 and increment the pointer to an array - /// - /// Reads in big-endian format - public static UInt24 ReadUInt24BigEndian(this byte[] content, ref int offset) - { - byte[] buffer = ReadExactlyToBuffer(content, ref offset, 3); - return buffer.ToUInt24BigEndian(); - } - - /// - /// Read a UInt24 and increment the pointer to an array - /// - /// Reads in little-endian format - public static UInt24 ReadUInt24LittleEndian(this byte[] content, ref int offset) - { - byte[] buffer = ReadExactlyToBuffer(content, ref offset, 3); - return buffer.ToUInt24LittleEndian(); - } - - /// - /// Read an Int32 and increment the pointer to an array - /// - /// Reads in machine native format - public static int ReadInt32(this byte[] content, ref int offset) - { - if (BitConverter.IsLittleEndian) - return content.ReadInt32LittleEndian(ref offset); - else - return content.ReadInt32BigEndian(ref offset); - } - - /// - /// Read an Int32 and increment the pointer to an array - /// - /// Reads in big-endian format - public static int ReadInt32BigEndian(this byte[] content, ref int offset) - { - byte[] buffer = ReadExactlyToBuffer(content, ref offset, 4); - return buffer.ToInt32BigEndian(); - } - - /// - /// Read an Int32 and increment the pointer to an array - /// - /// Reads in little-endian format - public static int ReadInt32LittleEndian(this byte[] content, ref int offset) - { - byte[] buffer = ReadExactlyToBuffer(content, ref offset, 4); - return buffer.ToInt32LittleEndian(); - } - - /// - /// Read a Int32 and increment the pointer to an array - /// - /// Reads in both-endian format - public static BothInt32 ReadInt32BothEndian(this byte[] content, ref int offset) - { - int le = content.ReadInt32LittleEndian(ref offset); - int be = content.ReadInt32BigEndian(ref offset); - return new BothInt32(le, be); - } - - /// - /// Read a UInt32 and increment the pointer to an array - /// - /// Reads in machine native format - public static uint ReadUInt32(this byte[] content, ref int offset) - { - if (BitConverter.IsLittleEndian) - return content.ReadUInt32LittleEndian(ref offset); - else - return content.ReadUInt32BigEndian(ref offset); - } - - /// - /// Read a UInt32 and increment the pointer to an array - /// - /// Reads in big-endian format - public static uint ReadUInt32BigEndian(this byte[] content, ref int offset) - { - byte[] buffer = ReadExactlyToBuffer(content, ref offset, 4); - return buffer.ToUInt32BigEndian(); - } - - /// - /// Read a UInt32 and increment the pointer to an array - /// - /// Reads in little-endian format - public static uint ReadUInt32LittleEndian(this byte[] content, ref int offset) - { - byte[] buffer = ReadExactlyToBuffer(content, ref offset, 4); - return buffer.ToUInt32LittleEndian(); - } - - /// - /// Read a UInt32 and increment the pointer to an array - /// - /// Reads in both-endian format - public static BothUInt32 ReadUInt32BothEndian(this byte[] content, ref int offset) - { - uint le = content.ReadUInt32LittleEndian(ref offset); - uint be = content.ReadUInt32BigEndian(ref offset); - return new BothUInt32(le, be); - } - - /// - /// Read a DWORD (4-byte) and increment the pointer to an array - /// - /// Reads in machine native format - public static uint ReadDWORD(this byte[] content, ref int offset) - => content.ReadUInt32(ref offset); - - /// - /// Read a DWORD (4-byte) and increment the pointer to an array - /// - /// Reads in big-endian format - public static uint ReadDWORDBigEndian(this byte[] content, ref int offset) - => content.ReadUInt32BigEndian(ref offset); - - /// - /// Read a DWORD (4-byte) and increment the pointer to an array - /// - /// Reads in little-endian format - public static uint ReadDWORDLittleEndian(this byte[] content, ref int offset) - => content.ReadUInt32LittleEndian(ref offset); - - /// - /// Read a DWORD (4-byte) and increment the pointer to an array - /// - /// Reads in both-endian format - public static BothUInt32 ReadDWORDBothEndian(this byte[] content, ref int offset) - => content.ReadUInt32BothEndian(ref offset); - - /// - /// Read a Single and increment the pointer to an array - /// - /// Reads in machine native format - public static float ReadSingle(this byte[] content, ref int offset) - { - if (BitConverter.IsLittleEndian) - return content.ReadSingleLittleEndian(ref offset); - else - return content.ReadSingleBigEndian(ref offset); - } - - /// - /// Read a Single and increment the pointer to an array - /// - /// Reads in big-endian format - public static float ReadSingleBigEndian(this byte[] content, ref int offset) - { - byte[] buffer = ReadExactlyToBuffer(content, ref offset, 4); - return buffer.ToSingleBigEndian(); - } - - /// - /// Read a Single and increment the pointer to an array - /// - /// Reads in little-endian format - public static float ReadSingleLittleEndian(this byte[] content, ref int offset) - { - byte[] buffer = ReadExactlyToBuffer(content, ref offset, 4); - return buffer.ToSingleLittleEndian(); - } - - /// - /// Read an Int48 and increment the pointer to an array - /// - /// Reads in machine native format - public static Int48 ReadInt48(this byte[] content, ref int offset) - { - if (BitConverter.IsLittleEndian) - return content.ReadInt48LittleEndian(ref offset); - else - return content.ReadInt48BigEndian(ref offset); - } - - /// - /// Read an Int48 and increment the pointer to an array - /// - /// Reads in big-endian format - public static Int48 ReadInt48BigEndian(this byte[] content, ref int offset) - { - byte[] buffer = ReadExactlyToBuffer(content, ref offset, 6); - return buffer.ToInt48BigEndian(); - } - - /// - /// Read an Int48 and increment the pointer to an array - /// - /// Reads in little-endian format - public static Int48 ReadInt48LittleEndian(this byte[] content, ref int offset) - { - byte[] buffer = ReadExactlyToBuffer(content, ref offset, 6); - return buffer.ToInt48LittleEndian(); - } - - /// - /// Read a UInt48 and increment the pointer to an array - /// - /// Reads in machine native format - public static UInt48 ReadUInt48(this byte[] content, ref int offset) - { - if (BitConverter.IsLittleEndian) - return content.ReadUInt48LittleEndian(ref offset); - else - return content.ReadUInt48BigEndian(ref offset); - } - - /// - /// Read an UInt48 and increment the pointer to an array - /// - /// Reads in big-endian format - public static UInt48 ReadUInt48BigEndian(this byte[] content, ref int offset) - { - byte[] buffer = ReadExactlyToBuffer(content, ref offset, 6); - return buffer.ToUInt48BigEndian(); - } - - /// - /// Read an UInt48 and increment the pointer to an array - /// - /// Reads in little-endian format - public static UInt48 ReadUInt48LittleEndian(this byte[] content, ref int offset) - { - byte[] buffer = ReadExactlyToBuffer(content, ref offset, 6); - return buffer.ToUInt48LittleEndian(); - } - - /// - /// Read an Int64 and increment the pointer to an array - /// - /// Reads in machine native format - public static long ReadInt64(this byte[] content, ref int offset) - { - if (BitConverter.IsLittleEndian) - return content.ReadInt64LittleEndian(ref offset); - else - return content.ReadInt64BigEndian(ref offset); - } - - /// - /// Read an Int64 and increment the pointer to an array - /// - /// Reads in big-endian format - public static long ReadInt64BigEndian(this byte[] content, ref int offset) - { - byte[] buffer = ReadExactlyToBuffer(content, ref offset, 8); - return buffer.ToInt64BigEndian(); - } - - /// - /// Read an Int64 and increment the pointer to an array - /// - /// Reads in big-endian format - public static long ReadInt64LittleEndian(this byte[] content, ref int offset) - { - byte[] buffer = ReadExactlyToBuffer(content, ref offset, 8); - return buffer.ToInt64LittleEndian(); - } - - /// - /// Read a Int64 and increment the pointer to an array - /// - /// Reads in both-endian format - public static BothInt64 ReadInt64BothEndian(this byte[] content, ref int offset) - { - long le = content.ReadInt64LittleEndian(ref offset); - long be = content.ReadInt64BigEndian(ref offset); - return new BothInt64(le, be); - } - - /// - /// Read a UInt64 and increment the pointer to an array - /// - /// Reads in machine native format - public static ulong ReadUInt64(this byte[] content, ref int offset) - { - if (BitConverter.IsLittleEndian) - return content.ReadUInt64LittleEndian(ref offset); - else - return content.ReadUInt64BigEndian(ref offset); - } - - /// - /// Read a UInt64 and increment the pointer to an array - /// - /// Reads in big-endian format - public static ulong ReadUInt64BigEndian(this byte[] content, ref int offset) - { - byte[] buffer = ReadExactlyToBuffer(content, ref offset, 8); - return buffer.ToUInt64BigEndian(); - } - - /// - /// Read a UInt64 and increment the pointer to an array - /// - /// Reads in little-endian format - public static ulong ReadUInt64LittleEndian(this byte[] content, ref int offset) - { - byte[] buffer = ReadExactlyToBuffer(content, ref offset, 8); - return buffer.ToUInt64LittleEndian(); - } - - /// - /// Read a UInt64 and increment the pointer to an array - /// - /// Reads in both-endian format - public static BothUInt64 ReadUInt64BothEndian(this byte[] content, ref int offset) - { - ulong le = content.ReadUInt64LittleEndian(ref offset); - ulong be = content.ReadUInt64BigEndian(ref offset); - return new BothUInt64(le, be); - } - - /// - /// Read a QWORD (8-byte) and increment the pointer to an array - /// - /// Reads in machine native format - public static ulong ReadQWORD(this byte[] content, ref int offset) - => content.ReadUInt64(ref offset); - - /// - /// Read a QWORD (8-byte) and increment the pointer to an array - /// - /// Reads in big-endian format - public static ulong ReadQWORDBigEndian(this byte[] content, ref int offset) - => content.ReadUInt64BigEndian(ref offset); - - /// - /// Read a QWORD (8-byte) and increment the pointer to an array - /// - /// Reads in little-endian format - public static ulong ReadQWORDLittleEndian(this byte[] content, ref int offset) - => content.ReadUInt64LittleEndian(ref offset); - - /// - /// Read a QWORD (8-byte) and increment the pointer to an array - /// - /// Reads in both-endian format - public static BothUInt64 ReadQWORDBothEndian(this byte[] content, ref int offset) - => content.ReadUInt64BothEndian(ref offset); - - /// - /// Read a Double and increment the pointer to an array - /// - /// Reads in machine native format - public static double ReadDouble(this byte[] content, ref int offset) - { - if (BitConverter.IsLittleEndian) - return content.ReadDoubleLittleEndian(ref offset); - else - return content.ReadDoubleBigEndian(ref offset); - } - - /// - /// Read a Double and increment the pointer to an array - /// - /// Reads in big-endian format - public static double ReadDoubleBigEndian(this byte[] content, ref int offset) - { - byte[] buffer = ReadExactlyToBuffer(content, ref offset, 8); - return buffer.ToDoubleBigEndian(); - } - - /// - /// Read a Double and increment the pointer to an array - /// - /// Reads in little-endian format - public static double ReadDoubleLittleEndian(this byte[] content, ref int offset) - { - byte[] buffer = ReadExactlyToBuffer(content, ref offset, 8); - return buffer.ToDoubleLittleEndian(); - } - - /// - /// Read a Guid and increment the pointer to an array - /// - /// Reads in machine native format - public static Guid ReadGuid(this byte[] content, ref int offset) - { - if (BitConverter.IsLittleEndian) - return content.ReadGuidLittleEndian(ref offset); - else - return content.ReadGuidBigEndian(ref offset); - } - - /// - /// Read a Guid and increment the pointer to an array - /// - /// Reads in big-endian format - public static Guid ReadGuidBigEndian(this byte[] content, ref int offset) - { - byte[] buffer = ReadExactlyToBuffer(content, ref offset, 16); - return buffer.ToGuidBigEndian(); - } - - /// - /// Read a Guid and increment the pointer to an array - /// - /// Reads in little-endian format - public static Guid ReadGuidLittleEndian(this byte[] content, ref int offset) - { - byte[] buffer = ReadExactlyToBuffer(content, ref offset, 16); - return buffer.ToGuidLittleEndian(); - } - -#if NET7_0_OR_GREATER - /// - /// Read an Int128 and increment the pointer to an array - /// - /// Reads in machine native format - public static Int128 ReadInt128(this byte[] content, ref int offset) - { - if (BitConverter.IsLittleEndian) - return content.ReadInt128LittleEndian(ref offset); - else - return content.ReadInt128BigEndian(ref offset); - } - - /// - /// Read an Int128 and increment the pointer to an array - /// - /// Reads in big-endian format - public static Int128 ReadInt128BigEndian(this byte[] content, ref int offset) - { - byte[] buffer = ReadExactlyToBuffer(content, ref offset, 16); - return buffer.ToInt128BigEndian(); - } - - /// - /// Read an Int128 and increment the pointer to an array - /// - /// Reads in little-endian format - public static Int128 ReadInt128LittleEndian(this byte[] content, ref int offset) - { - byte[] buffer = ReadExactlyToBuffer(content, ref offset, 16); - return buffer.ToInt128LittleEndian(); - } - - /// - /// Read a UInt128 and increment the pointer to an array - /// - /// Reads in machine native format - public static UInt128 ReadUInt128(this byte[] content, ref int offset) - { - if (BitConverter.IsLittleEndian) - return content.ReadUInt128LittleEndian(ref offset); - else - return content.ReadUInt128BigEndian(ref offset); - } - - /// - /// Read a UInt128 and increment the pointer to an array - /// - /// Reads in big-endian format - public static UInt128 ReadUInt128BigEndian(this byte[] content, ref int offset) - { - byte[] buffer = ReadExactlyToBuffer(content, ref offset, 16); - return buffer.ToUInt128BigEndian(); - } - - /// - /// Read a UInt128 and increment the pointer to an array - /// - /// Reads in little-endian format - public static UInt128 ReadUInt128LittleEndian(this byte[] content, ref int offset) - { - byte[] buffer = ReadExactlyToBuffer(content, ref offset, 16); - return buffer.ToUInt128LittleEndian(); - } -#endif - - /// - /// Read a Decimal and increment the pointer to an array - /// - /// Reads in machine native format - public static decimal ReadDecimal(this byte[] content, ref int offset) - { - if (BitConverter.IsLittleEndian) - return content.ReadDecimalLittleEndian(ref offset); - else - return content.ReadDecimalBigEndian(ref offset); - } - - /// - /// Read a Decimal and increment the pointer to an array - /// - /// Reads in big-endian format - public static decimal ReadDecimalBigEndian(this byte[] content, ref int offset) - { - byte[] buffer = ReadExactlyToBuffer(content, ref offset, 16); - return buffer.ToDecimalBigEndian(); - } - - /// - /// Read a Decimal and increment the pointer to an array - /// - /// Reads in little-endian format - public static decimal ReadDecimalLittleEndian(this byte[] content, ref int offset) - { - byte[] buffer = ReadExactlyToBuffer(content, ref offset, 16); - return buffer.ToDecimalLittleEndian(); - } - /// /// Read a null-terminated string from the array /// @@ -1255,1854 +494,5 @@ namespace SabreTools.IO.Extensions return buffer; } - - #endregion - - #region Peek Read - - /// - /// Peek a UInt8 without incrementing the pointer to an array - /// - public static byte PeekByte(this byte[] content, ref int offset) - { - byte value = content.ReadByte(ref offset); - offset -= 1; - return value; - } - - /// - /// Peek a UInt8 without incrementing the pointer to an array - /// - public static byte PeekByteValue(this byte[] content, ref int offset) - => content.PeekByte(ref offset); - - /// - /// Peek a UInt8 without incrementing the pointer to an array - /// - /// Reads in both-endian format - public static BothUInt8 PeekByteBothEndian(this byte[] content, ref int offset) - { - BothUInt8 value = content.ReadByteBothEndian(ref offset); - offset -= 2; - return value; - } - - /// - /// Peek a UInt8[] without incrementing the pointer to an array - /// - public static byte[] PeekBytes(this byte[] content, ref int offset, int count) - { - byte[] value = content.ReadBytes(ref offset, count); - offset -= count; - return value; - } - - /// - /// Peek an Int8 without incrementing the pointer to an array - /// - public static sbyte PeekSByte(this byte[] content, ref int offset) - { - sbyte value = content.ReadSByte(ref offset); - offset -= 1; - return value; - } - - /// - /// Peek a Int8 without incrementing the pointer to an array - /// - /// Reads in both-endian format - public static BothInt8 PeekSByteBothEndian(this byte[] content, ref int offset) - { - BothInt8 value = content.ReadSByteBothEndian(ref offset); - offset -= 2; - return value; - } - - /// - /// Peek a Char without incrementing the pointer to an array - /// - public static char PeekChar(this byte[] content, ref int offset) - { - char value = content.ReadChar(ref offset); - offset -= 1; - return value; - } - - /// - /// Peek an Int16 without incrementing the pointer to an array - /// - /// Reads in machine native format - public static short PeekInt16(this byte[] content, ref int offset) - { - if (BitConverter.IsLittleEndian) - return content.PeekInt16LittleEndian(ref offset); - else - return content.PeekInt16BigEndian(ref offset); - } - - /// - /// Peek an Int16 without incrementing the pointer to an array - /// - /// Reads in big-endian format - public static short PeekInt16BigEndian(this byte[] content, ref int offset) - { - short value = content.ReadInt16BigEndian(ref offset); - offset -= 2; - return value; - } - - /// - /// Peek an Int16 without incrementing the pointer to an array - /// - /// Reads in little-endian format - public static short PeekInt16LittleEndian(this byte[] content, ref int offset) - { - short value = content.ReadInt16LittleEndian(ref offset); - offset -= 2; - return value; - } - - /// - /// Peek a Int16 without incrementing the pointer to an array - /// - /// Reads in both-endian format - public static BothInt16 PeekInt16BothEndian(this byte[] content, ref int offset) - { - BothInt16 value = content.ReadInt16BothEndian(ref offset); - offset -= 4; - return value; - } - - /// - /// Peek a UInt16 without incrementing the pointer to an array - /// - /// Reads in machine native format - public static ushort PeekUInt16(this byte[] content, ref int offset) - { - if (BitConverter.IsLittleEndian) - return content.PeekUInt16LittleEndian(ref offset); - else - return content.PeekUInt16BigEndian(ref offset); - } - - /// - /// Peek a UInt16 without incrementing the pointer to an array - /// - /// Reads in big-endian format - public static ushort PeekUInt16BigEndian(this byte[] content, ref int offset) - { - ushort value = content.ReadUInt16BigEndian(ref offset); - offset -= 2; - return value; - } - - /// - /// Peek a UInt16 without incrementing the pointer to an array - /// - /// Reads in little-endian format - public static ushort PeekUInt16LittleEndian(this byte[] content, ref int offset) - { - ushort value = content.ReadUInt16LittleEndian(ref offset); - offset -= 2; - return value; - } - - /// - /// Peek a UInt16 without incrementing the pointer to an array - /// - /// Reads in both-endian format - public static BothUInt16 PeekUInt16BothEndian(this byte[] content, ref int offset) - { - BothUInt16 value = content.ReadUInt16BothEndian(ref offset); - offset -= 4; - return value; - } - - /// - /// Peek a WORD (2-byte) without incrementing the pointer to an array - /// - /// Reads in machine native format - public static ushort PeekWORD(this byte[] content, ref int offset) - => content.PeekUInt16(ref offset); - - /// - /// Peek a WORD (2-byte) without incrementing the pointer to an array - /// - /// Reads in big-endian format - public static ushort PeekWORDBigEndian(this byte[] content, ref int offset) - => content.PeekUInt16BigEndian(ref offset); - - /// - /// Peek a WORD (2-byte) without incrementing the pointer to an array - /// - /// Reads in little-endian format - public static ushort PeekWORDLittleEndian(this byte[] content, ref int offset) - => content.PeekUInt16LittleEndian(ref offset); - - /// - /// Peek a WORD (2-byte) without incrementing the pointer to an array - /// - /// Reads in both-endian format - public static BothUInt16 PeekWORDBothEndian(this byte[] content, ref int offset) - => content.PeekUInt16BothEndian(ref offset); - -#if NET5_0_OR_GREATER - /// - /// Peek a Half without incrementing the pointer to an array - /// - /// Reads in machine native format - public static Half PeekHalf(this byte[] content, ref int offset) - { - if (BitConverter.IsLittleEndian) - return content.PeekHalfLittleEndian(ref offset); - else - return content.PeekHalfBigEndian(ref offset); - } - - /// - /// Peek a Half without incrementing the pointer to an array - /// - /// Reads in big-endian format - public static Half PeekHalfBigEndian(this byte[] content, ref int offset) - { - Half value = content.ReadHalfBigEndian(ref offset); - offset -= 2; - return value; - } - - /// - /// Peek a Half without incrementing the pointer to an array - /// - /// Reads in little-endian format - public static Half PeekHalfLittleEndian(this byte[] content, ref int offset) - { - Half value = content.ReadHalfLittleEndian(ref offset); - offset -= 2; - return value; - } -#endif - - /// - /// Peek an Int24 without incrementing the pointer to an array - /// - /// Reads in machine native format - public static Int24 PeekInt24(this byte[] content, ref int offset) - { - if (BitConverter.IsLittleEndian) - return content.PeekInt24LittleEndian(ref offset); - else - return content.PeekInt24BigEndian(ref offset); - } - - /// - /// Peek an Int24 without incrementing the pointer to an array - /// - /// Reads in big-endian format - public static Int24 PeekInt24BigEndian(this byte[] content, ref int offset) - { - Int24 value = content.ReadInt24BigEndian(ref offset); - offset -= 3; - return value; - } - - /// - /// Peek an Int24 without incrementing the pointer to an array - /// - /// Reads in little-endian format - public static Int24 PeekInt24LittleEndian(this byte[] content, ref int offset) - { - Int24 value = content.ReadInt24LittleEndian(ref offset); - offset -= 3; - return value; - } - - /// - /// Peek a UInt24 without incrementing the pointer to an array - /// - /// Reads in machine native format - public static UInt24 PeekUInt24(this byte[] content, ref int offset) - { - if (BitConverter.IsLittleEndian) - return content.PeekUInt24LittleEndian(ref offset); - else - return content.PeekUInt24BigEndian(ref offset); - } - - /// - /// Peek a UInt24 without incrementing the pointer to an array - /// - /// Reads in big-endian format - public static UInt24 PeekUInt24BigEndian(this byte[] content, ref int offset) - { - UInt24 value = content.ReadUInt24BigEndian(ref offset); - offset -= 3; - return value; - } - - /// - /// Peek a UInt24 without incrementing the pointer to an array - /// - /// Reads in little-endian format - public static UInt24 PeekUInt24LittleEndian(this byte[] content, ref int offset) - { - UInt24 value = content.ReadUInt24LittleEndian(ref offset); - offset -= 3; - return value; - } - - /// - /// Peek an Int32 without incrementing the pointer to an array - /// - /// Reads in machine native format - public static int PeekInt32(this byte[] content, ref int offset) - { - if (BitConverter.IsLittleEndian) - return content.PeekInt32LittleEndian(ref offset); - else - return content.PeekInt32BigEndian(ref offset); - } - - /// - /// Peek an Int32 without incrementing the pointer to an array - /// - /// Reads in big-endian format - public static int PeekInt32BigEndian(this byte[] content, ref int offset) - { - int value = content.ReadInt32BigEndian(ref offset); - offset -= 4; - return value; - } - - /// - /// Peek an Int32 without incrementing the pointer to an array - /// - /// Reads in little-endian format - public static int PeekInt32LittleEndian(this byte[] content, ref int offset) - { - int value = content.ReadInt32LittleEndian(ref offset); - offset -= 4; - return value; - } - - /// - /// Peek a Int32 without incrementing the pointer to an array - /// - /// Reads in both-endian format - public static BothInt32 PeekInt32BothEndian(this byte[] content, ref int offset) - { - BothInt32 value = content.ReadInt32BothEndian(ref offset); - offset -= 8; - return value; - } - - /// - /// Peek a UInt32 without incrementing the pointer to an array - /// - /// Reads in machine native format - public static uint PeekUInt32(this byte[] content, ref int offset) - { - if (BitConverter.IsLittleEndian) - return content.PeekUInt32LittleEndian(ref offset); - else - return content.PeekUInt32BigEndian(ref offset); - } - - /// - /// Peek a UInt32 without incrementing the pointer to an array - /// - /// Reads in big-endian format - public static uint PeekUInt32BigEndian(this byte[] content, ref int offset) - { - uint value = content.ReadUInt32BigEndian(ref offset); - offset -= 4; - return value; - } - - /// - /// Peek a UInt32 without incrementing the pointer to an array - /// - /// Reads in little-endian format - public static uint PeekUInt32LittleEndian(this byte[] content, ref int offset) - { - uint value = content.ReadUInt32LittleEndian(ref offset); - offset -= 4; - return value; - } - - /// - /// Peek a UInt32 without incrementing the pointer to an array - /// - /// Reads in both-endian format - public static BothUInt32 PeekUInt32BothEndian(this byte[] content, ref int offset) - { - BothUInt32 value = content.ReadUInt32BothEndian(ref offset); - offset -= 8; - return value; - } - - /// - /// Peek a DWORD (4-byte) without incrementing the pointer to an array - /// - /// Reads in machine native format - public static uint PeekDWORD(this byte[] content, ref int offset) - => content.PeekUInt32(ref offset); - - /// - /// Peek a DWORD (4-byte) without incrementing the pointer to an array - /// - /// Reads in big-endian format - public static uint PeekDWORDBigEndian(this byte[] content, ref int offset) - => content.PeekUInt32BigEndian(ref offset); - - /// - /// Peek a DWORD (4-byte) without incrementing the pointer to an array - /// - /// Reads in little-endian format - public static uint PeekDWORDLittleEndian(this byte[] content, ref int offset) - => content.PeekUInt32LittleEndian(ref offset); - - /// - /// Peek a DWORD (4-byte) without incrementing the pointer to an array - /// - /// Reads in both-endian format - public static BothUInt32 PeekDWORDBothEndian(this byte[] content, ref int offset) - => content.PeekUInt32BothEndian(ref offset); - - /// - /// Peek a Single without incrementing the pointer to an array - /// - /// Reads in machine native format - public static float PeekSingle(this byte[] content, ref int offset) - { - if (BitConverter.IsLittleEndian) - return content.PeekSingleLittleEndian(ref offset); - else - return content.PeekSingleBigEndian(ref offset); - } - - /// - /// Peek a Single without incrementing the pointer to an array - /// - /// Reads in big-endian format - public static float PeekSingleBigEndian(this byte[] content, ref int offset) - { - float value = content.ReadSingleBigEndian(ref offset); - offset -= 4; - return value; - } - - /// - /// Peek a Single without incrementing the pointer to an array - /// - /// Reads in little-endian format - public static float PeekSingleLittleEndian(this byte[] content, ref int offset) - { - float value = content.ReadSingleLittleEndian(ref offset); - offset -= 4; - return value; - } - - /// - /// Peek an Int48 without incrementing the pointer to an array - /// - /// Reads in machine native format - public static Int48 PeekInt48(this byte[] content, ref int offset) - { - if (BitConverter.IsLittleEndian) - return content.PeekInt48LittleEndian(ref offset); - else - return content.PeekInt48BigEndian(ref offset); - } - - /// - /// Peek an Int48 without incrementing the pointer to an array - /// - /// Reads in big-endian format - public static Int48 PeekInt48BigEndian(this byte[] content, ref int offset) - { - Int48 value = content.ReadInt48BigEndian(ref offset); - offset -= 6; - return value; - } - - /// - /// Peek an Int48 without incrementing the pointer to an array - /// - /// Reads in little-endian format - public static Int48 PeekInt48LittleEndian(this byte[] content, ref int offset) - { - Int48 value = content.ReadInt48LittleEndian(ref offset); - offset -= 6; - return value; - } - - /// - /// Peek a UInt48 without incrementing the pointer to an array - /// - /// Reads in machine native format - public static UInt48 PeekUInt48(this byte[] content, ref int offset) - { - if (BitConverter.IsLittleEndian) - return content.PeekUInt48LittleEndian(ref offset); - else - return content.PeekUInt48BigEndian(ref offset); - } - - /// - /// Peek an UInt48 without incrementing the pointer to an array - /// - /// Reads in big-endian format - public static UInt48 PeekUInt48BigEndian(this byte[] content, ref int offset) - { - UInt48 value = content.ReadUInt48BigEndian(ref offset); - offset -= 6; - return value; - } - - /// - /// Peek an UInt48 without incrementing the pointer to an array - /// - /// Reads in little-endian format - public static UInt48 PeekUInt48LittleEndian(this byte[] content, ref int offset) - { - UInt48 value = content.ReadUInt48LittleEndian(ref offset); - offset -= 6; - return value; - } - - /// - /// Peek an Int64 without incrementing the pointer to an array - /// - /// Reads in machine native format - public static long PeekInt64(this byte[] content, ref int offset) - { - if (BitConverter.IsLittleEndian) - return content.PeekInt64LittleEndian(ref offset); - else - return content.PeekInt64BigEndian(ref offset); - } - - /// - /// Peek an Int64 without incrementing the pointer to an array - /// - /// Reads in big-endian format - public static long PeekInt64BigEndian(this byte[] content, ref int offset) - { - long value = content.ReadInt64BigEndian(ref offset); - offset -= 8; - return value; - } - - /// - /// Peek an Int64 without incrementing the pointer to an array - /// - /// Reads in big-endian format - public static long PeekInt64LittleEndian(this byte[] content, ref int offset) - { - long value = content.ReadInt64LittleEndian(ref offset); - offset -= 8; - return value; - } - - /// - /// Peek a Int64 without incrementing the pointer to an array - /// - /// Reads in both-endian format - public static BothInt64 PeekInt64BothEndian(this byte[] content, ref int offset) - { - BothInt64 value = content.ReadInt64BothEndian(ref offset); - offset -= 16; - return value; - } - - /// - /// Peek a UInt64 without incrementing the pointer to an array - /// - /// Reads in machine native format - public static ulong PeekUInt64(this byte[] content, ref int offset) - { - if (BitConverter.IsLittleEndian) - return content.PeekUInt64LittleEndian(ref offset); - else - return content.PeekUInt64BigEndian(ref offset); - } - - /// - /// Peek a UInt64 without incrementing the pointer to an array - /// - /// Reads in big-endian format - public static ulong PeekUInt64BigEndian(this byte[] content, ref int offset) - { - ulong value = content.ReadUInt64BigEndian(ref offset); - offset -= 8; - return value; - } - - /// - /// Peek a UInt64 without incrementing the pointer to an array - /// - /// Reads in little-endian format - public static ulong PeekUInt64LittleEndian(this byte[] content, ref int offset) - { - ulong value = content.ReadUInt64LittleEndian(ref offset); - offset -= 8; - return value; - } - - /// - /// Peek a UInt64 without incrementing the pointer to an array - /// - /// Reads in both-endian format - public static BothUInt64 PeekUInt64BothEndian(this byte[] content, ref int offset) - { - BothUInt64 value = content.ReadUInt64BothEndian(ref offset); - offset -= 16; - return value; - } - - /// - /// Peek a QWORD (8-byte) without incrementing the pointer to an array - /// - /// Reads in machine native format - public static ulong PeekQWORD(this byte[] content, ref int offset) - => content.PeekUInt64(ref offset); - - /// - /// Peek a QWORD (8-byte) without incrementing the pointer to an array - /// - /// Reads in big-endian format - public static ulong PeekQWORDBigEndian(this byte[] content, ref int offset) - => content.PeekUInt64BigEndian(ref offset); - - /// - /// Peek a QWORD (8-byte) without incrementing the pointer to an array - /// - /// Reads in little-endian format - public static ulong PeekQWORDLittleEndian(this byte[] content, ref int offset) - => content.PeekUInt64LittleEndian(ref offset); - - /// - /// Peek a QWORD (8-byte) without incrementing the pointer to an array - /// - /// Reads in both-endian format - public static BothUInt64 PeekQWORDBothEndian(this byte[] content, ref int offset) - => content.PeekUInt64BothEndian(ref offset); - - /// - /// Peek a Double without incrementing the pointer to an array - /// - /// Reads in machine native format - public static double PeekDouble(this byte[] content, ref int offset) - { - if (BitConverter.IsLittleEndian) - return content.PeekDoubleLittleEndian(ref offset); - else - return content.PeekDoubleBigEndian(ref offset); - } - - /// - /// Peek a Double without incrementing the pointer to an array - /// - /// Reads in big-endian format - public static double PeekDoubleBigEndian(this byte[] content, ref int offset) - { - double value = content.ReadDoubleBigEndian(ref offset); - offset -= 8; - return value; - } - - /// - /// Peek a Double without incrementing the pointer to an array - /// - /// Reads in little-endian format - public static double PeekDoubleLittleEndian(this byte[] content, ref int offset) - { - double value = content.ReadDoubleLittleEndian(ref offset); - offset -= 8; - return value; - } - - /// - /// Peek a Guid without incrementing the pointer to an array - /// - /// Reads in machine native format - public static Guid PeekGuid(this byte[] content, ref int offset) - { - if (BitConverter.IsLittleEndian) - return content.PeekGuidLittleEndian(ref offset); - else - return content.PeekGuidBigEndian(ref offset); - } - - /// - /// Peek a Guid without incrementing the pointer to an array - /// - /// Reads in big-endian format - public static Guid PeekGuidBigEndian(this byte[] content, ref int offset) - { - Guid value = content.ReadGuidBigEndian(ref offset); - offset -= 16; - return value; - } - - /// - /// Peek a Guid without incrementing the pointer to an array - /// - /// Reads in big-endian format - public static Guid PeekGuidLittleEndian(this byte[] content, ref int offset) - { - Guid value = content.ReadGuidLittleEndian(ref offset); - offset -= 16; - return value; - } - -#if NET7_0_OR_GREATER - /// - /// Peek an Int128 without incrementing the pointer to an array - /// - /// Reads in machine native format - public static Int128 PeekInt128(this byte[] content, ref int offset) - { - if (BitConverter.IsLittleEndian) - return content.PeekInt128LittleEndian(ref offset); - else - return content.PeekInt128BigEndian(ref offset); - } - - /// - /// Peek an Int128 without incrementing the pointer to an array - /// - /// Reads in big-endian format - public static Int128 PeekInt128BigEndian(this byte[] content, ref int offset) - { - Int128 value = content.ReadInt128BigEndian(ref offset); - offset -= 16; - return value; - } - - /// - /// Peek an Int128 without incrementing the pointer to an array - /// - /// Reads in little-endian format - public static Int128 PeekInt128LittleEndian(this byte[] content, ref int offset) - { - Int128 value = content.ReadInt128LittleEndian(ref offset); - offset -= 16; - return value; - } - - /// - /// Peek a UInt128 without incrementing the pointer to an array - /// - /// Reads in machine native format - public static UInt128 PeekUInt128(this byte[] content, ref int offset) - { - if (BitConverter.IsLittleEndian) - return content.PeekUInt128LittleEndian(ref offset); - else - return content.PeekUInt128BigEndian(ref offset); - } - - /// - /// Peek a UInt128 without incrementing the pointer to an array - /// - /// Reads in big-endian format - public static UInt128 PeekUInt128BigEndian(this byte[] content, ref int offset) - { - UInt128 value = content.ReadUInt128BigEndian(ref offset); - offset -= 16; - return value; - } - - /// - /// Peek a UInt128 without incrementing the pointer to an array - /// - /// Reads in little-endian format - public static UInt128 PeekUInt128LittleEndian(this byte[] content, ref int offset) - { - UInt128 value = content.ReadUInt128LittleEndian(ref offset); - offset -= 16; - return value; - } -#endif - - /// - /// Peek a Decimal without incrementing the pointer to an array - /// - /// Reads in machine native format - public static decimal PeekDecimal(this byte[] content, ref int offset) - { - if (BitConverter.IsLittleEndian) - return content.PeekDecimalLittleEndian(ref offset); - else - return content.PeekDecimalBigEndian(ref offset); - } - - /// - /// Peek a Decimal without incrementing the pointer to an array - /// - /// Reads in big-endian format - public static decimal PeekDecimalBigEndian(this byte[] content, ref int offset) - { - decimal value = content.ReadDecimalBigEndian(ref offset); - offset -= 16; - return value; - } - - /// - /// Peek a Decimal without incrementing the pointer to an array - /// - /// Reads in little-endian format - public static decimal PeekDecimalLittleEndian(this byte[] content, ref int offset) - { - decimal value = content.ReadDecimalLittleEndian(ref offset); - offset -= 16; - return value; - } - - #endregion - - #region Try Read - - /// - /// Read a UInt8 and increment the pointer to an array - /// - public static bool TryReadByte(this byte[] content, ref int offset, out byte value) - { - if (offset > content.Length - 1) - { - value = default; - return false; - } - - value = content.ReadByte(ref offset); - return true; - } - - /// - /// Read a UInt8 and increment the pointer to an array - /// - public static bool TryReadByteValue(this byte[] content, ref int offset, out byte value) - { - if (offset > content.Length - 1) - { - value = default; - return false; - } - - value = content.ReadByteValue(ref offset); - return true; - } - - /// - /// Read a UInt8 and increment the pointer to an array - /// - /// Reads in both-endian format - public static bool TryReadByteBothEndian(this byte[] content, ref int offset, out BothUInt8 value) - { - if (offset > content.Length - 2) - { - value = default(byte); - return false; - } - - value = content.ReadByteBothEndian(ref offset); - return true; - } - - /// - /// Read a UInt8[] and increment the pointer to an array - /// - public static bool TryReadBytes(this byte[] content, ref int offset, int count, out byte[] value) - { - if (offset > content.Length - count) - { - value = []; - return false; - } - - value = content.ReadBytes(ref offset, count); - return true; - } - - /// - /// Read an Int8 and increment the pointer to an array - /// - public static bool TryReadSByte(this byte[] content, ref int offset, out sbyte value) - { - if (offset > content.Length - 1) - { - value = default; - return false; - } - - value = content.ReadSByte(ref offset); - return true; - } - - /// - /// Read a Int8 and increment the pointer to an array - /// - /// Reads in both-endian format - public static bool TryReadSByteBothEndian(this byte[] content, ref int offset, out BothInt8 value) - { - if (offset > content.Length - 2) - { - value = default(sbyte); - return false; - } - - value = content.ReadSByteBothEndian(ref offset); - return true; - } - - /// - /// Read a Char and increment the pointer to an array - /// - public static bool TryReadChar(this byte[] content, ref int offset, out char value) - { - if (offset > content.Length - 1) - { - value = default; - return false; - } - - value = content.ReadChar(ref offset); - return true; - } - - /// - /// Read an Int16 and increment the pointer to an array - /// - /// Reads in machine native format - public static bool TryReadInt16(this byte[] content, ref int offset, out short value) - { - if (BitConverter.IsLittleEndian) - return content.TryReadInt16LittleEndian(ref offset, out value); - else - return content.TryReadInt16BigEndian(ref offset, out value); - } - - /// - /// Read an Int16 and increment the pointer to an array - /// - /// Reads in big-endian format - public static bool TryReadInt16BigEndian(this byte[] content, ref int offset, out short value) - { - if (offset > content.Length - 2) - { - value = default; - return false; - } - - value = content.ReadInt16BigEndian(ref offset); - return true; - } - - /// - /// Read an Int16 and increment the pointer to an array - /// - /// Reads in little-endian format - public static bool TryReadInt16LittleEndian(this byte[] content, ref int offset, out short value) - { - if (offset > content.Length - 2) - { - value = default; - return false; - } - - value = content.ReadInt16LittleEndian(ref offset); - return true; - } - - /// - /// Read a Int16 and increment the pointer to an array - /// - /// Reads in both-endian format - public static bool TryReadInt16BothEndian(this byte[] content, ref int offset, out BothInt16 value) - { - if (offset > content.Length - 4) - { - value = default(short); - return false; - } - - value = content.ReadInt16BothEndian(ref offset); - return true; - } - - /// - /// Read a UInt16 and increment the pointer to an array - /// - /// Reads in machine native format - public static bool TryReadUInt16(this byte[] content, ref int offset, out ushort value) - { - if (BitConverter.IsLittleEndian) - return content.TryReadUInt16LittleEndian(ref offset, out value); - else - return content.TryReadUInt16BigEndian(ref offset, out value); - } - - /// - /// Read a UInt16 and increment the pointer to an array - /// - /// Reads in big-endian format - public static bool TryReadUInt16BigEndian(this byte[] content, ref int offset, out ushort value) - { - if (offset > content.Length - 2) - { - value = default; - return false; - } - - value = content.ReadUInt16BigEndian(ref offset); - return true; - } - - /// - /// Read a UInt16 and increment the pointer to an array - /// - /// Reads in little-endian format - public static bool TryReadUInt16LittleEndian(this byte[] content, ref int offset, out ushort value) - { - if (offset > content.Length - 2) - { - value = default; - return false; - } - - value = content.ReadUInt16LittleEndian(ref offset); - return true; - } - - /// - /// Read a UInt16 and increment the pointer to an array - /// - /// Reads in both-endian format - public static bool TryReadUInt16BothEndian(this byte[] content, ref int offset, out BothUInt16 value) - { - if (offset > content.Length - 4) - { - value = default(ushort); - return false; - } - - value = content.ReadUInt16BothEndian(ref offset); - return true; - } - - /// - /// Read a WORD (2-byte) and increment the pointer to an array - /// - /// Reads in machine native format - public static bool TryReadWORD(this byte[] content, ref int offset, out ushort value) - => content.TryReadUInt16(ref offset, out value); - - /// - /// Read a WORD (2-byte) and increment the pointer to an array - /// - /// Reads in big-endian format - public static bool TryReadWORDBigEndian(this byte[] content, ref int offset, out ushort value) - => content.TryReadUInt16BigEndian(ref offset, out value); - - /// - /// Read a WORD (2-byte) and increment the pointer to an array - /// - /// Reads in little-endian format - public static bool TryReadWORDLittleEndian(this byte[] content, ref int offset, out ushort value) - => content.TryReadUInt16LittleEndian(ref offset, out value); - - /// - /// Read a WORD (2-byte) and increment the pointer to an array - /// - /// Reads in both-endian format - public static bool TryReadWORDBothEndian(this byte[] content, ref int offset, out BothUInt16 value) - => content.TryReadUInt16BothEndian(ref offset, out value); - -#if NET5_0_OR_GREATER - /// - /// Read a Half and increment the pointer to an array - /// - /// Reads in machine native format - public static bool TryReadHalf(this byte[] content, ref int offset, out Half value) - { - if (BitConverter.IsLittleEndian) - return content.TryReadHalfLittleEndian(ref offset, out value); - else - return content.TryReadHalfBigEndian(ref offset, out value); - } - - /// - /// Read a Half and increment the pointer to an array - /// - /// Reads in big-endian format - public static bool TryReadHalfBigEndian(this byte[] content, ref int offset, out Half value) - { - if (offset > content.Length - 2) - { - value = default; - return false; - } - - value = content.ReadHalfBigEndian(ref offset); - return true; - } - - /// - /// Read a Half and increment the pointer to an array - /// - /// Reads in little-endian format - public static bool TryReadHalfLittleEndian(this byte[] content, ref int offset, out Half value) - { - if (offset > content.Length - 2) - { - value = default; - return false; - } - - value = content.ReadHalfLittleEndian(ref offset); - return true; - } -#endif - - /// - /// Read an Int24 and increment the pointer to an array - /// - /// Reads in machine native format - public static bool TryReadInt24(this byte[] content, ref int offset, out Int24 value) - { - if (BitConverter.IsLittleEndian) - return content.TryReadInt24LittleEndian(ref offset, out value); - else - return content.TryReadInt24BigEndian(ref offset, out value); - } - - /// - /// Read an Int24 and increment the pointer to an array - /// - /// Reads in big-endian format - public static bool TryReadInt24BigEndian(this byte[] content, ref int offset, out Int24 value) - { - if (offset > content.Length - 3) - { - value = new Int24(); - return false; - } - - value = content.ReadInt24BigEndian(ref offset); - return true; - } - - /// - /// Read an Int24 and increment the pointer to an array - /// - /// Reads in little-endian format - public static bool TryReadInt24LittleEndian(this byte[] content, ref int offset, out Int24 value) - { - if (offset > content.Length - 3) - { - value = new Int24(); - return false; - } - - value = content.ReadInt24LittleEndian(ref offset); - return true; - } - - /// - /// Read a UInt24 and increment the pointer to an array - /// - /// Reads in machine native format - public static bool TryReadUInt24(this byte[] content, ref int offset, out UInt24 value) - { - if (BitConverter.IsLittleEndian) - return content.TryReadUInt24LittleEndian(ref offset, out value); - else - return content.TryReadUInt24BigEndian(ref offset, out value); - } - - /// - /// Read a UInt24 and increment the pointer to an array - /// - /// Reads in big-endian format - public static bool TryReadUInt24BigEndian(this byte[] content, ref int offset, out UInt24 value) - { - if (offset > content.Length - 3) - { - value = new UInt24(); - return false; - } - - value = content.ReadUInt24BigEndian(ref offset); - return true; - } - - /// - /// Read a UInt24 and increment the pointer to an array - /// - /// Reads in little-endian format - public static bool TryReadUInt24LittleEndian(this byte[] content, ref int offset, out UInt24 value) - { - if (offset > content.Length - 3) - { - value = new UInt24(); - return false; - } - - value = content.ReadUInt24LittleEndian(ref offset); - return true; - } - - /// - /// Read an Int32 and increment the pointer to an array - /// - /// Reads in machine native format - public static bool TryReadInt32(this byte[] content, ref int offset, out int value) - { - if (BitConverter.IsLittleEndian) - return content.TryReadInt32LittleEndian(ref offset, out value); - else - return content.TryReadInt32BigEndian(ref offset, out value); - } - - /// - /// Read an Int32 and increment the pointer to an array - /// - /// Reads in big-endian format - public static bool TryReadInt32BigEndian(this byte[] content, ref int offset, out int value) - { - if (offset > content.Length - 4) - { - value = default; - return false; - } - - value = content.ReadInt32BigEndian(ref offset); - return true; - } - - /// - /// Read an Int32 and increment the pointer to an array - /// - /// Reads in little-endian format - public static bool TryReadInt32LittleEndian(this byte[] content, ref int offset, out int value) - { - if (offset > content.Length - 4) - { - value = default; - return false; - } - - value = content.ReadInt32LittleEndian(ref offset); - return true; - } - - /// - /// Read a Int32 and increment the pointer to an array - /// - /// Reads in both-endian format - public static bool TryReadInt32BothEndian(this byte[] content, ref int offset, out BothInt32 value) - { - if (offset > content.Length - 8) - { - value = default(int); - return false; - } - - value = content.ReadInt32BothEndian(ref offset); - return true; - } - - /// - /// Read a UInt32 and increment the pointer to an array - /// - /// Reads in machine native format - public static bool TryReadUInt32(this byte[] content, ref int offset, out uint value) - { - if (BitConverter.IsLittleEndian) - return content.TryReadUInt32LittleEndian(ref offset, out value); - else - return content.TryReadUInt32BigEndian(ref offset, out value); - } - - /// - /// Read a UInt32 and increment the pointer to an array - /// - /// Reads in big-endian format - public static bool TryReadUInt32BigEndian(this byte[] content, ref int offset, out uint value) - { - if (offset > content.Length - 4) - { - value = default; - return false; - } - - value = content.ReadUInt32BigEndian(ref offset); - return true; - } - - /// - /// Read a UInt32 and increment the pointer to an array - /// - /// Reads in little-endian format - public static bool TryReadUInt32LittleEndian(this byte[] content, ref int offset, out uint value) - { - if (offset > content.Length - 4) - { - value = default; - return false; - } - - value = content.ReadUInt32LittleEndian(ref offset); - return true; - } - - /// - /// Read a UInt32 and increment the pointer to an array - /// - /// Reads in both-endian format - public static bool TryReadUInt32BothEndian(this byte[] content, ref int offset, out BothUInt32 value) - { - if (offset > content.Length - 8) - { - value = default(uint); - return false; - } - - value = content.ReadUInt32BothEndian(ref offset); - return true; - } - - /// - /// Read a DWORD (4-byte) and increment the pointer to an array - /// - /// Reads in machine native format - public static bool TryReadDWORD(this byte[] content, ref int offset, out uint value) - => content.TryReadUInt32(ref offset, out value); - - /// - /// Read a DWORD (4-byte) and increment the pointer to an array - /// - /// Reads in big-endian format - public static bool TryReadDWORDBigEndian(this byte[] content, ref int offset, out uint value) - => content.TryReadUInt32BigEndian(ref offset, out value); - - /// - /// Read a DWORD (4-byte) and increment the pointer to an array - /// - /// Reads in little-endian format - public static bool TryReadDWORDLittleEndian(this byte[] content, ref int offset, out uint value) - => content.TryReadUInt32LittleEndian(ref offset, out value); - - /// - /// Read a DWORD (4-byte) and increment the pointer to an array - /// - /// Reads in both-endian format - public static bool TryReadDWORDBothEndian(this byte[] content, ref int offset, out BothUInt32 value) - => content.TryReadUInt32BothEndian(ref offset, out value); - - /// - /// Read a Single and increment the pointer to an array - /// - /// Reads in machine native format - public static bool TryReadSingle(this byte[] content, ref int offset, out float value) - { - if (BitConverter.IsLittleEndian) - return content.TryReadSingleLittleEndian(ref offset, out value); - else - return content.TryReadSingleBigEndian(ref offset, out value); - } - - /// - /// Read a Single and increment the pointer to an array - /// - /// Reads in big-endian format - public static bool TryReadSingleBigEndian(this byte[] content, ref int offset, out float value) - { - if (offset > content.Length - 4) - { - value = default; - return false; - } - - value = content.ReadSingleBigEndian(ref offset); - return true; - } - - /// - /// Read a Single and increment the pointer to an array - /// - /// Reads in little-endian format - public static bool TryReadSingleLittleEndian(this byte[] content, ref int offset, out float value) - { - if (offset > content.Length - 4) - { - value = default; - return false; - } - - value = content.ReadSingleLittleEndian(ref offset); - return true; - } - - /// - /// Read an Int48 and increment the pointer to an array - /// - /// Reads in machine native format - public static bool TryReadInt48(this byte[] content, ref int offset, out Int48 value) - { - if (BitConverter.IsLittleEndian) - return content.TryReadInt48LittleEndian(ref offset, out value); - else - return content.TryReadInt48BigEndian(ref offset, out value); - } - - /// - /// Read an Int48 and increment the pointer to an array - /// - /// Reads in big-endian format - public static bool TryReadInt48BigEndian(this byte[] content, ref int offset, out Int48 value) - { - if (offset > content.Length - 6) - { - value = new Int48(); - return false; - } - - value = content.ReadInt48BigEndian(ref offset); - return true; - } - - /// - /// Read an Int48 and increment the pointer to an array - /// - /// Reads in little-endian format - public static bool TryReadInt48LittleEndian(this byte[] content, ref int offset, out Int48 value) - { - if (offset > content.Length - 6) - { - value = new Int48(); - return false; - } - - value = content.ReadInt48LittleEndian(ref offset); - return true; - } - - /// - /// Read a UInt48 and increment the pointer to an array - /// - /// Reads in machine native format - public static bool TryReadUInt48(this byte[] content, ref int offset, out UInt48 value) - { - if (BitConverter.IsLittleEndian) - return content.TryReadUInt48LittleEndian(ref offset, out value); - else - return content.TryReadUInt48BigEndian(ref offset, out value); - } - - /// - /// Read a UInt48 and increment the pointer to an array - /// - /// Reads in big-endian format - public static bool TryReadUInt48BigEndian(this byte[] content, ref int offset, out UInt48 value) - { - if (offset > content.Length - 6) - { - value = new UInt48(); - return false; - } - - value = content.ReadUInt48BigEndian(ref offset); - return true; - } - - /// - /// Read an UInt48 and increment the pointer to an array - /// - /// Reads in little-endian format - public static bool TryReadUInt48LittleEndian(this byte[] content, ref int offset, out UInt48 value) - { - if (offset > content.Length - 6) - { - value = new UInt48(); - return false; - } - - value = content.ReadUInt48LittleEndian(ref offset); - return true; - } - - /// - /// Read an Int64 and increment the pointer to an array - /// - /// Reads in machine native format - public static bool TryReadInt64(this byte[] content, ref int offset, out long value) - { - if (BitConverter.IsLittleEndian) - return content.TryReadInt64LittleEndian(ref offset, out value); - else - return content.TryReadInt64BigEndian(ref offset, out value); - } - - /// - /// Read an Int64 and increment the pointer to an array - /// - /// Reads in big-endian format - public static bool TryReadInt64BigEndian(this byte[] content, ref int offset, out long value) - { - if (offset > content.Length - 8) - { - value = default; - return false; - } - - value = content.ReadInt64BigEndian(ref offset); - return true; - } - - /// - /// Read an Int64 and increment the pointer to an array - /// - /// Reads in little-endian format - public static bool TryReadInt64LittleEndian(this byte[] content, ref int offset, out long value) - { - if (offset > content.Length - 8) - { - value = default; - return false; - } - - value = content.ReadInt64BigEndian(ref offset); - return true; - } - - /// - /// Read a Int64 and increment the pointer to an array - /// - /// Reads in both-endian format - public static bool TryReadInt64BothEndian(this byte[] content, ref int offset, out BothInt64 value) - { - if (offset > content.Length - 16) - { - value = default(long); - return false; - } - - value = content.ReadInt64BothEndian(ref offset); - return true; - } - - /// - /// Read a UInt64 and increment the pointer to an array - /// - /// Reads in machine native format - public static bool TryReadUInt64(this byte[] content, ref int offset, out ulong value) - { - if (BitConverter.IsLittleEndian) - return content.TryReadUInt64LittleEndian(ref offset, out value); - else - return content.TryReadUInt64BigEndian(ref offset, out value); - } - - /// - /// Read a UInt64 and increment the pointer to an array - /// - /// Reads in big-endian format - public static bool TryReadUInt64BigEndian(this byte[] content, ref int offset, out ulong value) - { - if (offset > content.Length - 8) - { - value = default; - return false; - } - - value = content.ReadUInt64BigEndian(ref offset); - return true; - } - - /// - /// Read a UInt64 and increment the pointer to an array - /// - /// Reads in little-endian format - public static bool TryReadUInt64LittleEndian(this byte[] content, ref int offset, out ulong value) - { - if (offset > content.Length - 8) - { - value = default; - return false; - } - - value = content.ReadUInt64LittleEndian(ref offset); - return true; - } - - /// - /// Read a UInt64 and increment the pointer to an array - /// - /// Reads in both-endian format - public static bool TryReadUInt64BothEndian(this byte[] content, ref int offset, out BothUInt64 value) - { - if (offset > content.Length - 16) - { - value = default(ulong); - return false; - } - - value = content.ReadUInt64BothEndian(ref offset); - return true; - } - - /// - /// Read a QWORD (8-byte) and increment the pointer to an array - /// - /// Reads in machine native format - public static bool TryReadQWORD(this byte[] content, ref int offset, out ulong value) - => content.TryReadUInt64(ref offset, out value); - - /// - /// Read a QWORD (8-byte) and increment the pointer to an array - /// - /// Reads in big-endian format - public static bool TryReadQWORDBigEndian(this byte[] content, ref int offset, out ulong value) - => content.TryReadUInt64BigEndian(ref offset, out value); - - /// - /// Read a QWORD (8-byte) and increment the pointer to an array - /// - /// Reads in little-endian format - public static bool TryReadQWORDLittleEndian(this byte[] content, ref int offset, out ulong value) - => content.TryReadUInt64LittleEndian(ref offset, out value); - - /// - /// Read a QWORD (8-byte) and increment the pointer to an array - /// - /// Reads in both-endian format - public static bool TryReadQWORDBothEndian(this byte[] content, ref int offset, out BothUInt64 value) - => content.TryReadUInt64BothEndian(ref offset, out value); - - /// - /// Read a Double and increment the pointer to an array - /// - /// Reads in machine native format - public static bool TryReadDouble(this byte[] content, ref int offset, out double value) - { - if (BitConverter.IsLittleEndian) - return content.TryReadDoubleLittleEndian(ref offset, out value); - else - return content.TryReadDoubleBigEndian(ref offset, out value); - } - - /// - /// Read a Double and increment the pointer to an array - /// - /// Reads in big-endian format - public static bool TryReadDoubleBigEndian(this byte[] content, ref int offset, out double value) - { - if (offset > content.Length - 8) - { - value = default; - return false; - } - - value = content.ReadDoubleBigEndian(ref offset); - return true; - } - - /// - /// Read a Double and increment the pointer to an array - /// - /// Reads in big-endian format - public static bool TryReadDoubleLittleEndian(this byte[] content, ref int offset, out double value) - { - if (offset > content.Length - 8) - { - value = default; - return false; - } - - value = content.ReadDoubleLittleEndian(ref offset); - return true; - } - - /// - /// Read a Guid and increment the pointer to an array - /// - /// Reads in machine native format - public static bool TryReadGuid(this byte[] content, ref int offset, out Guid value) - { - if (BitConverter.IsLittleEndian) - return content.TryReadGuidLittleEndian(ref offset, out value); - else - return content.TryReadGuidBigEndian(ref offset, out value); - } - - /// - /// Read a Guid and increment the pointer to an array - /// - /// Reads in big-endian format - public static bool TryReadGuidBigEndian(this byte[] content, ref int offset, out Guid value) - { - if (offset > content.Length - 16) - { - value = default; - return false; - } - - value = content.ReadGuidBigEndian(ref offset); - return true; - } - - /// - /// Read a Guid and increment the pointer to an array - /// - /// Reads in little-endian format - public static bool TryReadGuidLittleEndian(this byte[] content, ref int offset, out Guid value) - { - if (offset > content.Length - 16) - { - value = default; - return false; - } - - value = content.ReadGuidLittleEndian(ref offset); - return true; - } - -#if NET7_0_OR_GREATER - /// - /// Read an Int128 and increment the pointer to an array - /// - /// Reads in machine native format - public static bool TryReadInt128(this byte[] content, ref int offset, out Int128 value) - { - if (BitConverter.IsLittleEndian) - return content.TryReadInt128LittleEndian(ref offset, out value); - else - return content.TryReadInt128BigEndian(ref offset, out value); - } - - /// - /// Read an Int128 and increment the pointer to an array - /// - /// Reads in big-endian format - public static bool TryReadInt128BigEndian(this byte[] content, ref int offset, out Int128 value) - { - if (offset > content.Length - 16) - { - value = default; - return false; - } - - value = content.ReadInt128BigEndian(ref offset); - return true; - } - - /// - /// Read an Int128 and increment the pointer to an array - /// - /// Reads in little-endian format - public static bool TryReadInt128LittleEndian(this byte[] content, ref int offset, out Int128 value) - { - if (offset > content.Length - 16) - { - value = default; - return false; - } - - value = content.ReadInt128LittleEndian(ref offset); - return true; - } - - /// - /// Read a UInt128 and increment the pointer to an array - /// - /// Reads in machine native format - public static bool TryReadUInt128(this byte[] content, ref int offset, out UInt128 value) - { - if (BitConverter.IsLittleEndian) - return content.TryReadUInt128LittleEndian(ref offset, out value); - else - return content.TryReadUInt128BigEndian(ref offset, out value); - } - - /// - /// Read a UInt128 and increment the pointer to an array - /// - /// Reads in big-endian format - public static bool TryReadUInt128BigEndian(this byte[] content, ref int offset, out UInt128 value) - { - if (offset > content.Length - 16) - { - value = default; - return false; - } - - value = content.ReadUInt128BigEndian(ref offset); - return true; - } - - /// - /// Read a UInt128 and increment the pointer to an array - /// - /// Reads in little-endian format - public static bool TryReadUInt128LittleEndian(this byte[] content, ref int offset, out UInt128 value) - { - if (offset > content.Length - 16) - { - value = default; - return false; - } - - value = content.ReadUInt128LittleEndian(ref offset); - return true; - } -#endif - - /// - /// Read a Decimal and increment the pointer to an array - /// - /// Reads in machine native format - public static bool TryReadDecimal(this byte[] content, ref int offset, out decimal value) - { - if (BitConverter.IsLittleEndian) - return content.TryReadDecimalLittleEndian(ref offset, out value); - else - return content.TryReadDecimalBigEndian(ref offset, out value); - } - - /// - /// Read a Decimal and increment the pointer to an array - /// - /// Reads in big-endian format - public static bool TryReadDecimalBigEndian(this byte[] content, ref int offset, out decimal value) - { - if (offset > content.Length - 16) - { - value = default; - return false; - } - - value = content.ReadDecimalBigEndian(ref offset); - return true; - } - - /// - /// Read a Decimal and increment the pointer to an array - /// - /// Reads in little-endian format - public static bool TryReadDecimalLittleEndian(this byte[] content, ref int offset, out decimal value) - { - if (offset > content.Length - 16) - { - value = default; - return false; - } - - value = content.ReadDecimalLittleEndian(ref offset); - return true; - } - - #endregion } } diff --git a/SabreTools.IO.Extensions/ByteArrayWriterExtensions.cs b/SabreTools.IO.Extensions/ByteArrayWriterExtensions.cs index d4cc8ee..8f3a8a8 100644 --- a/SabreTools.IO.Extensions/ByteArrayWriterExtensions.cs +++ b/SabreTools.IO.Extensions/ByteArrayWriterExtensions.cs @@ -2,7 +2,7 @@ using System; using System.Reflection; using System.Runtime.InteropServices; using System.Text; -using SabreTools.Numerics; +using SabreTools.Numerics.Extensions; namespace SabreTools.IO.Extensions { @@ -11,688 +11,6 @@ namespace SabreTools.IO.Extensions /// public static class ByteArrayWriterExtensions { - /// - /// Write a UInt8 and increment the pointer to an array - /// - public static bool Write(this byte[] content, ref int offset, byte value) - => WriteFromBuffer(content, ref offset, [value]); - - /// - /// Write a UInt8 and increment the pointer to an array - /// - /// Writes in both-endian format - public static bool WriteBothEndian(this byte[] content, ref int offset, BothUInt8 value) - { - bool actual = content.Write(ref offset, value.LittleEndian); - actual &= content.Write(ref offset, value.BigEndian); - return actual; - } - - /// - /// Write a UInt8[] and increment the pointer to an array - /// - public static bool Write(this byte[] content, ref int offset, byte[] value) - => WriteFromBuffer(content, ref offset, value); - - /// - /// Write a UInt8[] and increment the pointer to an array - /// - /// Writes in big-endian format - public static bool WriteBigEndian(this byte[] content, ref int offset, byte[] value) - { - Array.Reverse(value); - return WriteFromBuffer(content, ref offset, value); - } - - /// - /// Write an Int8 and increment the pointer to an array - /// - public static bool Write(this byte[] content, ref int offset, sbyte value) - => WriteFromBuffer(content, ref offset, [(byte)value]); - - /// - /// Write a Int8 and increment the pointer to an array - /// - /// Writes in both-endian format - public static bool WriteBothEndian(this byte[] content, ref int offset, BothInt8 value) - { - bool actual = content.Write(ref offset, value.LittleEndian); - actual &= content.Write(ref offset, value.BigEndian); - return actual; - } - - /// - /// Write a Char and increment the pointer to an array - /// - public static bool Write(this byte[] content, ref int offset, char value) - { - byte[] buffer = BitConverter.GetBytes(value); - return WriteFromBuffer(content, ref offset, buffer); - } - - /// - /// Write a Char with an Encoding and increment the pointer to an array - /// - public static bool Write(this byte[] content, ref int offset, char value, Encoding encoding) - { - byte[] buffer = encoding.GetBytes($"{value}"); - return WriteFromBuffer(content, ref offset, buffer); - } - - /// - /// Write an Int16 and increment the pointer to an array - /// - /// Writes in machine native format - public static bool Write(this byte[] content, ref int offset, short value) - { - if (BitConverter.IsLittleEndian) - return content.WriteLittleEndian(ref offset, value); - else - return content.WriteBigEndian(ref offset, value); - } - - /// - /// Write an Int16 and increment the pointer to an array - /// - /// Writes in big-endian format - public static bool WriteBigEndian(this byte[] content, ref int offset, short value) - { - byte[] buffer = value.GetBytesBigEndian(); - return WriteFromBuffer(content, ref offset, buffer); - } - - /// - /// Write an Int16 and increment the pointer to an array - /// - /// Writes in little-endian format - public static bool WriteLittleEndian(this byte[] content, ref int offset, short value) - { - byte[] buffer = value.GetBytesLittleEndian(); - return WriteFromBuffer(content, ref offset, buffer); - } - - /// - /// Write a Int16 and increment the pointer to an array - /// - /// Writes in both-endian format - public static bool WriteBothEndian(this byte[] content, ref int offset, BothInt16 value) - { - bool actual = content.WriteLittleEndian(ref offset, value.LittleEndian); - actual &= content.WriteBigEndian(ref offset, value.BigEndian); - return actual; - } - - /// - /// Write a UInt16 and increment the pointer to an array - /// - /// Writes in machine native format - public static bool Write(this byte[] content, ref int offset, ushort value) - { - if (BitConverter.IsLittleEndian) - return content.WriteLittleEndian(ref offset, value); - else - return content.WriteBigEndian(ref offset, value); - } - - /// - /// Write a UInt16 and increment the pointer to an array - /// - /// Writes in big-endian format - public static bool WriteBigEndian(this byte[] content, ref int offset, ushort value) - { - byte[] buffer = value.GetBytesBigEndian(); - return WriteFromBuffer(content, ref offset, buffer); - } - - /// - /// Write a UInt16 and increment the pointer to an array - /// - /// Writes in little-endian format - public static bool WriteLittleEndian(this byte[] content, ref int offset, ushort value) - { - byte[] buffer = value.GetBytesLittleEndian(); - return WriteFromBuffer(content, ref offset, buffer); - } - - /// - /// Write a UInt16 and increment the pointer to an array - /// - /// Writes in both-endian format - public static bool WriteBothEndian(this byte[] content, ref int offset, BothUInt16 value) - { - bool actual = content.WriteLittleEndian(ref offset, value.LittleEndian); - actual &= content.WriteBigEndian(ref offset, value.BigEndian); - return actual; - } - -#if NET5_0_OR_GREATER - /// - /// Write a Half and increment the pointer to an array - /// - /// Writes in machine native format - public static bool Write(this byte[] content, ref int offset, Half value) - { - if (BitConverter.IsLittleEndian) - return content.WriteLittleEndian(ref offset, value); - else - return content.WriteBigEndian(ref offset, value); - } - - /// - /// Write a Half and increment the pointer to an array - /// - /// Writes in big-endian format - public static bool WriteBigEndian(this byte[] content, ref int offset, Half value) - { - byte[] buffer = value.GetBytesBigEndian(); - return WriteFromBuffer(content, ref offset, buffer); - } - - /// - /// Write a Half and increment the pointer to an array - /// - /// Writes in little-endian format - public static bool WriteLittleEndian(this byte[] content, ref int offset, Half value) - { - byte[] buffer = value.GetBytesLittleEndian(); - return WriteFromBuffer(content, ref offset, buffer); - } -#endif - - /// - /// Write an Int24 and increment the pointer to an array - /// - /// Writes in machine native format - public static bool Write(this byte[] content, ref int offset, Int24 value) - { - if (BitConverter.IsLittleEndian) - return content.WriteLittleEndian(ref offset, value); - else - return content.WriteBigEndian(ref offset, value); - } - - /// - /// Write an Int24 and increment the pointer to an array - /// - /// Writes in big-endian format - public static bool WriteBigEndian(this byte[] content, ref int offset, Int24 value) - { - byte[] buffer = value.GetBytesBigEndian(); - return WriteFromBuffer(content, ref offset, buffer); - } - - /// - /// Write an Int24 and increment the pointer to an array - /// - /// Writes in little-endian format - public static bool WriteLittleEndian(this byte[] content, ref int offset, Int24 value) - { - byte[] buffer = value.GetBytesLittleEndian(); - return WriteFromBuffer(content, ref offset, buffer); - } - - /// - /// Write a UInt24 and increment the pointer to an array - /// - /// Writes in machine native format - public static bool Write(this byte[] content, ref int offset, UInt24 value) - { - if (BitConverter.IsLittleEndian) - return content.WriteLittleEndian(ref offset, value); - else - return content.WriteBigEndian(ref offset, value); - } - - /// - /// Write a UInt24 and increment the pointer to an array - /// - /// Writes in big-endian format - public static bool WriteBigEndian(this byte[] content, ref int offset, UInt24 value) - { - byte[] buffer = value.GetBytesBigEndian(); - return WriteFromBuffer(content, ref offset, buffer); - } - - /// - /// Write a UInt24 and increment the pointer to an array - /// - /// Writes in little-endian format - public static bool WriteLittleEndian(this byte[] content, ref int offset, UInt24 value) - { - byte[] buffer = value.GetBytesLittleEndian(); - return WriteFromBuffer(content, ref offset, buffer); - } - - /// - /// Write an Int32 and increment the pointer to an array - /// - /// Writes in machine native format - public static bool Write(this byte[] content, ref int offset, int value) - { - if (BitConverter.IsLittleEndian) - return content.WriteLittleEndian(ref offset, value); - else - return content.WriteBigEndian(ref offset, value); - } - - /// - /// Write an Int32 and increment the pointer to an array - /// - /// Writes in big-endian format - public static bool WriteBigEndian(this byte[] content, ref int offset, int value) - { - byte[] buffer = value.GetBytesBigEndian(); - return WriteFromBuffer(content, ref offset, buffer); - } - - /// - /// Write an Int32 and increment the pointer to an array - /// - /// Writes in little-endian format - public static bool WriteLittleEndian(this byte[] content, ref int offset, int value) - { - byte[] buffer = value.GetBytesLittleEndian(); - return WriteFromBuffer(content, ref offset, buffer); - } - - /// - /// Write a Int32 and increment the pointer to an array - /// - /// Writes in both-endian format - public static bool WriteBothEndian(this byte[] content, ref int offset, BothInt32 value) - { - bool actual = content.WriteLittleEndian(ref offset, value.LittleEndian); - actual &= content.WriteBigEndian(ref offset, value.BigEndian); - return actual; - } - - /// - /// Write a UInt32 and increment the pointer to an array - /// - /// Writes in machine native format - public static bool Write(this byte[] content, ref int offset, uint value) - { - if (BitConverter.IsLittleEndian) - return content.WriteLittleEndian(ref offset, value); - else - return content.WriteBigEndian(ref offset, value); - } - - /// - /// Write a UInt32 and increment the pointer to an array - /// - /// Writes in big-endian format - public static bool WriteBigEndian(this byte[] content, ref int offset, uint value) - { - byte[] buffer = value.GetBytesBigEndian(); - return WriteFromBuffer(content, ref offset, buffer); - } - - /// - /// Write a UInt32 and increment the pointer to an array - /// - /// Writes in little-endian format - public static bool WriteLittleEndian(this byte[] content, ref int offset, uint value) - { - byte[] buffer = value.GetBytesLittleEndian(); - return WriteFromBuffer(content, ref offset, buffer); - } - - /// - /// Write a UInt32 and increment the pointer to an array - /// - /// Writes in both-endian format - public static bool WriteBothEndian(this byte[] content, ref int offset, BothUInt32 value) - { - bool actual = content.WriteLittleEndian(ref offset, value.LittleEndian); - actual &= content.WriteBigEndian(ref offset, value.BigEndian); - return actual; - } - - /// - /// Write a Single and increment the pointer to an array - /// - /// Writes in machine native format - public static bool Write(this byte[] content, ref int offset, float value) - { - if (BitConverter.IsLittleEndian) - return content.WriteLittleEndian(ref offset, value); - else - return content.WriteBigEndian(ref offset, value); - } - - /// - /// Write a Single and increment the pointer to an array - /// - /// Writes in big-endian format - public static bool WriteBigEndian(this byte[] content, ref int offset, float value) - { - byte[] buffer = value.GetBytesBigEndian(); - return WriteFromBuffer(content, ref offset, buffer); - } - - /// - /// Write a Single and increment the pointer to an array - /// - /// Writes in little-endian format - public static bool WriteLittleEndian(this byte[] content, ref int offset, float value) - { - byte[] buffer = value.GetBytesLittleEndian(); - return WriteFromBuffer(content, ref offset, buffer); - } - - /// - /// Write an Int48 and increment the pointer to an array - /// - /// Writes in machine native format - public static bool Write(this byte[] content, ref int offset, Int48 value) - { - if (BitConverter.IsLittleEndian) - return content.WriteLittleEndian(ref offset, value); - else - return content.WriteBigEndian(ref offset, value); - } - - /// - /// Write an Int48 and increment the pointer to an array - /// - /// Writes in big-endian format - public static bool WriteBigEndian(this byte[] content, ref int offset, Int48 value) - { - byte[] buffer = value.GetBytesBigEndian(); - return WriteFromBuffer(content, ref offset, buffer); - } - - /// - /// Write an Int48 and increment the pointer to an array - /// - /// Writes in little-endian format - public static bool WriteLittleEndian(this byte[] content, ref int offset, Int48 value) - { - byte[] buffer = value.GetBytesLittleEndian(); - return WriteFromBuffer(content, ref offset, buffer); - } - - /// - /// Write a UInt48 and increment the pointer to an array - /// - /// Writes in machine native format - public static bool Write(this byte[] content, ref int offset, UInt48 value) - { - if (BitConverter.IsLittleEndian) - return content.WriteLittleEndian(ref offset, value); - else - return content.WriteBigEndian(ref offset, value); - } - - /// - /// Write a UInt48 and increment the pointer to an array - /// - /// Writes in big-endian format - public static bool WriteBigEndian(this byte[] content, ref int offset, UInt48 value) - { - byte[] buffer = value.GetBytesBigEndian(); - return WriteFromBuffer(content, ref offset, buffer); - } - - /// - /// Write a UInt48 and increment the pointer to an array - /// - /// Writes in little-endian format - public static bool WriteLittleEndian(this byte[] content, ref int offset, UInt48 value) - { - byte[] buffer = value.GetBytesLittleEndian(); - return WriteFromBuffer(content, ref offset, buffer); - } - - /// - /// Write an Int64 and increment the pointer to an array - /// - /// Writes in machine native format - public static bool Write(this byte[] content, ref int offset, long value) - { - if (BitConverter.IsLittleEndian) - return content.WriteLittleEndian(ref offset, value); - else - return content.WriteBigEndian(ref offset, value); - } - - /// - /// Write an Int64 and increment the pointer to an array - /// - /// Writes in big-endian format - public static bool WriteBigEndian(this byte[] content, ref int offset, long value) - { - byte[] buffer = value.GetBytesBigEndian(); - return WriteFromBuffer(content, ref offset, buffer); - } - - /// - /// Write an Int64 and increment the pointer to an array - /// - /// Writes in little-endian format - public static bool WriteLittleEndian(this byte[] content, ref int offset, long value) - { - byte[] buffer = value.GetBytesLittleEndian(); - return WriteFromBuffer(content, ref offset, buffer); - } - - /// - /// Write a Int64 and increment the pointer to an array - /// - /// Writes in both-endian format - public static bool WriteBothEndian(this byte[] content, ref int offset, BothInt64 value) - { - bool actual = content.WriteLittleEndian(ref offset, value.LittleEndian); - actual &= content.WriteBigEndian(ref offset, value.BigEndian); - return actual; - } - - /// - /// Write a UInt64 and increment the pointer to an array - /// - /// Writes in machine native format - public static bool Write(this byte[] content, ref int offset, ulong value) - { - if (BitConverter.IsLittleEndian) - return content.WriteLittleEndian(ref offset, value); - else - return content.WriteBigEndian(ref offset, value); - } - - /// - /// Write a UInt64 and increment the pointer to an array - /// - /// Writes in big-endian format - public static bool WriteBigEndian(this byte[] content, ref int offset, ulong value) - { - byte[] buffer = value.GetBytesBigEndian(); - return WriteFromBuffer(content, ref offset, buffer); - } - - /// - /// Write a UInt64 and increment the pointer to an array - /// - /// Writes in little-endian format - public static bool WriteLittleEndian(this byte[] content, ref int offset, ulong value) - { - byte[] buffer = value.GetBytesLittleEndian(); - return WriteFromBuffer(content, ref offset, buffer); - } - - /// - /// Write a UInt64 and increment the pointer to an array - /// - /// Writes in both-endian format - public static bool WriteBothEndian(this byte[] content, ref int offset, BothUInt64 value) - { - bool actual = content.WriteLittleEndian(ref offset, value.LittleEndian); - actual &= content.WriteBigEndian(ref offset, value.BigEndian); - return actual; - } - - /// - /// Write a Double and increment the pointer to an array - /// - /// Writes in machine native format - public static bool Write(this byte[] content, ref int offset, double value) - { - if (BitConverter.IsLittleEndian) - return content.WriteLittleEndian(ref offset, value); - else - return content.WriteBigEndian(ref offset, value); - } - - /// - /// Write a Double and increment the pointer to an array - /// - /// Writes in big-endian format - public static bool WriteBigEndian(this byte[] content, ref int offset, double value) - { - byte[] buffer = value.GetBytesBigEndian(); - return WriteFromBuffer(content, ref offset, buffer); - } - - /// - /// Write a Double and increment the pointer to an array - /// - /// Writes in little-endian format - public static bool WriteLittleEndian(this byte[] content, ref int offset, double value) - { - byte[] buffer = value.GetBytesLittleEndian(); - return WriteFromBuffer(content, ref offset, buffer); - } - - /// - /// Write a Guid and increment the pointer to an array - /// - /// Writes in machine native format - public static bool Write(this byte[] content, ref int offset, Guid value) - { - if (BitConverter.IsLittleEndian) - return content.WriteLittleEndian(ref offset, value); - else - return content.WriteBigEndian(ref offset, value); - } - - /// - /// Write a Guid and increment the pointer to an array - /// - /// Writes in big-endian format - public static bool WriteBigEndian(this byte[] content, ref int offset, Guid value) - { - byte[] buffer = value.GetBytesBigEndian(); - return WriteFromBuffer(content, ref offset, buffer); - } - - /// - /// Write a Guid and increment the pointer to an array - /// - /// Writes in little-endian format - public static bool WriteLittleEndian(this byte[] content, ref int offset, Guid value) - { - byte[] buffer = value.GetBytesLittleEndian(); - return WriteFromBuffer(content, ref offset, buffer); - } - -#if NET7_0_OR_GREATER - /// - /// Write an Int128 and increment the pointer to an array - /// - /// Writes in machine native format - public static bool Write(this byte[] content, ref int offset, Int128 value) - { - if (BitConverter.IsLittleEndian) - return content.WriteLittleEndian(ref offset, value); - else - return content.WriteBigEndian(ref offset, value); - } - - /// - /// Write an Int128 and increment the pointer to an array - /// - /// Writes in big-endian format - public static bool WriteBigEndian(this byte[] content, ref int offset, Int128 value) - { - byte[] buffer = value.GetBytesBigEndian(); - return WriteFromBuffer(content, ref offset, buffer); - } - - /// - /// Write an Int128 and increment the pointer to an array - /// - /// Writes in little-endian format - public static bool WriteLittleEndian(this byte[] content, ref int offset, Int128 value) - { - byte[] buffer = value.GetBytesLittleEndian(); - return WriteFromBuffer(content, ref offset, buffer); - } - - /// - /// Write a UInt128 and increment the pointer to an array - /// - /// Writes in machine native format - public static bool Write(this byte[] content, ref int offset, UInt128 value) - { - if (BitConverter.IsLittleEndian) - return content.WriteLittleEndian(ref offset, value); - else - return content.WriteBigEndian(ref offset, value); - } - - /// - /// Write a UInt128 and increment the pointer to an array - /// - /// Writes in big-endian format - public static bool WriteBigEndian(this byte[] content, ref int offset, UInt128 value) - { - byte[] buffer = value.GetBytesBigEndian(); - return WriteFromBuffer(content, ref offset, buffer); - } - - /// - /// Write a UInt128 and increment the pointer to an array - /// - /// Writes in little-endian format - public static bool WriteLittleEndian(this byte[] content, ref int offset, UInt128 value) - { - byte[] buffer = value.GetBytesLittleEndian(); - return WriteFromBuffer(content, ref offset, buffer); - } -#endif - - /// - /// Write a Decimal and increment the pointer to an array - /// - /// Writes in machine native format - public static bool Write(this byte[] content, ref int offset, decimal value) - { - if (BitConverter.IsLittleEndian) - return content.WriteLittleEndian(ref offset, value); - else - return content.WriteBigEndian(ref offset, value); - } - - /// - /// Write a Decimal and increment the pointer to an array - /// - /// Writes in big-endian format - public static bool WriteBigEndian(this byte[] content, ref int offset, decimal value) - { - byte[] buffer = value.GetBytesBigEndian(); - return WriteFromBuffer(content, ref offset, buffer); - } - - /// - /// Write a Decimal and increment the pointer to an array - /// - /// Writes in little-endian format - public static bool WriteLittleEndian(this byte[] content, ref int offset, decimal value) - { - byte[] buffer = value.GetBytesLittleEndian(); - return WriteFromBuffer(content, ref offset, buffer); - } - /// /// Write a null-terminated string to the array /// diff --git a/SabreTools.IO.Extensions/README.MD b/SabreTools.IO.Extensions/README.MD index 02f97f7..067f481 100644 --- a/SabreTools.IO.Extensions/README.MD +++ b/SabreTools.IO.Extensions/README.MD @@ -6,22 +6,21 @@ Relies on functionality found in `SabreTools.IO.Matching`, `SabreTools.Numerics` | Class | Description | | --- | --- | -| `BinaryReaderExtensions` | Extensions to `BinaryReader` allowing for endian-specific reads, peeking functionality, and try-read functionality. | -| `BinaryWriterExtensions` | Extensions to `BinaryWriter` allowing for endian-specific writes. | +| `BinaryReaderExtensions` | Extensions to `BinaryReader` for specialized string and type reading. | +| `BinaryWriterExtensions` | Extensions to `BinaryWriter` for specialized string and type writing. | | `ByteArrayExtensions` | Utility `byte[]` and `byte?[]` extensions commonly used in SabreTools projects. | -| `ByteArrayReaderExtensions` | Extensions to `byte[]` allowing for endian-specific reads, peeking functionality, and try-read functionality, similar to a stream. | -| `ByteArrayWriterExtensions` | Extensions to `byte[]` allowing for endian-specific writes, similar to a stream. | +| `ByteArrayReaderExtensions` | Extensions to `byte[]` for specialized string and type reading. | +| `ByteArrayWriterExtensions` | Extensions to `byte[]` for specialized string and type writing. | | `DateTimeExtensions` | `DateTime` conversion, specifically between standard and MS-DOS formats. | | `DictionaryExtensions` | Utility `Dictionary>` extensions commonly used in SabreTools projects. | | `EnumerableExtensions` | Specialized enumeration wrapper functionality. | | `IOExtensions` | Path, directory, and file extensions. | | `MarshalHelpers` | Internal-only reflection-based extensions used by some other extension classes. | -| `NumericExtensions` | Common numeric type extensions allowing for endian-specific processing. | | `ParentablePathExtensions` | Extensions to `SabreTools.IO.ParentablePath` for filtering. | | `ReadOnlyBitStreamExtensions` | Extensions to `SabreTools.IO.ReadOnlyBitStream` to allow non-bitwise reads. | | `StreamExtensions` | Utility `Stream` extensions commonly used in other SabreTools projects. | -| `StreamReaderExtensions` | Extensions to `Stream` allowing for endian-specific reads, peeking functionality, and try-read functionality. | -| `StreamWriterExtensions` | Extensions to `Stream` allowing for endian-specific writes. | +| `StreamReaderExtensions` | Extensions to `Stream` for specialized string and type reading. | +| `StreamWriterExtensions` | Extensions to `Stream` for specialized string and type writing. | | `StringBuilderExtensions` | Formatting overloads for `StringBuilder` to allow for type-specific handling. | | `StringExtensions` | Optional overloads to `string.Contains`, `string.EndsWith`, `string.Equals`, and `string.StartsWith`. | | `XmlTextWriterExtensions` | Overloads to `XmlTextWriter.WriteAttributeString` and `XmlTextWriter.WriteElementString`. | diff --git a/SabreTools.IO.Extensions/ReadOnlyBitStreamExtensions.cs b/SabreTools.IO.Extensions/ReadOnlyBitStreamExtensions.cs index 735bcdc..2c501e6 100644 --- a/SabreTools.IO.Extensions/ReadOnlyBitStreamExtensions.cs +++ b/SabreTools.IO.Extensions/ReadOnlyBitStreamExtensions.cs @@ -1,3 +1,5 @@ +using SabreTools.Numerics.Extensions; + namespace SabreTools.IO.Extensions { /// diff --git a/SabreTools.IO.Extensions/SabreTools.IO.Extensions.csproj b/SabreTools.IO.Extensions/SabreTools.IO.Extensions.csproj index c0e0fd1..173bf9b 100644 --- a/SabreTools.IO.Extensions/SabreTools.IO.Extensions.csproj +++ b/SabreTools.IO.Extensions/SabreTools.IO.Extensions.csproj @@ -33,6 +33,7 @@ + diff --git a/SabreTools.IO.Extensions/StreamExtensions.cs b/SabreTools.IO.Extensions/StreamExtensions.cs index 083c142..116b6da 100644 --- a/SabreTools.IO.Extensions/StreamExtensions.cs +++ b/SabreTools.IO.Extensions/StreamExtensions.cs @@ -1,5 +1,6 @@ using System.Collections.Generic; using System.IO; +using SabreTools.Numerics.Extensions; namespace SabreTools.IO.Extensions { diff --git a/SabreTools.IO.Extensions/StreamReaderExtensions.cs b/SabreTools.IO.Extensions/StreamReaderExtensions.cs index cad9b30..999934f 100644 --- a/SabreTools.IO.Extensions/StreamReaderExtensions.cs +++ b/SabreTools.IO.Extensions/StreamReaderExtensions.cs @@ -4,7 +4,7 @@ using System.IO; using System.Reflection; using System.Runtime.InteropServices; using System.Text; -using SabreTools.Numerics; +using SabreTools.Numerics.Extensions; namespace SabreTools.IO.Extensions { @@ -13,761 +13,6 @@ namespace SabreTools.IO.Extensions /// public static class StreamReaderExtensions { - #region Exact Read - - /// - /// Read a UInt8 from the stream - /// - public static byte ReadByteValue(this Stream stream) - { - byte[] buffer = ReadExactlyToBuffer(stream, 1); - return buffer[0]; - } - - /// - /// Read a UInt8 from the stream - /// - /// Reads in both-endian format - public static BothUInt8 ReadByteBothEndian(this Stream stream) - { - byte le = stream.ReadByteValue(); - byte be = stream.ReadByteValue(); - return new BothUInt8(le, be); - } - - /// - /// Read a UInt8[] from the stream - /// - public static byte[] ReadBytes(this Stream stream, int count) - => ReadExactlyToBuffer(stream, count); - - /// - /// Read an Int8 from the stream - /// - public static sbyte ReadSByte(this Stream stream) - { - byte[] buffer = ReadExactlyToBuffer(stream, 1); - return (sbyte)buffer[0]; - } - - /// - /// Read a UInt8 from the stream - /// - /// Reads in both-endian format - public static BothInt8 ReadSByteBothEndian(this Stream stream) - { - sbyte le = stream.ReadSByte(); - sbyte be = stream.ReadSByte(); - return new BothInt8(le, be); - } - - /// - /// Read a Char from the stream - /// - public static char ReadChar(this Stream stream) - { - byte[] buffer = ReadExactlyToBuffer(stream, 1); - return (char)buffer[0]; - } - - /// - /// Read an Int16 from the stream - /// - /// Reads in machine native format - public static short ReadInt16(this Stream stream) - { - if (BitConverter.IsLittleEndian) - return stream.ReadInt16LittleEndian(); - else - return stream.ReadInt16BigEndian(); - } - - /// - /// Read an Int16 from the stream - /// - /// Reads in big-endian format - public static short ReadInt16BigEndian(this Stream stream) - { - byte[] buffer = ReadExactlyToBuffer(stream, 2); - return buffer.ToInt16BigEndian(); - } - - /// - /// Read an Int16 from the stream - /// - /// Reads in little-endian format - public static short ReadInt16LittleEndian(this Stream stream) - { - byte[] buffer = ReadExactlyToBuffer(stream, 2); - return buffer.ToInt16LittleEndian(); - } - - /// - /// Read a Int16 from the stream - /// - /// Reads in both-endian format - public static BothInt16 ReadInt16BothEndian(this Stream stream) - { - short le = stream.ReadInt16LittleEndian(); - short be = stream.ReadInt16BigEndian(); - return new BothInt16(le, be); - } - - /// - /// Read a UInt16 from the stream - /// - /// Reads in machine native format - public static ushort ReadUInt16(this Stream stream) - { - if (BitConverter.IsLittleEndian) - return stream.ReadUInt16LittleEndian(); - else - return stream.ReadUInt16BigEndian(); - } - - /// - /// Read a UInt16 from the stream - /// - /// Reads in big-endian format - public static ushort ReadUInt16BigEndian(this Stream stream) - { - byte[] buffer = ReadExactlyToBuffer(stream, 2); - return buffer.ToUInt16BigEndian(); - } - - /// - /// Read a UInt16 from the stream - /// - /// Reads in little-endian format - public static ushort ReadUInt16LittleEndian(this Stream stream) - { - byte[] buffer = ReadExactlyToBuffer(stream, 2); - return buffer.ToUInt16LittleEndian(); - } - - /// - /// Read a UInt16 from the stream - /// - /// Reads in both-endian format - public static BothUInt16 ReadUInt16BothEndian(this Stream stream) - { - ushort le = stream.ReadUInt16LittleEndian(); - ushort be = stream.ReadUInt16BigEndian(); - return new BothUInt16(le, be); - } - - /// - /// Read a WORD (2-byte) from the stream - /// - /// Reads in machine native format - public static ushort ReadWORD(this Stream stream) - => stream.ReadUInt16(); - - /// - /// Read a WORD (2-byte) from the stream - /// - /// Reads in big-endian format - public static ushort ReadWORDBigEndian(this Stream stream) - => stream.ReadUInt16BigEndian(); - - /// - /// Read a WORD (2-byte) from the stream - /// - /// Reads in little-endian format - public static ushort ReadWORDLittleEndian(this Stream stream) - => stream.ReadUInt16LittleEndian(); - - /// - /// Read a WORD (2-byte) from the stream - /// - /// Reads in both-endian format - public static BothUInt16 ReadWORDBothEndian(this Stream stream) - => stream.ReadUInt16BothEndian(); - -#if NET5_0_OR_GREATER - /// - /// Read a Half from the stream - /// - /// Reads in machine native format - public static Half ReadHalf(this Stream stream) - { - if (BitConverter.IsLittleEndian) - return stream.ReadHalfLittleEndian(); - else - return stream.ReadHalfBigEndian(); - } - - /// - /// Read a Half from the stream - /// - /// Reads in big-endian format - public static Half ReadHalfBigEndian(this Stream stream) - { - byte[] buffer = ReadExactlyToBuffer(stream, 2); - return buffer.ToHalfBigEndian(); - } - - /// - /// Read a Half from the stream - /// - /// Reads in little-endian format - public static Half ReadHalfLittleEndian(this Stream stream) - { - byte[] buffer = ReadExactlyToBuffer(stream, 2); - return buffer.ToHalfLittleEndian(); - } -#endif - - /// - /// Read an Int24 from the stream - /// - /// Reads in machine native format - public static Int24 ReadInt24(this Stream stream) - { - if (BitConverter.IsLittleEndian) - return stream.ReadInt24LittleEndian(); - else - return stream.ReadInt24BigEndian(); - } - - /// - /// Read an Int24 from the stream - /// - /// Reads in big-endian format - public static Int24 ReadInt24BigEndian(this Stream stream) - { - byte[] buffer = ReadExactlyToBuffer(stream, 3); - return buffer.ToInt24BigEndian(); - } - - /// - /// Read an Int24 from the stream - /// - /// Reads in little-endian format - public static Int24 ReadInt24LittleEndian(this Stream stream) - { - byte[] buffer = ReadExactlyToBuffer(stream, 3); - return buffer.ToInt24LittleEndian(); - } - - /// - /// Read a UInt24 from the stream - /// - /// Reads in machine native format - public static UInt24 ReadUInt24(this Stream stream) - { - if (BitConverter.IsLittleEndian) - return stream.ReadUInt24LittleEndian(); - else - return stream.ReadUInt24BigEndian(); - } - - /// - /// Read a UInt24 from the stream - /// - /// Reads in big-endian format - public static UInt24 ReadUInt24BigEndian(this Stream stream) - { - byte[] buffer = ReadExactlyToBuffer(stream, 3); - return buffer.ToUInt24BigEndian(); - } - - /// - /// Read a UInt24 from the stream - /// - /// Reads in little-endian format - public static UInt24 ReadUInt24LittleEndian(this Stream stream) - { - byte[] buffer = ReadExactlyToBuffer(stream, 3); - return buffer.ToUInt24LittleEndian(); - } - - /// - /// Read an Int32 from the stream - /// - /// Reads in machine native format - public static int ReadInt32(this Stream stream) - { - if (BitConverter.IsLittleEndian) - return stream.ReadInt32LittleEndian(); - else - return stream.ReadInt32BigEndian(); - } - - /// - /// Read an Int32 from the stream - /// - /// Reads in big-endian format - public static int ReadInt32BigEndian(this Stream stream) - { - byte[] buffer = ReadExactlyToBuffer(stream, 4); - return buffer.ToInt32BigEndian(); - } - - /// - /// Read an Int32 from the stream - /// - /// Reads in little-endian format - public static int ReadInt32LittleEndian(this Stream stream) - { - byte[] buffer = ReadExactlyToBuffer(stream, 4); - return buffer.ToInt32LittleEndian(); - } - - /// - /// Read a Int32 from the stream - /// - /// Reads in both-endian format - public static BothInt32 ReadInt32BothEndian(this Stream stream) - { - int le = stream.ReadInt32LittleEndian(); - int be = stream.ReadInt32BigEndian(); - return new BothInt32(le, be); - } - - /// - /// Read a UInt32 from the stream - /// - /// Reads in machine native format - public static uint ReadUInt32(this Stream stream) - { - if (BitConverter.IsLittleEndian) - return stream.ReadUInt32LittleEndian(); - else - return stream.ReadUInt32BigEndian(); - } - - /// - /// Read a UInt32 from the stream - /// - /// Reads in big-endian format - public static uint ReadUInt32BigEndian(this Stream stream) - { - byte[] buffer = ReadExactlyToBuffer(stream, 4); - return buffer.ToUInt32BigEndian(); - } - - /// - /// Read a UInt32 from the stream - /// - /// Reads in little-endian format - public static uint ReadUInt32LittleEndian(this Stream stream) - { - byte[] buffer = ReadExactlyToBuffer(stream, 4); - return buffer.ToUInt32LittleEndian(); - } - - /// - /// Read a UInt32 from the stream - /// - /// Reads in both-endian format - public static BothUInt32 ReadUInt32BothEndian(this Stream stream) - { - uint le = stream.ReadUInt32LittleEndian(); - uint be = stream.ReadUInt32BigEndian(); - return new BothUInt32(le, be); - } - - /// - /// Read a DWORD (4-byte) from the stream - /// - /// Reads in machine native format - public static uint ReadDWORD(this Stream stream) - => stream.ReadUInt32(); - - /// - /// Read a DWORD (4-byte) from the stream - /// - /// Reads in big-endian format - public static uint ReadDWORDBigEndian(this Stream stream) - => stream.ReadUInt32BigEndian(); - - /// - /// Read a DWORD (4-byte) from the stream - /// - /// Reads in little-endian format - public static uint ReadDWORDLittleEndian(this Stream stream) - => stream.ReadUInt32LittleEndian(); - - /// - /// Read a DWORD (4-byte) from the stream - /// - /// Reads in both-endian format - public static BothUInt32 ReadDWORDBothEndian(this Stream stream) - => stream.ReadUInt32BothEndian(); - - /// - /// Read a Single from the stream - /// - /// Reads in machine native format - public static float ReadSingle(this Stream stream) - { - if (BitConverter.IsLittleEndian) - return stream.ReadSingleLittleEndian(); - else - return stream.ReadSingleBigEndian(); - } - - /// - /// Read a Single from the stream - /// - /// Reads in big-endian format - public static float ReadSingleBigEndian(this Stream stream) - { - byte[] buffer = ReadExactlyToBuffer(stream, 4); - return buffer.ToSingleBigEndian(); - } - - /// - /// Read a Single from the stream - /// - /// Reads in little-endian format - public static float ReadSingleLittleEndian(this Stream stream) - { - byte[] buffer = ReadExactlyToBuffer(stream, 4); - return buffer.ToSingleLittleEndian(); - } - - /// - /// Read an Int48 from the stream - /// - /// Reads in machine native format - public static Int48 ReadInt48(this Stream stream) - { - if (BitConverter.IsLittleEndian) - return stream.ReadInt48LittleEndian(); - else - return stream.ReadInt48BigEndian(); - } - - /// - /// Read an Int48 from the stream - /// - /// Reads in big-endian format - public static Int48 ReadInt48BigEndian(this Stream stream) - { - byte[] buffer = ReadExactlyToBuffer(stream, 6); - return buffer.ToInt48BigEndian(); - } - - /// - /// Read an Int48 from the stream - /// - /// Reads in little-endian format - public static Int48 ReadInt48LittleEndian(this Stream stream) - { - byte[] buffer = ReadExactlyToBuffer(stream, 6); - return buffer.ToInt48LittleEndian(); - } - - /// - /// Read a UInt48 from the stream - /// - /// Reads in machine native format - public static UInt48 ReadUInt48(this Stream stream) - { - if (BitConverter.IsLittleEndian) - return stream.ReadUInt48LittleEndian(); - else - return stream.ReadUInt48BigEndian(); - } - - /// - /// Read a UInt48 from the stream - /// - /// Reads in big-endian format - public static UInt48 ReadUInt48BigEndian(this Stream stream) - { - byte[] buffer = ReadExactlyToBuffer(stream, 6); - return buffer.ToUInt48BigEndian(); - } - - /// - /// Read an UInt48 from the stream - /// - /// Reads in little-endian format - public static UInt48 ReadUInt48LittleEndian(this Stream stream) - { - byte[] buffer = ReadExactlyToBuffer(stream, 6); - return buffer.ToUInt48LittleEndian(); - } - - /// - /// Read an Int64 from the stream - /// - /// Reads in machine native format - public static long ReadInt64(this Stream stream) - { - if (BitConverter.IsLittleEndian) - return stream.ReadInt64LittleEndian(); - else - return stream.ReadInt64BigEndian(); - } - - /// - /// Read an Int64 from the stream - /// - /// Reads in big-endian format - public static long ReadInt64BigEndian(this Stream stream) - { - byte[] buffer = ReadExactlyToBuffer(stream, 8); - return buffer.ToInt64BigEndian(); - } - - /// - /// Read an Int64 from the stream - /// - /// Reads in little-endian format - public static long ReadInt64LittleEndian(this Stream stream) - { - byte[] buffer = ReadExactlyToBuffer(stream, 8); - return buffer.ToInt64LittleEndian(); - } - - /// - /// Read a Int64 from the stream - /// - /// Reads in both-endian format - public static BothInt64 ReadInt64BothEndian(this Stream stream) - { - long le = stream.ReadInt64LittleEndian(); - long be = stream.ReadInt64BigEndian(); - return new BothInt64(le, be); - } - - /// - /// Read a UInt64 from the stream - /// - /// Reads in machine native format - public static ulong ReadUInt64(this Stream stream) - { - if (BitConverter.IsLittleEndian) - return stream.ReadUInt64LittleEndian(); - else - return stream.ReadUInt64BigEndian(); - } - - /// - /// Read a UInt64 from the stream - /// - /// Reads in big-endian format - public static ulong ReadUInt64BigEndian(this Stream stream) - { - byte[] buffer = ReadExactlyToBuffer(stream, 8); - return buffer.ToUInt64BigEndian(); - } - - /// - /// Read a UInt64 from the stream - /// - /// Reads in little-endian format - public static ulong ReadUInt64LittleEndian(this Stream stream) - { - byte[] buffer = ReadExactlyToBuffer(stream, 8); - return buffer.ToUInt64LittleEndian(); - } - - /// - /// Read a UInt64 from the stream - /// - /// Reads in both-endian format - public static BothUInt64 ReadUInt64BothEndian(this Stream stream) - { - ulong le = stream.ReadUInt64LittleEndian(); - ulong be = stream.ReadUInt64BigEndian(); - return new BothUInt64(le, be); - } - - /// - /// Read a QWORD (8-byte) from the stream - /// - /// Reads in machine native format - public static ulong ReadQWORD(this Stream stream) - => stream.ReadUInt64(); - - /// - /// Read a QWORD (8-byte) from the stream - /// - /// Reads in big-endian format - public static ulong ReadQWORDBigEndian(this Stream stream) - => stream.ReadUInt64BigEndian(); - - /// - /// Read a QWORD (8-byte) from the stream - /// - /// Reads in little-endian format - public static ulong ReadQWORDLittleEndian(this Stream stream) - => stream.ReadUInt64LittleEndian(); - - /// - /// Read a QWORD (8-byte) from the stream - /// - /// Reads in both-endian format - public static BothUInt64 ReadQWORDBothEndian(this Stream stream) - => stream.ReadUInt64BothEndian(); - - /// - /// Read a Double from the stream - /// - /// Reads in machine native format - public static double ReadDouble(this Stream stream) - { - if (BitConverter.IsLittleEndian) - return stream.ReadDoubleLittleEndian(); - else - return stream.ReadDoubleBigEndian(); - } - - /// - /// Read a Double from the stream - /// - /// Reads in big-endian format - public static double ReadDoubleBigEndian(this Stream stream) - { - byte[] buffer = ReadExactlyToBuffer(stream, 8); - return buffer.ToDoubleBigEndian(); - } - - /// - /// Read a Double from the stream - /// - /// Reads in little-endian format - public static double ReadDoubleLittleEndian(this Stream stream) - { - byte[] buffer = ReadExactlyToBuffer(stream, 8); - return buffer.ToDoubleLittleEndian(); - } - - /// - /// Read a Guid from the stream - /// - /// Reads in machine native format - public static Guid ReadGuid(this Stream stream) - { - if (BitConverter.IsLittleEndian) - return stream.ReadGuidLittleEndian(); - else - return stream.ReadGuidBigEndian(); - } - - /// - /// Read a Guid from the stream - /// - /// Reads in big-endian format - public static Guid ReadGuidBigEndian(this Stream stream) - { - byte[] buffer = ReadExactlyToBuffer(stream, 16); - return buffer.ToGuidBigEndian(); - } - - /// - /// Read a Guid from the stream - /// - /// Reads in little-endian format - public static Guid ReadGuidLittleEndian(this Stream stream) - { - byte[] buffer = ReadExactlyToBuffer(stream, 16); - return buffer.ToGuidLittleEndian(); - } - -#if NET7_0_OR_GREATER - /// - /// Read an Int128 from the stream - /// - /// Reads in machine native format - public static Int128 ReadInt128(this Stream stream) - { - if (BitConverter.IsLittleEndian) - return stream.ReadInt128LittleEndian(); - else - return stream.ReadInt128BigEndian(); - } - - /// - /// Read an Int128 from the stream - /// - /// Reads in big-endian format - public static Int128 ReadInt128BigEndian(this Stream stream) - { - byte[] buffer = ReadExactlyToBuffer(stream, 16); - return buffer.ToInt128BigEndian(); - } - - /// - /// Read an Int128 from the stream - /// - /// Reads in little-endian format - public static Int128 ReadInt128LittleEndian(this Stream stream) - { - byte[] buffer = ReadExactlyToBuffer(stream, 16); - return buffer.ToInt128LittleEndian(); - } - - /// - /// Read a UInt128 from the stream - /// - /// Reads in machine native format - public static UInt128 ReadUInt128(this Stream stream) - { - if (BitConverter.IsLittleEndian) - return stream.ReadUInt128LittleEndian(); - else - return stream.ReadUInt128BigEndian(); - } - - /// - /// Read a UInt128 from the stream - /// - /// Reads in big-endian format - public static UInt128 ReadUInt128BigEndian(this Stream stream) - { - byte[] buffer = ReadExactlyToBuffer(stream, 16); - return buffer.ToUInt128BigEndian(); - } - - /// - /// Read a UInt128 from the stream - /// - /// Reads in little-endian format - public static UInt128 ReadUInt128LittleEndian(this Stream stream) - { - byte[] buffer = ReadExactlyToBuffer(stream, 16); - return buffer.ToUInt128LittleEndian(); - } -#endif - - /// - /// Read a Decimal from the stream - /// - /// Reads in machine native format - public static decimal ReadDecimal(this Stream stream) - { - if (BitConverter.IsLittleEndian) - return stream.ReadDecimalLittleEndian(); - else - return stream.ReadDecimalBigEndian(); - } - - /// - /// Read a Decimal from the stream - /// - /// Reads in big-endian format - public static decimal ReadDecimalBigEndian(this Stream stream) - { - byte[] buffer = ReadExactlyToBuffer(stream, 16); - return buffer.ToDecimalBigEndian(); - } - - /// - /// Read a Decimal from the stream - /// - /// Reads in little-endian format - public static decimal ReadDecimalLittleEndian(this Stream stream) - { - byte[] buffer = ReadExactlyToBuffer(stream, 16); - return buffer.ToDecimalLittleEndian(); - } - /// /// Read a null-terminated string from the stream /// @@ -1242,1915 +487,5 @@ namespace SabreTools.IO.Extensions return buffer; } - - #endregion - - #region Peek Read - - /// - /// Peek a UInt8 from the stream - /// - /// Only works properly on seekable streams - public static byte PeekByte(this Stream stream) - { - byte value = stream.ReadByteValue(); - stream.SeekIfPossible(-1, SeekOrigin.Current); - return value; - } - - /// - /// Peek a UInt8 from the stream - /// - /// Only works properly on seekable streams - public static byte PeekByteValue(this Stream stream) - => stream.PeekByte(); - - /// - /// Peek a UInt8 from the stream - /// - /// Reads in both-endian format - /// Only works properly on seekable streams - public static BothUInt8 PeekByteBothEndian(this Stream stream) - { - BothUInt8 value = stream.ReadByteBothEndian(); - stream.SeekIfPossible(-2, SeekOrigin.Current); - return value; - } - - /// - /// Peek a UInt8[] from the stream - /// - /// Only works properly on seekable streams - public static byte[] PeekBytes(this Stream stream, int count) - { - byte[] value = stream.ReadBytes(count); - stream.SeekIfPossible(-count, SeekOrigin.Current); - return value; - } - - /// - /// Peek an Int8 from the stream - /// - /// Only works properly on seekable streams - public static sbyte PeekSByte(this Stream stream) - { - sbyte value = stream.ReadSByte(); - stream.SeekIfPossible(-1, SeekOrigin.Current); - return value; - } - - /// - /// Peek a Int8 from the stream - /// - /// Reads in both-endian format - /// Only works properly on seekable streams - public static BothInt8 PeekSByteBothEndian(this Stream stream) - { - BothInt8 value = stream.ReadSByteBothEndian(); - stream.SeekIfPossible(-2, SeekOrigin.Current); - return value; - } - - /// - /// Peek a Char from the stream - /// - /// Only works properly on seekable streams - public static char PeekChar(this Stream stream) - { - char value = stream.ReadChar(); - stream.SeekIfPossible(-1, SeekOrigin.Current); - return value; - } - - /// - /// Peek an Int16 from the stream - /// - /// Reads in machine native format - /// Only works properly on seekable streams - public static short PeekInt16(this Stream stream) - { - if (BitConverter.IsLittleEndian) - return stream.PeekInt16LittleEndian(); - else - return stream.PeekInt16BigEndian(); - } - - /// - /// Peek an Int16 from the stream - /// - /// Reads in big-endian format - /// Only works properly on seekable streams - public static short PeekInt16BigEndian(this Stream stream) - { - short value = stream.ReadInt16BigEndian(); - stream.SeekIfPossible(-2, SeekOrigin.Current); - return value; - } - - /// - /// Peek an Int16 from the stream - /// - /// Reads in little-endian format - /// Only works properly on seekable streams - public static short PeekInt16LittleEndian(this Stream stream) - { - short value = stream.ReadInt16LittleEndian(); - stream.SeekIfPossible(-2, SeekOrigin.Current); - return value; - } - - /// - /// Peek a Int16 from the stream - /// - /// Reads in both-endian format - /// Only works properly on seekable streams - public static BothInt16 PeekInt16BothEndian(this Stream stream) - { - BothInt16 value = stream.ReadInt16BothEndian(); - stream.SeekIfPossible(-4, SeekOrigin.Current); - return value; - } - - /// - /// Peek a UInt16 from the stream - /// - /// Reads in machine native format - /// Only works properly on seekable streams - public static ushort PeekUInt16(this Stream stream) - { - if (BitConverter.IsLittleEndian) - return stream.PeekUInt16LittleEndian(); - else - return stream.PeekUInt16BigEndian(); - } - - /// - /// Peek a UInt16 from the stream - /// - /// Reads in big-endian format - /// Only works properly on seekable streams - public static ushort PeekUInt16BigEndian(this Stream stream) - { - ushort value = stream.ReadUInt16BigEndian(); - stream.SeekIfPossible(-2, SeekOrigin.Current); - return value; - } - - /// - /// Peek a UInt16 from the stream - /// - /// Reads in little-endian format - /// Only works properly on seekable streams - public static ushort PeekUInt16LittleEndian(this Stream stream) - { - ushort value = stream.ReadUInt16LittleEndian(); - stream.SeekIfPossible(-2, SeekOrigin.Current); - return value; - } - - /// - /// Peek a UInt16 from the stream - /// - /// Reads in both-endian format - /// Only works properly on seekable streams - public static BothUInt16 PeekUInt16BothEndian(this Stream stream) - { - BothUInt16 value = stream.ReadUInt16BothEndian(); - stream.SeekIfPossible(-4, SeekOrigin.Current); - return value; - } - - /// - /// Peek a WORD (2-byte) from the stream - /// - /// Reads in machine native format - /// Only works properly on seekable streams - public static ushort PeekWORD(this Stream stream) - => stream.PeekUInt16(); - - /// - /// Peek a WORD (2-byte) from the stream - /// - /// Reads in big-endian format - /// Only works properly on seekable streams - public static ushort PeekWORDBigEndian(this Stream stream) - => stream.PeekUInt16BigEndian(); - - /// - /// Peek a WORD (2-byte) from the stream - /// - /// Reads in little-endian format - /// Only works properly on seekable streams - public static ushort PeekWORDLittleEndian(this Stream stream) - => stream.PeekUInt16LittleEndian(); - - /// - /// Peek a WORD (2-byte) from the stream - /// - /// Reads in little-endian format - /// Only works properly on seekable streams - public static BothUInt16 PeekWORDBothEndian(this Stream stream) - => stream.PeekUInt16BothEndian(); - -#if NET5_0_OR_GREATER - /// - /// Peek a Half from the stream - /// - /// Reads in machine native format - /// Only works properly on seekable streams - public static Half PeekHalf(this Stream stream) - { - if (BitConverter.IsLittleEndian) - return stream.PeekHalfLittleEndian(); - else - return stream.PeekHalfBigEndian(); - } - - /// - /// Peek a Half from the stream - /// - /// Reads in big-endian format - /// Only works properly on seekable streams - public static Half PeekHalfBigEndian(this Stream stream) - { - Half value = stream.ReadHalfBigEndian(); - stream.SeekIfPossible(-2, SeekOrigin.Current); - return value; - } - - /// - /// Peek a Half from the stream - /// - /// Reads in little-endian format - /// Only works properly on seekable streams - public static Half PeekHalfLittleEndian(this Stream stream) - { - Half value = stream.ReadHalfLittleEndian(); - stream.SeekIfPossible(-2, SeekOrigin.Current); - return value; - } -#endif - - /// - /// Peek an Int24 from the stream - /// - /// Reads in machine native format - /// Only works properly on seekable streams - public static Int24 PeekInt24(this Stream stream) - { - if (BitConverter.IsLittleEndian) - return stream.PeekInt24LittleEndian(); - else - return stream.PeekInt24BigEndian(); - } - - /// - /// Peek an Int24 from the stream - /// - /// Reads in big-endian format - /// Only works properly on seekable streams - public static Int24 PeekInt24BigEndian(this Stream stream) - { - Int24 value = stream.ReadInt24BigEndian(); - stream.SeekIfPossible(-3, SeekOrigin.Current); - return value; - } - - /// - /// Peek an Int24 from the stream - /// - /// Reads in little-endian format - /// Only works properly on seekable streams - public static Int24 PeekInt24LittleEndian(this Stream stream) - { - Int24 value = stream.ReadInt24LittleEndian(); - stream.SeekIfPossible(-3, SeekOrigin.Current); - return value; - } - - /// - /// Peek a UInt24 from the stream - /// - /// Reads in machine native format - /// Only works properly on seekable streams - public static UInt24 PeekUInt24(this Stream stream) - { - if (BitConverter.IsLittleEndian) - return stream.PeekUInt24LittleEndian(); - else - return stream.PeekUInt24BigEndian(); - } - - /// - /// Peek a UInt24 from the stream - /// - /// Reads in big-endian format - /// Only works properly on seekable streams - public static UInt24 PeekUInt24BigEndian(this Stream stream) - { - UInt24 value = stream.ReadUInt24BigEndian(); - stream.SeekIfPossible(-3, SeekOrigin.Current); - return value; - } - - /// - /// Peek a UInt24 from the stream - /// - /// Reads in little-endian format - /// Only works properly on seekable streams - public static UInt24 PeekUInt24LittleEndian(this Stream stream) - { - UInt24 value = stream.ReadUInt24LittleEndian(); - stream.SeekIfPossible(-3, SeekOrigin.Current); - return value; - } - - /// - /// Peek an Int32 from the stream - /// - /// Reads in machine native format - /// Only works properly on seekable streams - public static int PeekInt32(this Stream stream) - { - if (BitConverter.IsLittleEndian) - return stream.PeekInt32LittleEndian(); - else - return stream.PeekInt32BigEndian(); - } - - /// - /// Peek an Int32 from the stream - /// - /// Reads in big-endian format - /// Only works properly on seekable streams - public static int PeekInt32BigEndian(this Stream stream) - { - int value = stream.ReadInt32BigEndian(); - stream.SeekIfPossible(-4, SeekOrigin.Current); - return value; - } - - /// - /// Peek an Int32 from the stream - /// - /// Reads in little-endian format - /// Only works properly on seekable streams - public static int PeekInt32LittleEndian(this Stream stream) - { - int value = stream.ReadInt32LittleEndian(); - stream.SeekIfPossible(-4, SeekOrigin.Current); - return value; - } - - /// - /// Peek a Int32 from the stream - /// - /// Reads in both-endian format - /// Only works properly on seekable streams - public static BothInt32 PeekInt32BothEndian(this Stream stream) - { - BothInt32 value = stream.ReadInt32BothEndian(); - stream.SeekIfPossible(-8, SeekOrigin.Current); - return value; - } - - /// - /// Peek a UInt32 from the stream - /// - /// Reads in machine native format - /// Only works properly on seekable streams - public static uint PeekUInt32(this Stream stream) - { - if (BitConverter.IsLittleEndian) - return stream.PeekUInt32LittleEndian(); - else - return stream.PeekUInt32BigEndian(); - } - - /// - /// Peek a UInt32 from the stream - /// - /// Reads in big-endian format - /// Only works properly on seekable streams - public static uint PeekUInt32BigEndian(this Stream stream) - { - uint value = stream.ReadUInt32BigEndian(); - stream.SeekIfPossible(-4, SeekOrigin.Current); - return value; - } - - /// - /// Peek a UInt32 from the stream - /// - /// Reads in little-endian format - /// Only works properly on seekable streams - public static uint PeekUInt32LittleEndian(this Stream stream) - { - uint value = stream.ReadUInt32LittleEndian(); - stream.SeekIfPossible(-4, SeekOrigin.Current); - return value; - } - - /// - /// Peek a UInt32 from the stream - /// - /// Reads in both-endian format - /// Only works properly on seekable streams - public static BothUInt32 PeekUInt32BothEndian(this Stream stream) - { - BothUInt32 value = stream.ReadUInt32BothEndian(); - stream.SeekIfPossible(-8, SeekOrigin.Current); - return value; - } - - /// - /// Peek a DWORD (4-byte) from the stream - /// - /// Reads in machine native format - /// Only works properly on seekable streams - public static uint PeekDWORD(this Stream stream) - => stream.PeekUInt32(); - - /// - /// Peek a DWORD (4-byte) from the stream - /// - /// Reads in big-endian format - /// Only works properly on seekable streams - public static uint PeekDWORDBigEndian(this Stream stream) - => stream.PeekUInt32BigEndian(); - - /// - /// Peek a DWORD (4-byte) from the stream - /// - /// Reads in little-endian format - /// Only works properly on seekable streams - public static uint PeekDWORDLittleEndian(this Stream stream) - => stream.PeekUInt32LittleEndian(); - - /// - /// Peek a DWORD (4-byte) from the stream - /// - /// Reads in little-endian format - /// Only works properly on seekable streams - public static BothUInt32 PeekDWORDBothEndian(this Stream stream) - => stream.PeekUInt32BothEndian(); - - /// - /// Peek a Single from the stream - /// - /// Reads in machine native format - /// Only works properly on seekable streams - public static float PeekSingle(this Stream stream) - { - if (BitConverter.IsLittleEndian) - return stream.PeekSingleLittleEndian(); - else - return stream.PeekSingleBigEndian(); - } - - /// - /// Peek a Single from the stream - /// - /// Reads in big-endian format - /// Only works properly on seekable streams - public static float PeekSingleBigEndian(this Stream stream) - { - float value = stream.ReadSingleBigEndian(); - stream.SeekIfPossible(-4, SeekOrigin.Current); - return value; - } - - /// - /// Peek a Single from the stream - /// - /// Reads in little-endian format - /// Only works properly on seekable streams - public static float PeekSingleLittleEndian(this Stream stream) - { - float value = stream.ReadSingleLittleEndian(); - stream.SeekIfPossible(-4, SeekOrigin.Current); - return value; - } - - /// - /// Peek an Int48 from the stream - /// - /// Reads in machine native format - /// Only works properly on seekable streams - public static Int48 PeekInt48(this Stream stream) - { - if (BitConverter.IsLittleEndian) - return stream.PeekInt48LittleEndian(); - else - return stream.PeekInt48BigEndian(); - } - - /// - /// Peek an Int48 from the stream - /// - /// Reads in big-endian format - /// Only works properly on seekable streams - public static Int48 PeekInt48BigEndian(this Stream stream) - { - Int48 value = stream.ReadInt48BigEndian(); - stream.SeekIfPossible(-6, SeekOrigin.Current); - return value; - } - - /// - /// Peek an Int48 from the stream - /// - /// Reads in little-endian format - /// Only works properly on seekable streams - public static Int48 PeekInt48LittleEndian(this Stream stream) - { - Int48 value = stream.ReadInt48LittleEndian(); - stream.SeekIfPossible(-6, SeekOrigin.Current); - return value; - } - - /// - /// Peek a UInt48 from the stream - /// - /// Reads in machine native format - /// Only works properly on seekable streams - public static UInt48 PeekUInt48(this Stream stream) - { - if (BitConverter.IsLittleEndian) - return stream.PeekUInt48LittleEndian(); - else - return stream.PeekUInt48BigEndian(); - } - - /// - /// Peek an UInt48 from the stream - /// - /// Reads in big-endian format - /// Only works properly on seekable streams - public static UInt48 PeekUInt48BigEndian(this Stream stream) - { - UInt48 value = stream.ReadUInt48BigEndian(); - stream.SeekIfPossible(-6, SeekOrigin.Current); - return value; - } - - /// - /// Peek an UInt48 from the stream - /// - /// Reads in little-endian format - /// Only works properly on seekable streams - public static UInt48 PeekUInt48LittleEndian(this Stream stream) - { - UInt48 value = stream.ReadUInt48LittleEndian(); - stream.SeekIfPossible(-6, SeekOrigin.Current); - return value; - } - - /// - /// Peek an Int64 from the stream - /// - /// Reads in machine native format - /// Only works properly on seekable streams - public static long PeekInt64(this Stream stream) - { - if (BitConverter.IsLittleEndian) - return stream.PeekInt64LittleEndian(); - else - return stream.PeekInt64BigEndian(); - } - - /// - /// Peek an Int64 from the stream - /// - /// Reads in big-endian format - /// Only works properly on seekable streams - public static long PeekInt64BigEndian(this Stream stream) - { - long value = stream.ReadInt64BigEndian(); - stream.SeekIfPossible(-8, SeekOrigin.Current); - return value; - } - - /// - /// Peek an Int64 from the stream - /// - /// Reads in big-endian format - /// Only works properly on seekable streams - public static long PeekInt64LittleEndian(this Stream stream) - { - long value = stream.ReadInt64LittleEndian(); - stream.SeekIfPossible(-8, SeekOrigin.Current); - return value; - } - - /// - /// Peek a Int64 from the stream - /// - /// Reads in both-endian format - /// Only works properly on seekable streams - public static BothInt64 PeekInt64BothEndian(this Stream stream) - { - BothInt64 value = stream.ReadInt64BothEndian(); - stream.SeekIfPossible(-16, SeekOrigin.Current); - return value; - } - - /// - /// Peek a UInt64 from the stream - /// - /// Reads in machine native format - /// Only works properly on seekable streams - public static ulong PeekUInt64(this Stream stream) - { - if (BitConverter.IsLittleEndian) - return stream.PeekUInt64LittleEndian(); - else - return stream.PeekUInt64BigEndian(); - } - - /// - /// Peek a UInt64 from the stream - /// - /// Reads in big-endian format - /// Only works properly on seekable streams - public static ulong PeekUInt64BigEndian(this Stream stream) - { - ulong value = stream.ReadUInt64BigEndian(); - stream.SeekIfPossible(-8, SeekOrigin.Current); - return value; - } - - /// - /// Peek a UInt64 from the stream - /// - /// Reads in little-endian format - /// Only works properly on seekable streams - public static ulong PeekUInt64LittleEndian(this Stream stream) - { - ulong value = stream.ReadUInt64LittleEndian(); - stream.SeekIfPossible(-8, SeekOrigin.Current); - return value; - } - - /// - /// Peek a UInt64 from the stream - /// - /// Reads in both-endian format - /// Only works properly on seekable streams - public static BothUInt64 PeekUInt64BothEndian(this Stream stream) - { - BothUInt64 value = stream.ReadUInt64BothEndian(); - stream.SeekIfPossible(-16, SeekOrigin.Current); - return value; - } - - /// - /// Peek a QWORD (8-byte) from the stream - /// - /// Reads in machine native format - /// Only works properly on seekable streams - public static ulong PeekQWORD(this Stream stream) - => stream.PeekUInt64(); - - /// - /// Peek a QWORD (8-byte) from the stream - /// - /// Reads in big-endian format - /// Only works properly on seekable streams - public static ulong PeekQWORDBigEndian(this Stream stream) - => stream.PeekUInt64BigEndian(); - - /// - /// Peek a QWORD (8-byte) from the stream - /// - /// Reads in little-endian format - /// Only works properly on seekable streams - public static ulong PeekQWORDLittleEndian(this Stream stream) - => stream.PeekUInt64LittleEndian(); - - /// - /// Peek a QWORD (8-byte) from the stream - /// - /// Reads in little-endian format - /// Only works properly on seekable streams - public static BothUInt64 PeekQWORDBothEndian(this Stream stream) - => stream.PeekUInt64BothEndian(); - - /// - /// Peek a Double from the stream - /// - /// Reads in machine native format - /// Only works properly on seekable streams - public static double PeekDouble(this Stream stream) - { - if (BitConverter.IsLittleEndian) - return stream.PeekDoubleLittleEndian(); - else - return stream.PeekDoubleBigEndian(); - } - - /// - /// Peek a Double from the stream - /// - /// Reads in big-endian format - /// Only works properly on seekable streams - public static double PeekDoubleBigEndian(this Stream stream) - { - double value = stream.ReadDoubleBigEndian(); - stream.SeekIfPossible(-8, SeekOrigin.Current); - return value; - } - - /// - /// Peek a Double from the stream - /// - /// Reads in little-endian format - /// Only works properly on seekable streams - public static double PeekDoubleLittleEndian(this Stream stream) - { - double value = stream.ReadDoubleLittleEndian(); - stream.SeekIfPossible(-8, SeekOrigin.Current); - return value; - } - - /// - /// Peek a Guid from the stream - /// - /// Reads in machine native format - /// Only works properly on seekable streams - public static Guid PeekGuid(this Stream stream) - { - if (BitConverter.IsLittleEndian) - return stream.PeekGuidLittleEndian(); - else - return stream.PeekGuidBigEndian(); - } - - /// - /// Peek a Guid from the stream - /// - /// Reads in big-endian format - /// Only works properly on seekable streams - public static Guid PeekGuidBigEndian(this Stream stream) - { - Guid value = stream.ReadGuidBigEndian(); - stream.SeekIfPossible(-16, SeekOrigin.Current); - return value; - } - - /// - /// Peek a Guid from the stream - /// - /// Reads in little-endian format - /// Only works properly on seekable streams - public static Guid PeekGuidLittleEndian(this Stream stream) - { - Guid value = stream.ReadGuidLittleEndian(); - stream.SeekIfPossible(-16, SeekOrigin.Current); - return value; - } - -#if NET7_0_OR_GREATER - /// - /// Peek an Int128 from the stream - /// - /// Reads in machine native format - /// Only works properly on seekable streams - public static Int128 PeekInt128(this Stream stream) - { - if (BitConverter.IsLittleEndian) - return stream.PeekInt128LittleEndian(); - else - return stream.PeekInt128BigEndian(); - } - - /// - /// Peek an Int128 from the stream - /// - /// Reads in big-endian format - /// Only works properly on seekable streams - public static Int128 PeekInt128BigEndian(this Stream stream) - { - Int128 value = stream.ReadInt128BigEndian(); - stream.SeekIfPossible(-16, SeekOrigin.Current); - return value; - } - - /// - /// Peek an Int128 from the stream - /// - /// Reads in little-endian format - /// Only works properly on seekable streams - public static Int128 PeekInt128LittleEndian(this Stream stream) - { - Int128 value = stream.ReadInt128LittleEndian(); - stream.SeekIfPossible(-16, SeekOrigin.Current); - return value; - } - - /// - /// Peek a UInt128 from the stream - /// - /// Reads in machine native format - /// Only works properly on seekable streams - public static UInt128 PeekUInt128(this Stream stream) - { - if (BitConverter.IsLittleEndian) - return stream.PeekUInt128LittleEndian(); - else - return stream.PeekUInt128BigEndian(); - } - - /// - /// Peek a UInt128 from the stream - /// - /// Reads in big-endian format - /// Only works properly on seekable streams - public static UInt128 PeekUInt128BigEndian(this Stream stream) - { - UInt128 value = stream.ReadUInt128BigEndian(); - stream.SeekIfPossible(-16, SeekOrigin.Current); - return value; - } - - /// - /// Peek a UInt128 from the stream - /// - /// Reads in little-endian format - /// Only works properly on seekable streams - public static UInt128 PeekUInt128LittleEndian(this Stream stream) - { - UInt128 value = stream.ReadUInt128LittleEndian(); - stream.SeekIfPossible(-16, SeekOrigin.Current); - return value; - } -#endif - - /// - /// Peek a Decimal from the stream - /// - /// Reads in machine native format - /// Only works properly on seekable streams - public static decimal PeekDecimal(this Stream stream) - { - if (BitConverter.IsLittleEndian) - return stream.PeekDecimalLittleEndian(); - else - return stream.PeekDecimalBigEndian(); - } - - /// - /// Peek a Decimal from the stream - /// - /// Reads in big-endian format - /// Only works properly on seekable streams - public static decimal PeekDecimalBigEndian(this Stream stream) - { - decimal value = stream.ReadDecimalBigEndian(); - stream.SeekIfPossible(-16, SeekOrigin.Current); - return value; - } - - /// - /// Peek a Decimal from the stream - /// - /// Reads in little-endian format - /// Only works properly on seekable streams - public static decimal PeekDecimalLittleEndian(this Stream stream) - { - decimal value = stream.ReadDecimalLittleEndian(); - stream.SeekIfPossible(-16, SeekOrigin.Current); - return value; - } - - #endregion - - #region Try Read - - /// - /// Read a UInt8 from the stream - /// - public static bool TryReadByteValue(this Stream stream, out byte value) - { - if (stream.Position > stream.Length - 1) - { - value = default; - return false; - } - - value = stream.ReadByteValue(); - return true; - } - - /// - /// Read a UInt8 from the stream - /// - /// Reads in both-endian format - public static bool TryReadByteBothEndian(this Stream stream, out BothUInt8 value) - { - if (stream.Position > stream.Length - 2) - { - value = default(byte); - return false; - } - - value = stream.ReadByteBothEndian(); - return true; - } - - /// - /// Read a UInt8[] from the stream - /// - public static bool TryReadBytes(this Stream stream, int count, out byte[] value) - { - if (stream.Position > stream.Length - count) - { - value = []; - return false; - } - - value = stream.ReadBytes(count); - return true; - } - - /// - /// Read an Int8 from the stream - /// - public static bool TryReadSByte(this Stream stream, out sbyte value) - { - if (stream.Position > stream.Length - 1) - { - value = default; - return false; - } - - value = stream.ReadSByte(); - return true; - } - - /// - /// Read a Int8 from the stream - /// - /// Reads in both-endian format - public static bool TryReadSByteBothEndian(this Stream stream, out BothInt8 value) - { - if (stream.Position > stream.Length - 2) - { - value = default(sbyte); - return false; - } - - value = stream.ReadSByteBothEndian(); - return true; - } - - /// - /// Read a Char from the stream - /// - public static bool TryReadChar(this Stream stream, out char value) - { - if (stream.Position > stream.Length - 1) - { - value = default; - return false; - } - - value = stream.ReadChar(); - return true; - } - - /// - /// Read an Int16 from the stream - /// - /// Reads in machine native format - public static bool TryReadInt16(this Stream stream, out short value) - { - if (BitConverter.IsLittleEndian) - return stream.TryReadInt16LittleEndian(out value); - else - return stream.TryReadInt16BigEndian(out value); - } - - /// - /// Read an Int16 from the stream - /// - /// Reads in big-endian format - public static bool TryReadInt16BigEndian(this Stream stream, out short value) - { - if (stream.Position > stream.Length - 2) - { - value = default; - return false; - } - - value = stream.ReadInt16BigEndian(); - return true; - } - - /// - /// Read an Int16 from the stream - /// - /// Reads in little-endian format - public static bool TryReadInt16LittleEndian(this Stream stream, out short value) - { - if (stream.Position > stream.Length - 2) - { - value = default; - return false; - } - - value = stream.ReadInt16LittleEndian(); - return true; - } - - /// - /// Read a Int16 from the stream - /// - /// Reads in both-endian format - public static bool TryReadInt16BothEndian(this Stream stream, out BothInt16 value) - { - if (stream.Position > stream.Length - 4) - { - value = default(short); - return false; - } - - value = stream.ReadInt16BothEndian(); - return true; - } - - /// - /// Read a UInt16 from the stream - /// - /// Reads in machine native format - public static bool TryReadUInt16(this Stream stream, out ushort value) - { - if (BitConverter.IsLittleEndian) - return stream.TryReadUInt16LittleEndian(out value); - else - return stream.TryReadUInt16BigEndian(out value); - } - - /// - /// Read a UInt16 from the stream - /// - /// Reads in big-endian format - public static bool TryReadUInt16BigEndian(this Stream stream, out ushort value) - { - if (stream.Position > stream.Length - 2) - { - value = default; - return false; - } - - value = stream.ReadUInt16BigEndian(); - return true; - } - - /// - /// Read a UInt16 from the stream - /// - /// Reads in little-endian format - public static bool TryReadUInt16LittleEndian(this Stream stream, out ushort value) - { - if (stream.Position > stream.Length - 2) - { - value = default; - return false; - } - - value = stream.ReadUInt16LittleEndian(); - return true; - } - - /// - /// Read a UInt16 from the stream - /// - /// Reads in both-endian format - public static bool TryReadUInt16BothEndian(this Stream stream, out BothUInt16 value) - { - if (stream.Position > stream.Length - 4) - { - value = default(ushort); - return false; - } - - value = stream.ReadUInt16BothEndian(); - return true; - } - - /// - /// Read a WORD (2-byte) from the stream - /// - /// Reads in machine native format - public static bool TryReadWORD(this Stream stream, out ushort value) - => stream.TryReadUInt16(out value); - - /// - /// Read a WORD (2-byte) from the stream - /// - /// Reads in big-endian format - public static bool TryReadWORDBigEndian(this Stream stream, out ushort value) - => stream.TryReadUInt16BigEndian(out value); - - /// - /// Read a WORD (2-byte) from the stream - /// - /// Reads in little-endian format - public static bool TryReadWORDLittleEndian(this Stream stream, out ushort value) - => stream.TryReadUInt16LittleEndian(out value); - - /// - /// Read a WORD (2-byte) from the stream - /// - /// Reads in both-endian format - public static bool TryReadWORDBothEndian(this Stream stream, out BothUInt16 value) - => stream.TryReadUInt16BothEndian(out value); - -#if NET5_0_OR_GREATER - /// - /// Read a Half from the stream - /// - /// Reads in machine native format - public static bool TryReadHalf(this Stream stream, out Half value) - { - if (BitConverter.IsLittleEndian) - return stream.TryReadHalfLittleEndian(out value); - else - return stream.TryReadHalfBigEndian(out value); - } - - /// - /// Read a Half from the stream - /// - /// Reads in big-endian format - public static bool TryReadHalfBigEndian(this Stream stream, out Half value) - { - if (stream.Position > stream.Length - 2) - { - value = default; - return false; - } - - value = stream.ReadHalfBigEndian(); - return true; - } - - /// - /// Read a Half from the stream - /// - /// Reads in little-endian format - public static bool TryReadHalfLittleEndian(this Stream stream, out Half value) - { - if (stream.Position > stream.Length - 2) - { - value = default; - return false; - } - - value = stream.ReadHalfLittleEndian(); - return true; - } -#endif - - /// - /// Read an Int24 from the stream - /// - /// Reads in machine native format - public static bool TryReadInt24(this Stream stream, out Int24 value) - { - if (BitConverter.IsLittleEndian) - return stream.TryReadInt24LittleEndian(out value); - else - return stream.TryReadInt24BigEndian(out value); - } - - /// - /// Read an Int24 from the stream - /// - /// Reads in big-endian format - public static bool TryReadInt24BigEndian(this Stream stream, out Int24 value) - { - if (stream.Position > stream.Length - 3) - { - value = new Int24(); - return false; - } - - value = stream.ReadInt24BigEndian(); - return true; - } - - /// - /// Read an Int24 from the stream - /// - /// Reads in little-endian format - public static bool TryReadInt24LittleEndian(this Stream stream, out Int24 value) - { - if (stream.Position > stream.Length - 3) - { - value = new Int24(); - return false; - } - - value = stream.ReadInt24LittleEndian(); - return true; - } - - /// - /// Read a UInt24 from the stream - /// - /// Reads in machine native format - public static bool TryReadUInt24(this Stream stream, out UInt24 value) - { - if (BitConverter.IsLittleEndian) - return stream.TryReadUInt24LittleEndian(out value); - else - return stream.TryReadUInt24BigEndian(out value); - } - - /// - /// Read a UInt24 from the stream - /// - /// Reads in big-endian format - public static bool TryReadUInt24BigEndian(this Stream stream, out UInt24 value) - { - if (stream.Position > stream.Length - 3) - { - value = new UInt24(); - return false; - } - - value = stream.ReadUInt24BigEndian(); - return true; - } - - /// - /// Read a UInt24 from the stream - /// - /// Reads in little-endian format - public static bool TryReadUInt24LittleEndian(this Stream stream, out UInt24 value) - { - if (stream.Position > stream.Length - 3) - { - value = new UInt24(); - return false; - } - - value = stream.ReadUInt24LittleEndian(); - return true; - } - - /// - /// Read an Int32 from the stream - /// - /// Reads in machine native format - public static bool TryReadInt32(this Stream stream, out int value) - { - if (BitConverter.IsLittleEndian) - return stream.TryReadInt32LittleEndian(out value); - else - return stream.TryReadInt32BigEndian(out value); - } - - /// - /// Read an Int32 from the stream - /// - /// Reads in big-endian format - public static bool TryReadInt32BigEndian(this Stream stream, out int value) - { - if (stream.Position > stream.Length - 4) - { - value = default; - return false; - } - - value = stream.ReadInt32BigEndian(); - return true; - } - - /// - /// Read an Int32 from the stream - /// - /// Reads in little-endian format - public static bool TryReadInt32LittleEndian(this Stream stream, out int value) - { - if (stream.Position > stream.Length - 4) - { - value = default; - return false; - } - - value = stream.ReadInt32LittleEndian(); - return true; - } - - /// - /// Read a Int32 from the stream - /// - /// Reads in both-endian format - public static bool TryReadInt32BothEndian(this Stream stream, out BothInt32 value) - { - if (stream.Position > stream.Length - 8) - { - value = default(int); - return false; - } - - value = stream.ReadInt32BothEndian(); - return true; - } - - /// - /// Read a UInt32 from the stream - /// - /// Reads in machine native format - public static bool TryReadUInt32(this Stream stream, out uint value) - { - if (BitConverter.IsLittleEndian) - return stream.TryReadUInt32LittleEndian(out value); - else - return stream.TryReadUInt32BigEndian(out value); - } - - /// - /// Read a UInt32 from the stream - /// - /// Reads in big-endian format - public static bool TryReadUInt32BigEndian(this Stream stream, out uint value) - { - if (stream.Position > stream.Length - 4) - { - value = default; - return false; - } - - value = stream.ReadUInt32BigEndian(); - return true; - } - - /// - /// Read a UInt32 from the stream - /// - /// Reads in little-endian format - public static bool TryReadUInt32LittleEndian(this Stream stream, out uint value) - { - if (stream.Position > stream.Length - 4) - { - value = default; - return false; - } - - value = stream.ReadUInt32LittleEndian(); - return true; - } - - /// - /// Read a UInt32 from the stream - /// - /// Reads in both-endian format - public static bool TryReadUInt32BothEndian(this Stream stream, out BothUInt32 value) - { - if (stream.Position > stream.Length - 8) - { - value = default(uint); - return false; - } - - value = stream.ReadUInt32BothEndian(); - return true; - } - - /// - /// Read a DWORD (4-byte) from the stream - /// - /// Reads in machine native format - public static bool TryReadDWORD(this Stream stream, out uint value) - => stream.TryReadUInt32(out value); - - /// - /// Read a DWORD (4-byte) from the stream - /// - /// Reads in big-endian format - public static bool TryReadDWORDBigEndian(this Stream stream, out uint value) - => stream.TryReadUInt32BigEndian(out value); - - /// - /// Read a DWORD (4-byte) from the stream - /// - /// Reads in little-endian format - public static bool TryReadDWORDLittleEndian(this Stream stream, out uint value) - => stream.TryReadUInt32LittleEndian(out value); - - /// - /// Read a DWORD (4-byte) from the stream - /// - /// Reads in both-endian format - public static bool TryReadDWORDBothEndian(this Stream stream, out BothUInt32 value) - => stream.TryReadUInt32BothEndian(out value); - - /// - /// Read a Single from the stream - /// - /// Reads in machine native format - public static bool TryReadSingle(this Stream stream, out float value) - { - if (BitConverter.IsLittleEndian) - return stream.TryReadSingleLittleEndian(out value); - else - return stream.TryReadSingleBigEndian(out value); - } - - /// - /// Read a Single from the stream - /// - /// Reads in big-endian format - public static bool TryReadSingleBigEndian(this Stream stream, out float value) - { - if (stream.Position > stream.Length - 4) - { - value = default; - return false; - } - - value = stream.ReadSingleBigEndian(); - return true; - } - - /// - /// Read a Single from the stream - /// - /// Reads in little-endian format - public static bool TryReadSingleLittleEndian(this Stream stream, out float value) - { - if (stream.Position > stream.Length - 4) - { - value = default; - return false; - } - - value = stream.ReadSingleLittleEndian(); - return true; - } - - /// - /// Read an Int48 from the stream - /// - /// Reads in machine native format - public static bool TryReadInt48(this Stream stream, out Int48 value) - { - if (BitConverter.IsLittleEndian) - return stream.TryReadInt48LittleEndian(out value); - else - return stream.TryReadInt48BigEndian(out value); - } - - /// - /// Read an Int48 from the stream - /// - /// Reads in big-endian format - public static bool TryReadInt48BigEndian(this Stream stream, out Int48 value) - { - if (stream.Position > stream.Length - 6) - { - value = new Int48(); - return false; - } - - value = stream.ReadInt48BigEndian(); - return true; - } - - /// - /// Read an Int48 from the stream - /// - /// Reads in little-endian format - public static bool TryReadInt48LittleEndian(this Stream stream, out Int48 value) - { - if (stream.Position > stream.Length - 6) - { - value = new Int48(); - return false; - } - - value = stream.ReadInt48LittleEndian(); - return true; - } - - /// - /// Read a UInt48 from the stream - /// - /// Reads in machine native format - public static bool TryReadUInt48(this Stream stream, out UInt48 value) - { - if (BitConverter.IsLittleEndian) - return stream.TryReadUInt48LittleEndian(out value); - else - return stream.TryReadUInt48BigEndian(out value); - } - - /// - /// Read a UInt48 from the stream - /// - /// Reads in big-endian format - public static bool TryReadUInt48BigEndian(this Stream stream, out UInt48 value) - { - if (stream.Position > stream.Length - 6) - { - value = new UInt48(); - return false; - } - - value = stream.ReadUInt48BigEndian(); - return true; - } - - /// - /// Read an UInt48 from the stream - /// - /// Reads in little-endian format - public static bool TryReadUInt48LittleEndian(this Stream stream, out UInt48 value) - { - if (stream.Position > stream.Length - 6) - { - value = new UInt48(); - return false; - } - - value = stream.ReadUInt48LittleEndian(); - return true; - } - - /// - /// Read an Int64 from the stream - /// - /// Reads in machine native format - public static bool TryReadInt64(this Stream stream, out long value) - { - if (BitConverter.IsLittleEndian) - return stream.TryReadInt64LittleEndian(out value); - else - return stream.TryReadInt64BigEndian(out value); - } - - /// - /// Read an Int64 from the stream - /// - /// Reads in big-endian format - public static bool TryReadInt64BigEndian(this Stream stream, out long value) - { - if (stream.Position > stream.Length - 8) - { - value = default; - return false; - } - - value = stream.ReadInt64BigEndian(); - return true; - } - - /// - /// Read an Int64 from the stream - /// - /// Reads in little-endian format - public static bool TryReadInt64LittleEndian(this Stream stream, out long value) - { - if (stream.Position > stream.Length - 8) - { - value = default; - return false; - } - - value = stream.ReadInt64BigEndian(); - return true; - } - - /// - /// Read a Int64 from the stream - /// - /// Reads in both-endian format - public static bool TryReadInt64BothEndian(this Stream stream, out BothInt64 value) - { - if (stream.Position > stream.Length - 16) - { - value = default(long); - return false; - } - - value = stream.ReadInt64BothEndian(); - return true; - } - - /// - /// Read a UInt64 from the stream - /// - /// Reads in machine native format - public static bool TryReadUInt64(this Stream stream, out ulong value) - { - if (BitConverter.IsLittleEndian) - return stream.TryReadUInt64LittleEndian(out value); - else - return stream.TryReadUInt64BigEndian(out value); - } - - /// - /// Read a UInt64 from the stream - /// - /// Reads in big-endian format - public static bool TryReadUInt64BigEndian(this Stream stream, out ulong value) - { - if (stream.Position > stream.Length - 8) - { - value = default; - return false; - } - - value = stream.ReadUInt64BigEndian(); - return true; - } - - /// - /// Read a UInt64 from the stream - /// - /// Reads in little-endian format - public static bool TryReadUInt64LittleEndian(this Stream stream, out ulong value) - { - if (stream.Position > stream.Length - 8) - { - value = default; - return false; - } - - value = stream.ReadUInt64LittleEndian(); - return true; - } - - /// - /// Read a UInt64 from the stream - /// - /// Reads in both-endian format - public static bool TryReadUInt64BothEndian(this Stream stream, out BothUInt64 value) - { - if (stream.Position > stream.Length - 16) - { - value = default(ulong); - return false; - } - - value = stream.ReadUInt64BothEndian(); - return true; - } - - /// - /// Read a QWORD (8-byte) from the stream - /// - /// Reads in machine native format - public static bool TryReadQWORD(this Stream stream, out ulong value) - => stream.TryReadUInt64(out value); - - /// - /// Read a QWORD (8-byte) from the stream - /// - /// Reads in big-endian format - public static bool TryReadQWORDBigEndian(this Stream stream, out ulong value) - => stream.TryReadUInt64BigEndian(out value); - - /// - /// Read a QWORD (8-byte) from the stream - /// - /// Reads in little-endian format - public static bool TryReadQWORDLittleEndian(this Stream stream, out ulong value) - => stream.TryReadUInt64LittleEndian(out value); - - /// - /// Read a QWORD (8-byte) from the stream - /// - /// Reads in both-endian format - public static bool TryReadQWORDBothEndian(this Stream stream, out BothUInt64 value) - => stream.TryReadUInt64BothEndian(out value); - - /// - /// Read a Double from the stream - /// - /// Reads in machine native format - public static bool TryReadDouble(this Stream stream, out double value) - { - if (BitConverter.IsLittleEndian) - return stream.TryReadDoubleLittleEndian(out value); - else - return stream.TryReadDoubleBigEndian(out value); - } - - /// - /// Read a Double from the stream - /// - /// Reads in big-endian format - public static bool TryReadDoubleBigEndian(this Stream stream, out double value) - { - if (stream.Position > stream.Length - 8) - { - value = default; - return false; - } - - value = stream.ReadDoubleBigEndian(); - return true; - } - - /// - /// Read a Double from the stream - /// - /// Reads in little-endian format - public static bool TryReadDoubleLittleEndian(this Stream stream, out double value) - { - if (stream.Position > stream.Length - 8) - { - value = default; - return false; - } - - value = stream.ReadDoubleLittleEndian(); - return true; - } - - /// - /// Read a Guid from the stream - /// - /// Reads in machine native format - public static bool TryReadGuid(this Stream stream, out Guid value) - { - if (BitConverter.IsLittleEndian) - return stream.TryReadGuidLittleEndian(out value); - else - return stream.TryReadGuidBigEndian(out value); - } - - /// - /// Read a Guid from the stream - /// - /// Reads in big-endian format - public static bool TryReadGuidBigEndian(this Stream stream, out Guid value) - { - if (stream.Position > stream.Length - 16) - { - value = default; - return false; - } - - value = stream.ReadGuidBigEndian(); - return true; - } - - /// - /// Read a Guid from the stream - /// - /// Reads in little-endian format - public static bool TryReadGuidLittleEndian(this Stream stream, out Guid value) - { - if (stream.Position > stream.Length - 16) - { - value = default; - return false; - } - - value = stream.ReadGuidLittleEndian(); - return true; - } - -#if NET7_0_OR_GREATER - /// - /// Read an Int128 from the stream - /// - /// Reads in machine native format - public static bool TryReadInt128(this Stream stream, out Int128 value) - { - if (BitConverter.IsLittleEndian) - return stream.TryReadInt128LittleEndian(out value); - else - return stream.TryReadInt128BigEndian(out value); - } - - /// - /// Read an Int128 from the stream - /// - /// Reads in big-endian format - public static bool TryReadInt128BigEndian(this Stream stream, out Int128 value) - { - if (stream.Position > stream.Length - 16) - { - value = default; - return false; - } - - value = stream.ReadInt128BigEndian(); - return true; - } - - /// - /// Read an Int128 from the stream - /// - /// Reads in little-endian format - public static bool TryReadInt128LittleEndian(this Stream stream, out Int128 value) - { - if (stream.Position > stream.Length - 16) - { - value = default; - return false; - } - - value = stream.ReadInt128LittleEndian(); - return true; - } - - /// - /// Read a UInt128 from the stream - /// - /// Reads in machine native format - public static bool TryReadUInt128(this Stream stream, out UInt128 value) - { - if (BitConverter.IsLittleEndian) - return stream.TryReadUInt128LittleEndian(out value); - else - return stream.TryReadUInt128BigEndian(out value); - } - - /// - /// Read a UInt128 from the stream - /// - /// Reads in big-endian format - public static bool TryReadUInt128BigEndian(this Stream stream, out UInt128 value) - { - if (stream.Position > stream.Length - 16) - { - value = default; - return false; - } - - value = stream.ReadUInt128BigEndian(); - return true; - } - - /// - /// Read a UInt128 from the stream - /// - /// Reads in little-endian format - public static bool TryReadUInt128LittleEndian(this Stream stream, out UInt128 value) - { - if (stream.Position > stream.Length - 16) - { - value = default; - return false; - } - - value = stream.ReadUInt128LittleEndian(); - return true; - } -#endif - - /// - /// Read a Decimal from the stream - /// - /// Reads in machine native format - public static bool TryReadDecimal(this Stream stream, out decimal value) - { - if (BitConverter.IsLittleEndian) - return stream.TryReadDecimalLittleEndian(out value); - else - return stream.TryReadDecimalBigEndian(out value); - } - - /// - /// Read a Decimal from the stream - /// - /// Reads in big-endian format - public static bool TryReadDecimalBigEndian(this Stream stream, out decimal value) - { - if (stream.Position > stream.Length - 16) - { - value = default; - return false; - } - - value = stream.ReadDecimalBigEndian(); - return true; - } - - /// - /// Read a Decimal from the stream - /// - /// Reads in little-endian format - public static bool TryReadDecimalLittleEndian(this Stream stream, out decimal value) - { - if (stream.Position > stream.Length - 16) - { - value = default; - return false; - } - - value = stream.ReadDecimalLittleEndian(); - return true; - } - - #endregion } } diff --git a/SabreTools.IO.Extensions/StreamWriterExtensions.cs b/SabreTools.IO.Extensions/StreamWriterExtensions.cs index 74384bd..abad37a 100644 --- a/SabreTools.IO.Extensions/StreamWriterExtensions.cs +++ b/SabreTools.IO.Extensions/StreamWriterExtensions.cs @@ -3,7 +3,7 @@ using System.IO; using System.Reflection; using System.Runtime.InteropServices; using System.Text; -using SabreTools.Numerics; +using SabreTools.Numerics.Extensions; namespace SabreTools.IO.Extensions { @@ -12,687 +12,6 @@ namespace SabreTools.IO.Extensions /// public static class StreamWriterExtensions { - /// - /// Write a UInt8 - /// - public static bool Write(this Stream stream, byte value) - => WriteFromBuffer(stream, [value]); - - /// - /// Write a UInt8 - /// - /// Writes in both-endian format - public static bool WriteBothEndian(this Stream stream, BothUInt8 value) - { - bool actual = stream.Write(value.LittleEndian); - actual &= stream.Write(value.BigEndian); - return actual; - } - - /// - /// Write a UInt8[] - /// - public static bool Write(this Stream stream, byte[] value) - => WriteFromBuffer(stream, value); - - /// - /// Write a UInt8[] - /// - /// Writes in big-endian format - public static bool WriteBigEndian(this Stream stream, byte[] value) - { - Array.Reverse(value); - return WriteFromBuffer(stream, value); - } - - /// - /// Write an Int8 - /// - public static bool Write(this Stream stream, sbyte value) - => WriteFromBuffer(stream, [(byte)value]); - - /// - /// Write a Int8 - /// - /// Writes in both-endian format - public static bool WriteBothEndian(this Stream stream, BothInt8 value) - { - bool actual = stream.Write(value.LittleEndian); - actual &= stream.Write(value.BigEndian); - return actual; - } - - /// - /// Write a Char - /// - public static bool Write(this Stream stream, char value) - { - byte[] buffer = BitConverter.GetBytes(value); - return WriteFromBuffer(stream, buffer); - } - - /// - /// Write a Char with an Encoding - /// - public static bool Write(this Stream stream, char value, Encoding encoding) - { - byte[] buffer = encoding.GetBytes($"{value}"); - return WriteFromBuffer(stream, buffer); - } - - /// - /// Write an Int16 - /// - /// Writes in machine native format - public static bool Write(this Stream stream, short value) - { - if (BitConverter.IsLittleEndian) - return stream.WriteLittleEndian(value); - else - return stream.WriteBigEndian(value); - } - - /// - /// Write an Int16 - /// - /// Writes in big-endian format - public static bool WriteBigEndian(this Stream stream, short value) - { - byte[] buffer = value.GetBytesBigEndian(); - return WriteFromBuffer(stream, buffer); - } - - /// - /// Write an Int16 - /// - /// Writes in little-endian format - public static bool WriteLittleEndian(this Stream stream, short value) - { - byte[] buffer = value.GetBytesLittleEndian(); - return WriteFromBuffer(stream, buffer); - } - - /// - /// Write a Int16 - /// - /// Writes in both-endian format - public static bool WriteBothEndian(this Stream stream, BothInt16 value) - { - bool actual = stream.WriteLittleEndian(value.LittleEndian); - actual &= stream.WriteBigEndian(value.BigEndian); - return actual; - } - - /// - /// Write a UInt16 - /// - /// Writes in machine native format - public static bool Write(this Stream stream, ushort value) - { - if (BitConverter.IsLittleEndian) - return stream.WriteLittleEndian(value); - else - return stream.WriteBigEndian(value); - } - - /// - /// Write a UInt16 - /// - /// Writes in big-endian format - public static bool WriteBigEndian(this Stream stream, ushort value) - { - byte[] buffer = value.GetBytesBigEndian(); - return WriteFromBuffer(stream, buffer); - } - - /// - /// Write a UInt16 - /// - /// Writes in little-endian format - public static bool WriteLittleEndian(this Stream stream, ushort value) - { - byte[] buffer = value.GetBytesLittleEndian(); - return WriteFromBuffer(stream, buffer); - } - - /// - /// Write a UInt16 - /// - /// Writes in both-endian format - public static bool WriteBothEndian(this Stream stream, BothUInt16 value) - { - bool actual = stream.WriteLittleEndian(value.LittleEndian); - actual &= stream.WriteBigEndian(value.BigEndian); - return actual; - } - -#if NET5_0_OR_GREATER - /// - /// Write a Half - /// - /// Writes in machine native format - public static bool Write(this Stream stream, Half value) - { - if (BitConverter.IsLittleEndian) - return stream.WriteLittleEndian(value); - else - return stream.WriteBigEndian(value); - } - - /// - /// Write a Half - /// - /// Writes in big-endian format - public static bool WriteBigEndian(this Stream stream, Half value) - { - byte[] buffer = value.GetBytesBigEndian(); - return WriteFromBuffer(stream, buffer); - } - - /// - /// Write a Half - /// - /// Writes in little-endian format - public static bool WriteLittleEndian(this Stream stream, Half value) - { - byte[] buffer = value.GetBytesLittleEndian(); - return WriteFromBuffer(stream, buffer); - } -#endif - - /// - /// Write an Int24 - /// - /// Writes in machine native format - public static bool Write(this Stream stream, Int24 value) - { - if (BitConverter.IsLittleEndian) - return stream.WriteLittleEndian(value); - else - return stream.WriteBigEndian(value); - } - - /// - /// Write an Int24 - /// - /// Writes in big-endian format - public static bool WriteBigEndian(this Stream stream, Int24 value) - { - byte[] buffer = value.GetBytesBigEndian(); - return WriteFromBuffer(stream, buffer); - } - - /// - /// Write an Int24 - /// - /// Writes in little-endian format - public static bool WriteLittleEndian(this Stream stream, Int24 value) - { - byte[] buffer = value.GetBytesLittleEndian(); - return WriteFromBuffer(stream, buffer); - } - - /// - /// Write a UInt24 - /// - /// Writes in machine native format - public static bool Write(this Stream stream, UInt24 value) - { - if (BitConverter.IsLittleEndian) - return stream.WriteLittleEndian(value); - else - return stream.WriteBigEndian(value); - } - - /// - /// Write a UInt24 - /// - /// Writes in big-endian format - public static bool WriteBigEndian(this Stream stream, UInt24 value) - { - byte[] buffer = value.GetBytesBigEndian(); - return WriteFromBuffer(stream, buffer); - } - - /// - /// Write a UInt24 - /// - /// Writes in little-endian format - public static bool WriteLittleEndian(this Stream stream, UInt24 value) - { - byte[] buffer = value.GetBytesLittleEndian(); - return WriteFromBuffer(stream, buffer); - } - - /// - /// Write an Int32 - /// - /// Writes in machine native format - public static bool Write(this Stream stream, int value) - { - if (BitConverter.IsLittleEndian) - return stream.WriteLittleEndian(value); - else - return stream.WriteBigEndian(value); - } - - /// - /// Write an Int32 - /// - /// Writes in big-endian format - public static bool WriteBigEndian(this Stream stream, int value) - { - byte[] buffer = value.GetBytesBigEndian(); - return WriteFromBuffer(stream, buffer); - } - - /// - /// Write an Int32 - /// - /// Writes in little-endian format - public static bool WriteLittleEndian(this Stream stream, int value) - { - byte[] buffer = value.GetBytesLittleEndian(); - return WriteFromBuffer(stream, buffer); - } - - /// - /// Write a Int32 - /// - /// Writes in both-endian format - public static bool WriteBothEndian(this Stream stream, BothInt32 value) - { - bool actual = stream.WriteLittleEndian(value.LittleEndian); - actual &= stream.WriteBigEndian(value.BigEndian); - return actual; - } - - /// - /// Write a UInt32 - /// - /// Writes in machine native format - public static bool Write(this Stream stream, uint value) - { - if (BitConverter.IsLittleEndian) - return stream.WriteLittleEndian(value); - else - return stream.WriteBigEndian(value); - } - - /// - /// Write a UInt32 - /// - /// Writes in big-endian format - public static bool WriteBigEndian(this Stream stream, uint value) - { - byte[] buffer = value.GetBytesBigEndian(); - return WriteFromBuffer(stream, buffer); - } - - /// - /// Write a UInt32 - /// - /// Writes in little-endian format - public static bool WriteLittleEndian(this Stream stream, uint value) - { - byte[] buffer = value.GetBytesLittleEndian(); - return WriteFromBuffer(stream, buffer); - } - - /// - /// Write a UInt32 - /// - /// Writes in both-endian format - public static bool WriteBothEndian(this Stream stream, BothUInt32 value) - { - bool actual = stream.WriteLittleEndian(value.LittleEndian); - actual &= stream.WriteBigEndian(value.BigEndian); - return actual; - } - - /// - /// Write a Single - /// - /// Writes in machine native format - public static bool Write(this Stream stream, float value) - { - if (BitConverter.IsLittleEndian) - return stream.WriteLittleEndian(value); - else - return stream.WriteBigEndian(value); - } - - /// - /// Write a Single - /// - /// Writes in big-endian format - public static bool WriteBigEndian(this Stream stream, float value) - { - byte[] buffer = value.GetBytesBigEndian(); - return WriteFromBuffer(stream, buffer); - } - - /// - /// Write a Single - /// - public static bool WriteLittleEndian(this Stream stream, float value) - { - byte[] buffer = value.GetBytesLittleEndian(); - return WriteFromBuffer(stream, buffer); - } - - /// - /// Write an Int48 - /// - /// Writes in machine native format - public static bool Write(this Stream stream, Int48 value) - { - if (BitConverter.IsLittleEndian) - return stream.WriteLittleEndian(value); - else - return stream.WriteBigEndian(value); - } - - /// - /// Write an Int48 - /// - /// Writes in big-endian format - public static bool WriteBigEndian(this Stream stream, Int48 value) - { - byte[] buffer = value.GetBytesBigEndian(); - return WriteFromBuffer(stream, buffer); - } - - /// - /// Write an Int48 - /// - /// Writes in little-endian format - public static bool WriteLittleEndian(this Stream stream, Int48 value) - { - byte[] buffer = value.GetBytesLittleEndian(); - return WriteFromBuffer(stream, buffer); - } - - /// - /// Write a UInt48 - /// - /// Writes in machine native format - public static bool Write(this Stream stream, UInt48 value) - { - if (BitConverter.IsLittleEndian) - return stream.WriteLittleEndian(value); - else - return stream.WriteBigEndian(value); - } - - /// - /// Write a UInt48 - /// - /// Writes in big-endian format - public static bool WriteBigEndian(this Stream stream, UInt48 value) - { - byte[] buffer = value.GetBytesBigEndian(); - return WriteFromBuffer(stream, buffer); - } - - /// - /// Write a UInt48 - /// - /// Writes in little-endian format - public static bool WriteLittleEndian(this Stream stream, UInt48 value) - { - byte[] buffer = value.GetBytesLittleEndian(); - return WriteFromBuffer(stream, buffer); - } - - /// - /// Write an Int64 - /// - /// Writes in machine native format - public static bool Write(this Stream stream, long value) - { - if (BitConverter.IsLittleEndian) - return stream.WriteLittleEndian(value); - else - return stream.WriteBigEndian(value); - } - - /// - /// Write an Int64 - /// - /// Writes in big-endian format - public static bool WriteBigEndian(this Stream stream, long value) - { - byte[] buffer = value.GetBytesBigEndian(); - return WriteFromBuffer(stream, buffer); - } - - /// - /// Write an Int64 - /// - /// Writes in little-endian format - public static bool WriteLittleEndian(this Stream stream, long value) - { - byte[] buffer = value.GetBytesLittleEndian(); - return WriteFromBuffer(stream, buffer); - } - - /// - /// Write a Int64 - /// - /// Writes in both-endian format - public static bool WriteBothEndian(this Stream stream, BothInt64 value) - { - bool actual = stream.WriteLittleEndian(value.LittleEndian); - actual &= stream.WriteBigEndian(value.BigEndian); - return actual; - } - - /// - /// Write a UInt64 - /// - /// Writes in machine native format - public static bool Write(this Stream stream, ulong value) - { - if (BitConverter.IsLittleEndian) - return stream.WriteLittleEndian(value); - else - return stream.WriteBigEndian(value); - } - - /// - /// Write a UInt64 - /// - /// Writes in big-endian format - public static bool WriteBigEndian(this Stream stream, ulong value) - { - byte[] buffer = value.GetBytesBigEndian(); - return WriteFromBuffer(stream, buffer); - } - - /// - /// Write a UInt64 - /// - /// Writes in little-endian format - public static bool WriteLittleEndian(this Stream stream, ulong value) - { - byte[] buffer = value.GetBytesLittleEndian(); - return WriteFromBuffer(stream, buffer); - } - - /// - /// Write a UInt64 - /// - /// Writes in both-endian format - public static bool WriteBothEndian(this Stream stream, BothUInt64 value) - { - bool actual = stream.WriteLittleEndian(value.LittleEndian); - actual &= stream.WriteBigEndian(value.BigEndian); - return actual; - } - - /// - /// Write a Double - /// - /// Writes in machine native format - public static bool Write(this Stream stream, double value) - { - if (BitConverter.IsLittleEndian) - return stream.WriteLittleEndian(value); - else - return stream.WriteBigEndian(value); - } - - /// - /// Write a Double - /// - /// Writes in big-endian format - public static bool WriteBigEndian(this Stream stream, double value) - { - byte[] buffer = value.GetBytesBigEndian(); - return WriteFromBuffer(stream, buffer); - } - - /// - /// Write a Double - /// - /// Writes in little-endian format - public static bool WriteLittleEndian(this Stream stream, double value) - { - byte[] buffer = value.GetBytesLittleEndian(); - return WriteFromBuffer(stream, buffer); - } - - /// - /// Write a Guid - /// - /// Writes in machine native format - public static bool Write(this Stream stream, Guid value) - { - if (BitConverter.IsLittleEndian) - return stream.WriteLittleEndian(value); - else - return stream.WriteBigEndian(value); - } - - /// - /// Write a Guid - /// - /// Writes in big-endian format - public static bool WriteBigEndian(this Stream stream, Guid value) - { - byte[] buffer = value.GetBytesBigEndian(); - return WriteFromBuffer(stream, buffer); - } - - /// - /// Write a Guid - /// - /// Writes in little-endian format - public static bool WriteLittleEndian(this Stream stream, Guid value) - { - byte[] buffer = value.GetBytesLittleEndian(); - return WriteFromBuffer(stream, buffer); - } - -#if NET7_0_OR_GREATER - /// - /// Write an Int128 - /// - /// Writes in machine native format - public static bool Write(this Stream stream, Int128 value) - { - if (BitConverter.IsLittleEndian) - return stream.WriteLittleEndian(value); - else - return stream.WriteBigEndian(value); - } - - /// - /// Write an Int128 - /// - /// Writes in big-endian format - public static bool WriteBigEndian(this Stream stream, Int128 value) - { - byte[] buffer = value.GetBytesBigEndian(); - return WriteFromBuffer(stream, buffer); - } - - /// - /// Write an Int128 - /// - /// Writes in little-endian format - public static bool WriteLittleEndian(this Stream stream, Int128 value) - { - byte[] buffer = value.GetBytesLittleEndian(); - return WriteFromBuffer(stream, buffer); - } - - /// - /// Write a UInt128 - /// - /// Writes in machine native format - public static bool Write(this Stream stream, UInt128 value) - { - if (BitConverter.IsLittleEndian) - return stream.WriteLittleEndian(value); - else - return stream.WriteBigEndian(value); - } - - /// - /// Write a UInt128 - /// - /// Writes in big-endian format - public static bool WriteBigEndian(this Stream stream, UInt128 value) - { - byte[] buffer = value.GetBytesBigEndian(); - return WriteFromBuffer(stream, buffer); - } - - /// - /// Write a UInt128 - /// - /// Writes in little-endian format - public static bool WriteLittleEndian(this Stream stream, UInt128 value) - { - byte[] buffer = value.GetBytesLittleEndian(); - return WriteFromBuffer(stream, buffer); - } -#endif - - /// - /// Write a Decimal and increment the pointer to an array - /// - /// Writes in machine native format - public static bool Write(this Stream stream, decimal value) - { - if (BitConverter.IsLittleEndian) - return stream.WriteLittleEndian(value); - else - return stream.WriteBigEndian(value); - } - - /// - /// Write a Decimal and increment the pointer to an array - /// - /// Writes in big-endian format - public static bool WriteBigEndian(this Stream stream, decimal value) - { - byte[] buffer = value.GetBytesBigEndian(); - return WriteFromBuffer(stream, buffer); - } - - /// - /// Write a Decimal and increment the pointer to an array - /// - /// Writes in little-endian format - public static bool WriteLittleEndian(this Stream stream, decimal value) - { - byte[] buffer = value.GetBytesLittleEndian(); - return WriteFromBuffer(stream, buffer); - } - /// /// Write a null-terminated string to the stream /// @@ -1030,7 +349,7 @@ namespace SabreTools.IO.Extensions byte[] byvalBytes = encoding.GetBytes(fieldValue); byte[] byvalSizedBytes = new byte[byvalLength]; Array.Copy(byvalBytes, byvalSizedBytes, Math.Min(byvalBytes.Length, byvalSizedBytes.Length)); - return Write(stream, byvalSizedBytes); + return Numerics.Extensions.StreamWriterExtensions.Write(stream, byvalSizedBytes); case UnmanagedType.LPStr: case UnmanagedType.LPTStr: // Technically distinct; possibly not null-terminated diff --git a/SabreTools.IO.Meta/SabreTools.IO.Meta.csproj b/SabreTools.IO.Meta/SabreTools.IO.Meta.csproj index e8cfb1b..859add2 100644 --- a/SabreTools.IO.Meta/SabreTools.IO.Meta.csproj +++ b/SabreTools.IO.Meta/SabreTools.IO.Meta.csproj @@ -44,6 +44,7 @@ + diff --git a/SabreTools.IO.sln b/SabreTools.IO.sln index 150e687..5012170 100644 --- a/SabreTools.IO.sln +++ b/SabreTools.IO.sln @@ -51,6 +51,10 @@ Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "SabreTools.IO.Compression.T EndProject Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "SabreTools.IO.Extensions.Test", "SabreTools.IO.Extensions.Test\SabreTools.IO.Extensions.Test.csproj", "{C829B479-3D3B-48EE-BDD7-5EFB40340166}" EndProject +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "SabreTools.Numerics.Extensions", "SabreTools.Numerics.Extensions\SabreTools.Numerics.Extensions.csproj", "{0D898742-CF37-46F9-AD41-42D7A9DDEDDC}" +EndProject +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "SabreTools.Numerics.Extensions.Test", "SabreTools.Numerics.Extensions.Test\SabreTools.Numerics.Extensions.Test.csproj", "{058292DF-E791-4206-9F67-DBCD76BB284C}" +EndProject Global GlobalSection(SolutionConfigurationPlatforms) = preSolution Debug|Any CPU = Debug|Any CPU @@ -349,6 +353,30 @@ Global {C829B479-3D3B-48EE-BDD7-5EFB40340166}.Release|x64.Build.0 = Release|Any CPU {C829B479-3D3B-48EE-BDD7-5EFB40340166}.Release|x86.ActiveCfg = Release|Any CPU {C829B479-3D3B-48EE-BDD7-5EFB40340166}.Release|x86.Build.0 = Release|Any CPU + {0D898742-CF37-46F9-AD41-42D7A9DDEDDC}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {0D898742-CF37-46F9-AD41-42D7A9DDEDDC}.Debug|Any CPU.Build.0 = Debug|Any CPU + {0D898742-CF37-46F9-AD41-42D7A9DDEDDC}.Debug|x64.ActiveCfg = Debug|Any CPU + {0D898742-CF37-46F9-AD41-42D7A9DDEDDC}.Debug|x64.Build.0 = Debug|Any CPU + {0D898742-CF37-46F9-AD41-42D7A9DDEDDC}.Debug|x86.ActiveCfg = Debug|Any CPU + {0D898742-CF37-46F9-AD41-42D7A9DDEDDC}.Debug|x86.Build.0 = Debug|Any CPU + {0D898742-CF37-46F9-AD41-42D7A9DDEDDC}.Release|Any CPU.ActiveCfg = Release|Any CPU + {0D898742-CF37-46F9-AD41-42D7A9DDEDDC}.Release|Any CPU.Build.0 = Release|Any CPU + {0D898742-CF37-46F9-AD41-42D7A9DDEDDC}.Release|x64.ActiveCfg = Release|Any CPU + {0D898742-CF37-46F9-AD41-42D7A9DDEDDC}.Release|x64.Build.0 = Release|Any CPU + {0D898742-CF37-46F9-AD41-42D7A9DDEDDC}.Release|x86.ActiveCfg = Release|Any CPU + {0D898742-CF37-46F9-AD41-42D7A9DDEDDC}.Release|x86.Build.0 = Release|Any CPU + {058292DF-E791-4206-9F67-DBCD76BB284C}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {058292DF-E791-4206-9F67-DBCD76BB284C}.Debug|Any CPU.Build.0 = Debug|Any CPU + {058292DF-E791-4206-9F67-DBCD76BB284C}.Debug|x64.ActiveCfg = Debug|Any CPU + {058292DF-E791-4206-9F67-DBCD76BB284C}.Debug|x64.Build.0 = Debug|Any CPU + {058292DF-E791-4206-9F67-DBCD76BB284C}.Debug|x86.ActiveCfg = Debug|Any CPU + {058292DF-E791-4206-9F67-DBCD76BB284C}.Debug|x86.Build.0 = Debug|Any CPU + {058292DF-E791-4206-9F67-DBCD76BB284C}.Release|Any CPU.ActiveCfg = Release|Any CPU + {058292DF-E791-4206-9F67-DBCD76BB284C}.Release|Any CPU.Build.0 = Release|Any CPU + {058292DF-E791-4206-9F67-DBCD76BB284C}.Release|x64.ActiveCfg = Release|Any CPU + {058292DF-E791-4206-9F67-DBCD76BB284C}.Release|x64.Build.0 = Release|Any CPU + {058292DF-E791-4206-9F67-DBCD76BB284C}.Release|x86.ActiveCfg = Release|Any CPU + {058292DF-E791-4206-9F67-DBCD76BB284C}.Release|x86.Build.0 = Release|Any CPU EndGlobalSection GlobalSection(SolutionProperties) = preSolution HideSolutionNode = FALSE diff --git a/SabreTools.Numerics.Extensions.Test/BinaryReaderExtensionsTests.cs b/SabreTools.Numerics.Extensions.Test/BinaryReaderExtensionsTests.cs new file mode 100644 index 0000000..e048d71 --- /dev/null +++ b/SabreTools.Numerics.Extensions.Test/BinaryReaderExtensionsTests.cs @@ -0,0 +1,2341 @@ +using System; +using System.IO; +using System.Linq; +using System.Numerics; +using Xunit; + +namespace SabreTools.Numerics.Extensions.Test +{ + public class BinaryReaderExtensionsTests + { + /// + /// Test pattern from 0x00-0x0F + /// + private static readonly byte[] _bytes = + [ + 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, + 0x08, 0x09, 0x0A, 0x0B, 0x0C, 0x0D, 0x0E, 0x0F, + ]; + + /// + /// Represents the decimal value 0.0123456789 + /// + private static readonly byte[] _decimalBytes = + [ + 0x15, 0xCD, 0x5B, 0x07, 0x00, 0x00, 0x00, 0x00, + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x0A, 0x00, + ]; + + /// + /// Test pattern for big-endian GUID created from + /// + private static readonly byte[] _guidBigEndianbytes = + [ + 0x03, 0x02, 0x01, 0x00, 0x05, 0x04, 0x07, 0x06, + 0x08, 0x09, 0x0A, 0x0B, 0x0C, 0x0D, 0x0E, 0x0F, + ]; + + #region Exact Read + + [Fact] + public void ReadByteArrayTest() + { + byte[] arr = new byte[4]; + var stream = new MemoryStream(_bytes); + var br = new BinaryReader(stream); + int read = br.Read(arr, 0, 4); + Assert.Equal(4, read); + Assert.True(arr.SequenceEqual(_bytes.Take(4))); + } + + [Fact] + public void ReadCharArrayTest() + { + char[] arr = new char[4]; + var stream = new MemoryStream(_bytes); + var br = new BinaryReader(stream); + int read = br.Read(arr, 0, 4); + Assert.Equal(4, read); + Assert.True(arr.SequenceEqual(_bytes.Take(4).Select(b => (char)b))); + } + + [Fact] + public void ReadByteTest() + { + var stream = new MemoryStream(_bytes); + var br = new BinaryReader(stream); + byte read = br.ReadByte(); + Assert.Equal(0x00, read); + } + + [Fact] + public void ReadByteBothEndianTest() + { + var stream = new MemoryStream(_bytes); + var br = new BinaryReader(stream); + BothUInt8 read = br.ReadByteBothEndian(); + Assert.Equal(0x00, read.LittleEndian); + Assert.Equal(0x01, read.BigEndian); + } + + [Fact] + public void ReadBytesTest() + { + var stream = new MemoryStream(_bytes); + var br = new BinaryReader(stream); + int length = 4; + byte[] read = br.ReadBytes(length); + Assert.Equal(length, read.Length); + Assert.True(read.SequenceEqual(_bytes.Take(length))); + } + + [Fact] + public void ReadCharsTest() + { + var stream = new MemoryStream(_bytes); + var br = new BinaryReader(stream); + int length = 4; + char[] read = br.ReadChars(length); + Assert.Equal(length, read.Length); + Assert.True(read.SequenceEqual(_bytes.Take(length).Select(b => (char)b))); + } + + [Fact] + public void ReadSByteTest() + { + var stream = new MemoryStream(_bytes); + var br = new BinaryReader(stream); + sbyte read = br.ReadSByte(); + Assert.Equal(0x00, read); + } + + [Fact] + public void ReadSByteBothEndianTest() + { + var stream = new MemoryStream(_bytes); + var br = new BinaryReader(stream); + BothInt8 read = br.ReadSByteBothEndian(); + Assert.Equal(0x00, read.LittleEndian); + Assert.Equal(0x01, read.BigEndian); + } + + [Fact] + public void ReadCharTest() + { + var stream = new MemoryStream(_bytes); + var br = new BinaryReader(stream); + char read = br.ReadChar(); + Assert.Equal('\0', read); + } + + [Fact] + public void ReadInt16Test() + { + var stream = new MemoryStream(_bytes); + var br = new BinaryReader(stream); + short read = br.ReadInt16(); + Assert.Equal(0x0100, read); + } + + [Fact] + public void ReadInt16BigEndianTest() + { + var stream = new MemoryStream(_bytes); + var br = new BinaryReader(stream); + short read = br.ReadInt16BigEndian(); + Assert.Equal(0x0001, read); + } + + [Fact] + public void ReadInt16LittleEndianTest() + { + var stream = new MemoryStream(_bytes); + var br = new BinaryReader(stream); + short read = br.ReadInt16LittleEndian(); + Assert.Equal(0x0100, read); + } + + [Fact] + public void ReadInt16BothEndianTest() + { + var stream = new MemoryStream(_bytes); + var br = new BinaryReader(stream); + BothInt16 read = br.ReadInt16BothEndian(); + Assert.Equal(0x0100, read.LittleEndian); + Assert.Equal(0x0203, read.BigEndian); + } + + [Fact] + public void ReadUInt16Test() + { + var stream = new MemoryStream(_bytes); + var br = new BinaryReader(stream); + ushort read = br.ReadUInt16(); + Assert.Equal(0x0100, read); + } + + [Fact] + public void ReadUInt16BigEndianTest() + { + var stream = new MemoryStream(_bytes); + var br = new BinaryReader(stream); + ushort read = br.ReadUInt16BigEndian(); + Assert.Equal(0x0001, read); + } + + [Fact] + public void ReadUInt16LittleEndianTest() + { + var stream = new MemoryStream(_bytes); + var br = new BinaryReader(stream); + ushort read = br.ReadUInt16LittleEndian(); + Assert.Equal(0x0100, read); + } + + [Fact] + public void ReadUInt16BothEndianTest() + { + var stream = new MemoryStream(_bytes); + var br = new BinaryReader(stream); + BothUInt16 read = br.ReadUInt16BothEndian(); + Assert.Equal(0x0100, read.LittleEndian); + Assert.Equal(0x0203, read.BigEndian); + } + + [Fact] + public void ReadWORDTest() + { + var stream = new MemoryStream(_bytes); + var br = new BinaryReader(stream); + ushort read = br.ReadWORD(); + Assert.Equal(0x0100, read); + } + + [Fact] + public void ReadWORDBigEndianTest() + { + var stream = new MemoryStream(_bytes); + var br = new BinaryReader(stream); + ushort read = br.ReadWORDBigEndian(); + Assert.Equal(0x0001, read); + } + + [Fact] + public void ReadWORDLittleEndianTest() + { + var stream = new MemoryStream(_bytes); + var br = new BinaryReader(stream); + ushort read = br.ReadWORDLittleEndian(); + Assert.Equal(0x0100, read); + } + + [Fact] + public void ReadWORDBothEndianTest() + { + var stream = new MemoryStream(_bytes); + var br = new BinaryReader(stream); + BothUInt16 read = br.ReadWORDBothEndian(); + Assert.Equal(0x0100, read.LittleEndian); + Assert.Equal(0x0203, read.BigEndian); + } + + [Fact] + public void ReadHalfTest() + { + var stream = new MemoryStream(_bytes); + var br = new BinaryReader(stream); + Half expected = BitConverter.Int16BitsToHalf(0x0100); + Half read = br.ReadHalf(); + Assert.Equal(expected, read); + } + + [Fact] + public void ReadHalfBigEndianTest() + { + var stream = new MemoryStream(_bytes); + var br = new BinaryReader(stream); + Half expected = BitConverter.Int16BitsToHalf(0x0001); + Half read = br.ReadHalfBigEndian(); + Assert.Equal(expected, read); + } + + [Fact] + public void ReadHalfLittleEndianTest() + { + var stream = new MemoryStream(_bytes); + var br = new BinaryReader(stream); + Half expected = BitConverter.Int16BitsToHalf(0x0100); + Half read = br.ReadHalfLittleEndian(); + Assert.Equal(expected, read); + } + + [Fact] + public void ReadInt24Test() + { + var stream = new MemoryStream(_bytes); + var br = new BinaryReader(stream); + Int24 read = br.ReadInt24(); + Assert.Equal(0x020100, (int)read); + } + + [Fact] + public void ReadInt24BigEndianTest() + { + var stream = new MemoryStream(_bytes); + var br = new BinaryReader(stream); + Int24 read = br.ReadInt24BigEndian(); + Assert.Equal(0x000102, (int)read); + } + + [Fact] + public void ReadInt24LittleEndianTest() + { + var stream = new MemoryStream(_bytes); + var br = new BinaryReader(stream); + Int24 read = br.ReadInt24LittleEndian(); + Assert.Equal(0x020100, (int)read); + } + + [Fact] + public void ReadUInt24Test() + { + var stream = new MemoryStream(_bytes); + var br = new BinaryReader(stream); + UInt24 read = br.ReadUInt24(); + Assert.Equal((uint)0x020100, (uint)read); + } + + [Fact] + public void ReadUInt24BigEndianTest() + { + var stream = new MemoryStream(_bytes); + var br = new BinaryReader(stream); + UInt24 read = br.ReadUInt24BigEndian(); + Assert.Equal((uint)0x000102, (uint)read); + } + + [Fact] + public void ReadUInt24LittleEndianTest() + { + var stream = new MemoryStream(_bytes); + var br = new BinaryReader(stream); + UInt24 read = br.ReadUInt24LittleEndian(); + Assert.Equal((uint)0x020100, (uint)read); + } + + [Fact] + public void ReadInt32Test() + { + var stream = new MemoryStream(_bytes); + var br = new BinaryReader(stream); + int read = br.ReadInt32(); + Assert.Equal(0x03020100, read); + } + + [Fact] + public void ReadInt32BigEndianTest() + { + var stream = new MemoryStream(_bytes); + var br = new BinaryReader(stream); + int read = br.ReadInt32BigEndian(); + Assert.Equal(0x00010203, read); + } + + [Fact] + public void ReadInt32LittleEndianTest() + { + var stream = new MemoryStream(_bytes); + var br = new BinaryReader(stream); + int read = br.ReadInt32LittleEndian(); + Assert.Equal(0x03020100, read); + } + + [Fact] + public void ReadInt32BothEndianTest() + { + var stream = new MemoryStream(_bytes); + var br = new BinaryReader(stream); + BothInt32 read = br.ReadInt32BothEndian(); + Assert.Equal(0x03020100, read.LittleEndian); + Assert.Equal(0x04050607, read.BigEndian); + } + + [Fact] + public void ReadUInt32Test() + { + var stream = new MemoryStream(_bytes); + var br = new BinaryReader(stream); + uint read = br.ReadUInt32(); + Assert.Equal((uint)0x03020100, read); + } + + [Fact] + public void ReadUInt32BigEndianTest() + { + var stream = new MemoryStream(_bytes); + var br = new BinaryReader(stream); + uint read = br.ReadUInt32BigEndian(); + Assert.Equal((uint)0x00010203, read); + } + + [Fact] + public void ReadUInt32LittleEndianTest() + { + var stream = new MemoryStream(_bytes); + var br = new BinaryReader(stream); + uint read = br.ReadUInt32LittleEndian(); + Assert.Equal((uint)0x03020100, read); + } + + [Fact] + public void ReadUInt32BothEndianTest() + { + var stream = new MemoryStream(_bytes); + var br = new BinaryReader(stream); + BothUInt32 read = br.ReadUInt32BothEndian(); + Assert.Equal((uint)0x03020100, read.LittleEndian); + Assert.Equal((uint)0x04050607, read.BigEndian); + } + + [Fact] + public void ReadDWORDTest() + { + var stream = new MemoryStream(_bytes); + var br = new BinaryReader(stream); + uint read = br.ReadDWORD(); + Assert.Equal((uint)0x03020100, read); + } + + [Fact] + public void ReadDWORDBigEndianTest() + { + var stream = new MemoryStream(_bytes); + var br = new BinaryReader(stream); + uint read = br.ReadDWORDBigEndian(); + Assert.Equal((uint)0x00010203, read); + } + + [Fact] + public void ReadDWORDLittleEndianTest() + { + var stream = new MemoryStream(_bytes); + var br = new BinaryReader(stream); + uint read = br.ReadDWORDLittleEndian(); + Assert.Equal((uint)0x03020100, read); + } + + [Fact] + public void ReadDWORDBothEndianTest() + { + var stream = new MemoryStream(_bytes); + var br = new BinaryReader(stream); + BothUInt32 read = br.ReadDWORDBothEndian(); + Assert.Equal((uint)0x03020100, read.LittleEndian); + Assert.Equal((uint)0x04050607, read.BigEndian); + } + + [Fact] + public void ReadSingleTest() + { + var stream = new MemoryStream(_bytes); + var br = new BinaryReader(stream); + float expected = BitConverter.Int32BitsToSingle(0x03020100); + float read = br.ReadSingle(); + Assert.Equal(expected, read); + } + + [Fact] + public void ReadSingleBigEndianTest() + { + var stream = new MemoryStream(_bytes); + var br = new BinaryReader(stream); + float expected = BitConverter.Int32BitsToSingle(0x00010203); + float read = br.ReadSingleBigEndian(); + Assert.Equal(expected, read); + } + + [Fact] + public void ReadSingleLittleEndianTest() + { + var stream = new MemoryStream(_bytes); + var br = new BinaryReader(stream); + float expected = BitConverter.Int32BitsToSingle(0x03020100); + float read = br.ReadSingleLittleEndian(); + Assert.Equal(expected, read); + } + + [Fact] + public void ReadInt48Test() + { + var stream = new MemoryStream(_bytes); + var br = new BinaryReader(stream); + Int48 read = br.ReadInt48(); + Assert.Equal(0x050403020100, (long)read); + } + + [Fact] + public void ReadInt48BigEndianTest() + { + var stream = new MemoryStream(_bytes); + var br = new BinaryReader(stream); + Int48 read = br.ReadInt48BigEndian(); + Assert.Equal(0x000102030405, (long)read); + } + + [Fact] + public void ReadInt48LittleEndianTest() + { + var stream = new MemoryStream(_bytes); + var br = new BinaryReader(stream); + Int48 read = br.ReadInt48LittleEndian(); + Assert.Equal(0x050403020100, (long)read); + } + + [Fact] + public void ReadUInt48Test() + { + var stream = new MemoryStream(_bytes); + var br = new BinaryReader(stream); + UInt48 read = br.ReadUInt48(); + Assert.Equal((ulong)0x050403020100, (ulong)read); + } + + [Fact] + public void ReadUInt48BigEndianTest() + { + var stream = new MemoryStream(_bytes); + var br = new BinaryReader(stream); + UInt48 read = br.ReadUInt48BigEndian(); + Assert.Equal((ulong)0x000102030405, (ulong)read); + } + + [Fact] + public void ReadUInt48LittleEndianTest() + { + var stream = new MemoryStream(_bytes); + var br = new BinaryReader(stream); + UInt48 read = br.ReadUInt48LittleEndian(); + Assert.Equal((ulong)0x050403020100, (ulong)read); + } + + [Fact] + public void ReadInt64Test() + { + var stream = new MemoryStream(_bytes); + var br = new BinaryReader(stream); + long read = br.ReadInt64(); + Assert.Equal(0x0706050403020100, read); + } + + [Fact] + public void ReadInt64BigEndianTest() + { + var stream = new MemoryStream(_bytes); + var br = new BinaryReader(stream); + long read = br.ReadInt64BigEndian(); + Assert.Equal(0x0001020304050607, read); + } + + [Fact] + public void ReadInt64LittleEndianTest() + { + var stream = new MemoryStream(_bytes); + var br = new BinaryReader(stream); + long read = br.ReadInt64LittleEndian(); + Assert.Equal(0x0706050403020100, read); + } + + [Fact] + public void ReadInt64BothEndianTest() + { + var stream = new MemoryStream(_bytes); + var br = new BinaryReader(stream); + BothInt64 read = br.ReadInt64BothEndian(); + Assert.Equal(0x0706050403020100, read.LittleEndian); + Assert.Equal(0x08090A0B0C0D0E0F, read.BigEndian); + } + + [Fact] + public void ReadUInt64Test() + { + var stream = new MemoryStream(_bytes); + var br = new BinaryReader(stream); + ulong read = br.ReadUInt64(); + Assert.Equal((ulong)0x0706050403020100, read); + } + + [Fact] + public void ReadUInt64BigEndianTest() + { + var stream = new MemoryStream(_bytes); + var br = new BinaryReader(stream); + ulong read = br.ReadUInt64BigEndian(); + Assert.Equal((ulong)0x0001020304050607, read); + } + + [Fact] + public void ReadUInt64LittleEndianTest() + { + var stream = new MemoryStream(_bytes); + var br = new BinaryReader(stream); + ulong read = br.ReadUInt64LittleEndian(); + Assert.Equal((ulong)0x0706050403020100, read); + } + + [Fact] + public void ReadUInt64BothEndianTest() + { + var stream = new MemoryStream(_bytes); + var br = new BinaryReader(stream); + BothUInt64 read = br.ReadUInt64BothEndian(); + Assert.Equal((ulong)0x0706050403020100, read.LittleEndian); + Assert.Equal((ulong)0x08090A0B0C0D0E0F, read.BigEndian); + } + + [Fact] + public void ReadQWORDTest() + { + var stream = new MemoryStream(_bytes); + var br = new BinaryReader(stream); + ulong read = br.ReadQWORD(); + Assert.Equal((ulong)0x0706050403020100, read); + } + + [Fact] + public void ReadQWORDBigEndianTest() + { + var stream = new MemoryStream(_bytes); + var br = new BinaryReader(stream); + ulong read = br.ReadQWORDBigEndian(); + Assert.Equal((ulong)0x0001020304050607, read); + } + + [Fact] + public void ReadQWORDLittleEndianTest() + { + var stream = new MemoryStream(_bytes); + var br = new BinaryReader(stream); + ulong read = br.ReadQWORDLittleEndian(); + Assert.Equal((ulong)0x0706050403020100, read); + } + + [Fact] + public void ReadQWORDBothEndianTest() + { + var stream = new MemoryStream(_bytes); + var br = new BinaryReader(stream); + BothUInt64 read = br.ReadQWORDBothEndian(); + Assert.Equal((ulong)0x0706050403020100, read.LittleEndian); + Assert.Equal((ulong)0x08090A0B0C0D0E0F, read.BigEndian); + } + + [Fact] + public void ReadDoubleTest() + { + var stream = new MemoryStream(_bytes); + var br = new BinaryReader(stream); + double expected = BitConverter.Int64BitsToDouble(0x0706050403020100); + double read = br.ReadDouble(); + Assert.Equal(expected, read); + } + + [Fact] + public void ReadDoubleBigEndianTest() + { + var stream = new MemoryStream(_bytes); + var br = new BinaryReader(stream); + double expected = BitConverter.Int64BitsToDouble(0x0001020304050607); + double read = br.ReadDoubleBigEndian(); + Assert.Equal(expected, read); + } + + [Fact] + public void ReadDoubleLittleEndianTest() + { + var stream = new MemoryStream(_bytes); + var br = new BinaryReader(stream); + double expected = BitConverter.Int64BitsToDouble(0x0706050403020100); + double read = br.ReadDoubleLittleEndian(); + Assert.Equal(expected, read); + } + + [Fact] + public void ReadGuidTest() + { + var stream = new MemoryStream(_bytes); + var br = new BinaryReader(stream); + var expected = new Guid(_bytes); + Guid read = br.ReadGuid(); + Assert.Equal(expected, read); + } + + [Fact] + public void ReadGuidBigEndianTest() + { + var stream = new MemoryStream(_bytes); + var br = new BinaryReader(stream); + var expected = new Guid(_guidBigEndianbytes); + Guid read = br.ReadGuidBigEndian(); + Assert.Equal(expected, read); + } + + [Fact] + public void ReadGuidLittleEndianTest() + { + var stream = new MemoryStream(_bytes); + var br = new BinaryReader(stream); + var expected = new Guid(_bytes); + Guid read = br.ReadGuidLittleEndian(); + Assert.Equal(expected, read); + } + + [Fact] + public void ReadInt128Test() + { + var stream = new MemoryStream(_bytes); + var br = new BinaryReader(stream); + var expected = (Int128)new BigInteger(_bytes); + Int128 read = br.ReadInt128(); + Assert.Equal(expected, read); + } + + [Fact] + public void ReadInt128BigEndianTest() + { + var stream = new MemoryStream(_bytes); + var br = new BinaryReader(stream); + var reversed = Enumerable.Reverse(_bytes).ToArray(); + var expected = (Int128)new BigInteger(reversed); + Int128 read = br.ReadInt128BigEndian(); + Assert.Equal(expected, read); + } + + [Fact] + public void ReadInt128LittleEndianTest() + { + var stream = new MemoryStream(_bytes); + var br = new BinaryReader(stream); + var expected = (Int128)new BigInteger(_bytes); + Int128 read = br.ReadInt128LittleEndian(); + Assert.Equal(expected, read); + } + + [Fact] + public void ReadUInt128Test() + { + var stream = new MemoryStream(_bytes); + var br = new BinaryReader(stream); + var expected = (UInt128)new BigInteger(_bytes); + UInt128 read = br.ReadUInt128(); + Assert.Equal(expected, read); + } + + [Fact] + public void ReadUInt128BigEndianTest() + { + var stream = new MemoryStream(_bytes); + var br = new BinaryReader(stream); + var reversed = Enumerable.Reverse(_bytes).ToArray(); + var expected = (UInt128)new BigInteger(reversed); + UInt128 read = br.ReadUInt128BigEndian(); + Assert.Equal(expected, read); + } + + [Fact] + public void ReadUInt128LittleEndianTest() + { + var stream = new MemoryStream(_bytes); + var br = new BinaryReader(stream); + var expected = (UInt128)new BigInteger(_bytes); + UInt128 read = br.ReadUInt128LittleEndian(); + Assert.Equal(expected, read); + } + + [Fact] + public void ReadDecimalTest() + { + var stream = new MemoryStream(_decimalBytes); + var br = new BinaryReader(stream); + decimal expected = 0.0123456789M; + decimal read = br.ReadDecimal(); + Assert.Equal(expected, read); + } + + [Fact] + public void ReadDecimalBigEndianTest() + { + var stream = new MemoryStream([.. Enumerable.Reverse(_decimalBytes)]); + var br = new BinaryReader(stream); + decimal expected = 0.0123456789M; + decimal read = br.ReadDecimalBigEndian(); + Assert.Equal(expected, read); + } + + [Fact] + public void ReadDecimalLittleEndianTest() + { + var stream = new MemoryStream(_decimalBytes); + var br = new BinaryReader(stream); + decimal expected = 0.0123456789M; + decimal read = br.ReadDecimalLittleEndian(); + Assert.Equal(expected, read); + } + + #endregion + + #region Peek Read + + [Fact] + public void PeekByteTest() + { + var stream = new MemoryStream(_bytes); + var br = new BinaryReader(stream); + byte read = br.PeekByte(); + Assert.Equal(0x00, read); + Assert.Equal(0, br.BaseStream.Position); + } + + [Fact] + public void PeekByteBothEndianTest() + { + var stream = new MemoryStream(_bytes); + var br = new BinaryReader(stream); + BothUInt8 read = br.PeekByteBothEndian(); + Assert.Equal(0x00, read.LittleEndian); + Assert.Equal(0x01, read.BigEndian); + Assert.Equal(0, br.BaseStream.Position); + } + + [Fact] + public void PeekBytesTest() + { + var stream = new MemoryStream(_bytes); + var br = new BinaryReader(stream); + int length = 4; + byte[] read = br.PeekBytes(length); + Assert.Equal(length, read.Length); + Assert.True(read.SequenceEqual(_bytes.Take(length))); + Assert.Equal(0, br.BaseStream.Position); + } + + [Fact] + public void PeekSByteTest() + { + var stream = new MemoryStream(_bytes); + var br = new BinaryReader(stream); + sbyte read = br.PeekSByte(); + Assert.Equal(0x00, read); + } + + [Fact] + public void PeekSByteBothEndianTest() + { + var stream = new MemoryStream(_bytes); + var br = new BinaryReader(stream); + BothInt8 read = br.PeekSByteBothEndian(); + Assert.Equal(0x00, read.LittleEndian); + Assert.Equal(0x01, read.BigEndian); + Assert.Equal(0, br.BaseStream.Position); + } + + [Fact] + public void PeekInt16Test() + { + var stream = new MemoryStream(_bytes); + var br = new BinaryReader(stream); + short read = br.PeekInt16(); + Assert.Equal(0x0100, read); + Assert.Equal(0, br.BaseStream.Position); + } + + [Fact] + public void PeekInt16BigEndianTest() + { + var stream = new MemoryStream(_bytes); + var br = new BinaryReader(stream); + short read = br.PeekInt16BigEndian(); + Assert.Equal(0x0001, read); + Assert.Equal(0, br.BaseStream.Position); + } + + [Fact] + public void PeekInt16LittleEndianTest() + { + var stream = new MemoryStream(_bytes); + var br = new BinaryReader(stream); + short read = br.PeekInt16LittleEndian(); + Assert.Equal(0x0100, read); + Assert.Equal(0, br.BaseStream.Position); + } + + [Fact] + public void PeekInt16BothEndianTest() + { + var stream = new MemoryStream(_bytes); + var br = new BinaryReader(stream); + BothInt16 read = br.PeekInt16BothEndian(); + Assert.Equal(0x0100, read.LittleEndian); + Assert.Equal(0x0203, read.BigEndian); + Assert.Equal(0, br.BaseStream.Position); + } + + [Fact] + public void PeekUInt16Test() + { + var stream = new MemoryStream(_bytes); + var br = new BinaryReader(stream); + ushort read = br.PeekUInt16(); + Assert.Equal(0x0100, read); + Assert.Equal(0, br.BaseStream.Position); + } + + [Fact] + public void PeekUInt16BigEndianTest() + { + var stream = new MemoryStream(_bytes); + var br = new BinaryReader(stream); + ushort read = br.PeekUInt16BigEndian(); + Assert.Equal(0x0001, read); + Assert.Equal(0, br.BaseStream.Position); + } + + [Fact] + public void PeekUInt16LittleEndianTest() + { + var stream = new MemoryStream(_bytes); + var br = new BinaryReader(stream); + ushort read = br.PeekUInt16LittleEndian(); + Assert.Equal(0x0100, read); + Assert.Equal(0, br.BaseStream.Position); + } + + [Fact] + public void PeekUInt16BothEndianTest() + { + var stream = new MemoryStream(_bytes); + var br = new BinaryReader(stream); + BothUInt16 read = br.PeekUInt16BothEndian(); + Assert.Equal(0x0100, read.LittleEndian); + Assert.Equal(0x0203, read.BigEndian); + Assert.Equal(0, br.BaseStream.Position); + } + + [Fact] + public void PeekWORDTest() + { + var stream = new MemoryStream(_bytes); + var br = new BinaryReader(stream); + ushort read = br.PeekWORD(); + Assert.Equal(0x0100, read); + Assert.Equal(0, br.BaseStream.Position); + } + + [Fact] + public void PeekWORDBigEndianTest() + { + var stream = new MemoryStream(_bytes); + var br = new BinaryReader(stream); + ushort read = br.PeekWORDBigEndian(); + Assert.Equal(0x0001, read); + Assert.Equal(0, br.BaseStream.Position); + } + + [Fact] + public void PeekWORDLittleEndianTest() + { + var stream = new MemoryStream(_bytes); + var br = new BinaryReader(stream); + ushort read = br.PeekWORDLittleEndian(); + Assert.Equal(0x0100, read); + Assert.Equal(0, br.BaseStream.Position); + } + + [Fact] + public void PeekWORDBothEndianTest() + { + var stream = new MemoryStream(_bytes); + var br = new BinaryReader(stream); + BothUInt16 read = br.PeekWORDBothEndian(); + Assert.Equal(0x0100, read.LittleEndian); + Assert.Equal(0x0203, read.BigEndian); + Assert.Equal(0, br.BaseStream.Position); + } + + [Fact] + public void PeekHalfTest() + { + var stream = new MemoryStream(_bytes); + var br = new BinaryReader(stream); + Half expected = BitConverter.Int16BitsToHalf(0x0100); + Half read = br.PeekHalf(); + Assert.Equal(expected, read); + Assert.Equal(0, br.BaseStream.Position); + } + + [Fact] + public void PeekHalfBigEndianTest() + { + var stream = new MemoryStream(_bytes); + var br = new BinaryReader(stream); + Half expected = BitConverter.Int16BitsToHalf(0x0001); + Half read = br.PeekHalfBigEndian(); + Assert.Equal(expected, read); + Assert.Equal(0, br.BaseStream.Position); + } + + [Fact] + public void PeekHalfLittleEndianTest() + { + var stream = new MemoryStream(_bytes); + var br = new BinaryReader(stream); + Half expected = BitConverter.Int16BitsToHalf(0x0100); + Half read = br.PeekHalfLittleEndian(); + Assert.Equal(expected, read); + Assert.Equal(0, br.BaseStream.Position); + } + + [Fact] + public void PeekInt24Test() + { + var stream = new MemoryStream(_bytes); + var br = new BinaryReader(stream); + Int24 read = br.PeekInt24(); + Assert.Equal(0x020100, (int)read); + Assert.Equal(0, br.BaseStream.Position); + } + + [Fact] + public void PeekInt24BigEndianTest() + { + var stream = new MemoryStream(_bytes); + var br = new BinaryReader(stream); + Int24 read = br.PeekInt24BigEndian(); + Assert.Equal(0x000102, (int)read); + Assert.Equal(0, br.BaseStream.Position); + } + + [Fact] + public void PeekInt24LittleEndianTest() + { + var stream = new MemoryStream(_bytes); + var br = new BinaryReader(stream); + Int24 read = br.PeekInt24LittleEndian(); + Assert.Equal(0x020100, (int)read); + Assert.Equal(0, br.BaseStream.Position); + } + + [Fact] + public void PeekUInt24Test() + { + var stream = new MemoryStream(_bytes); + var br = new BinaryReader(stream); + UInt24 read = br.PeekUInt24(); + Assert.Equal((uint)0x020100, (uint)read); + Assert.Equal(0, br.BaseStream.Position); + } + + [Fact] + public void PeekUInt24BigEndianTest() + { + var stream = new MemoryStream(_bytes); + var br = new BinaryReader(stream); + UInt24 read = br.PeekUInt24BigEndian(); + Assert.Equal((uint)0x000102, (uint)read); + Assert.Equal(0, br.BaseStream.Position); + } + + [Fact] + public void PeekUInt24LittleEndianTest() + { + var stream = new MemoryStream(_bytes); + var br = new BinaryReader(stream); + UInt24 read = br.PeekUInt24LittleEndian(); + Assert.Equal((uint)0x020100, (uint)read); + Assert.Equal(0, br.BaseStream.Position); + } + + [Fact] + public void PeekInt32Test() + { + var stream = new MemoryStream(_bytes); + var br = new BinaryReader(stream); + int read = br.PeekInt32(); + Assert.Equal(0x03020100, read); + Assert.Equal(0, br.BaseStream.Position); + } + + [Fact] + public void PeekInt32BigEndianTest() + { + var stream = new MemoryStream(_bytes); + var br = new BinaryReader(stream); + int read = br.PeekInt32BigEndian(); + Assert.Equal(0x00010203, read); + Assert.Equal(0, br.BaseStream.Position); + } + + [Fact] + public void PeekInt32LittleEndianTest() + { + var stream = new MemoryStream(_bytes); + var br = new BinaryReader(stream); + int read = br.PeekInt32LittleEndian(); + Assert.Equal(0x03020100, read); + Assert.Equal(0, br.BaseStream.Position); + } + + [Fact] + public void PeekInt32BothEndianTest() + { + var stream = new MemoryStream(_bytes); + var br = new BinaryReader(stream); + BothInt32 read = br.PeekInt32BothEndian(); + Assert.Equal(0x03020100, read.LittleEndian); + Assert.Equal(0x04050607, read.BigEndian); + Assert.Equal(0, br.BaseStream.Position); + } + + [Fact] + public void PeekUInt32Test() + { + var stream = new MemoryStream(_bytes); + var br = new BinaryReader(stream); + uint read = br.PeekUInt32(); + Assert.Equal((uint)0x03020100, read); + Assert.Equal(0, br.BaseStream.Position); + } + + [Fact] + public void PeekUInt32BigEndianTest() + { + var stream = new MemoryStream(_bytes); + var br = new BinaryReader(stream); + uint read = br.PeekUInt32BigEndian(); + Assert.Equal((uint)0x00010203, read); + Assert.Equal(0, br.BaseStream.Position); + } + + [Fact] + public void PeekUInt32LittleEndianTest() + { + var stream = new MemoryStream(_bytes); + var br = new BinaryReader(stream); + uint read = br.PeekUInt32LittleEndian(); + Assert.Equal((uint)0x03020100, read); + Assert.Equal(0, br.BaseStream.Position); + } + + [Fact] + public void PeekUInt32BothEndianTest() + { + var stream = new MemoryStream(_bytes); + var br = new BinaryReader(stream); + BothUInt32 read = br.PeekUInt32BothEndian(); + Assert.Equal((uint)0x03020100, read.LittleEndian); + Assert.Equal((uint)0x04050607, read.BigEndian); + Assert.Equal(0, br.BaseStream.Position); + } + + [Fact] + public void PeekDWORDTest() + { + var stream = new MemoryStream(_bytes); + var br = new BinaryReader(stream); + uint read = br.PeekDWORD(); + Assert.Equal((uint)0x03020100, read); + Assert.Equal(0, br.BaseStream.Position); + } + + [Fact] + public void PeekDWORDBigEndianTest() + { + var stream = new MemoryStream(_bytes); + var br = new BinaryReader(stream); + uint read = br.PeekDWORDBigEndian(); + Assert.Equal((uint)0x00010203, read); + Assert.Equal(0, br.BaseStream.Position); + } + + [Fact] + public void PeekDWORDLittleEndianTest() + { + var stream = new MemoryStream(_bytes); + var br = new BinaryReader(stream); + uint read = br.PeekDWORDLittleEndian(); + Assert.Equal((uint)0x03020100, read); + Assert.Equal(0, br.BaseStream.Position); + } + + [Fact] + public void PeekDWORDBothEndianTest() + { + var stream = new MemoryStream(_bytes); + var br = new BinaryReader(stream); + BothUInt32 read = br.PeekDWORDBothEndian(); + Assert.Equal((uint)0x03020100, read.LittleEndian); + Assert.Equal((uint)0x04050607, read.BigEndian); + Assert.Equal(0, br.BaseStream.Position); + } + + [Fact] + public void PeekSingleTest() + { + var stream = new MemoryStream(_bytes); + var br = new BinaryReader(stream); + float expected = BitConverter.Int32BitsToSingle(0x03020100); + float read = br.PeekSingle(); + Assert.Equal(expected, read); + Assert.Equal(0, br.BaseStream.Position); + } + + [Fact] + public void PeekSingleBigEndianTest() + { + var stream = new MemoryStream(_bytes); + var br = new BinaryReader(stream); + float expected = BitConverter.Int32BitsToSingle(0x00010203); + float read = br.PeekSingleBigEndian(); + Assert.Equal(expected, read); + Assert.Equal(0, br.BaseStream.Position); + } + + [Fact] + public void PeekSingleLittleEndianTest() + { + var stream = new MemoryStream(_bytes); + var br = new BinaryReader(stream); + float expected = BitConverter.Int32BitsToSingle(0x03020100); + float read = br.PeekSingleLittleEndian(); + Assert.Equal(expected, read); + Assert.Equal(0, br.BaseStream.Position); + } + + [Fact] + public void PeekInt48Test() + { + var stream = new MemoryStream(_bytes); + var br = new BinaryReader(stream); + Int48 read = br.PeekInt48(); + Assert.Equal(0x050403020100, (long)read); + Assert.Equal(0, br.BaseStream.Position); + } + + [Fact] + public void PeekInt48BigEndianTest() + { + var stream = new MemoryStream(_bytes); + var br = new BinaryReader(stream); + Int48 read = br.PeekInt48BigEndian(); + Assert.Equal(0x000102030405, (long)read); + Assert.Equal(0, br.BaseStream.Position); + } + + [Fact] + public void PeekInt48LittleEndianTest() + { + var stream = new MemoryStream(_bytes); + var br = new BinaryReader(stream); + Int48 read = br.PeekInt48LittleEndian(); + Assert.Equal(0x050403020100, (long)read); + Assert.Equal(0, br.BaseStream.Position); + } + + [Fact] + public void PeekUInt48Test() + { + var stream = new MemoryStream(_bytes); + var br = new BinaryReader(stream); + UInt48 read = br.PeekUInt48(); + Assert.Equal((ulong)0x050403020100, (ulong)read); + Assert.Equal(0, br.BaseStream.Position); + } + + [Fact] + public void PeekUInt48BigEndianTest() + { + var stream = new MemoryStream(_bytes); + var br = new BinaryReader(stream); + UInt48 read = br.PeekUInt48BigEndian(); + Assert.Equal((ulong)0x000102030405, (ulong)read); + Assert.Equal(0, br.BaseStream.Position); + } + + [Fact] + public void PeekUInt48LittleEndianTest() + { + var stream = new MemoryStream(_bytes); + var br = new BinaryReader(stream); + UInt48 read = br.PeekUInt48LittleEndian(); + Assert.Equal((ulong)0x050403020100, (ulong)read); + Assert.Equal(0, br.BaseStream.Position); + } + + [Fact] + public void PeekInt64Test() + { + var stream = new MemoryStream(_bytes); + var br = new BinaryReader(stream); + long read = br.PeekInt64(); + Assert.Equal(0x0706050403020100, read); + Assert.Equal(0, br.BaseStream.Position); + } + + [Fact] + public void PeekInt64BigEndianTest() + { + var stream = new MemoryStream(_bytes); + var br = new BinaryReader(stream); + long read = br.PeekInt64BigEndian(); + Assert.Equal(0x0001020304050607, read); + Assert.Equal(0, br.BaseStream.Position); + } + + [Fact] + public void PeekInt64LittleEndianTest() + { + var stream = new MemoryStream(_bytes); + var br = new BinaryReader(stream); + long read = br.PeekInt64LittleEndian(); + Assert.Equal(0x0706050403020100, read); + Assert.Equal(0, br.BaseStream.Position); + } + + [Fact] + public void PeekInt64BothEndianTest() + { + var stream = new MemoryStream(_bytes); + var br = new BinaryReader(stream); + BothInt64 read = br.PeekInt64BothEndian(); + Assert.Equal(0x0706050403020100, read.LittleEndian); + Assert.Equal(0x08090A0B0C0D0E0F, read.BigEndian); + Assert.Equal(0, br.BaseStream.Position); + } + + [Fact] + public void PeekUInt64Test() + { + var stream = new MemoryStream(_bytes); + var br = new BinaryReader(stream); + ulong read = br.PeekUInt64(); + Assert.Equal((ulong)0x0706050403020100, read); + Assert.Equal(0, br.BaseStream.Position); + } + + [Fact] + public void PeekUInt64BigEndianTest() + { + var stream = new MemoryStream(_bytes); + var br = new BinaryReader(stream); + ulong read = br.PeekUInt64BigEndian(); + Assert.Equal((ulong)0x0001020304050607, read); + Assert.Equal(0, br.BaseStream.Position); + } + + [Fact] + public void PeekUInt64LittleEndianTest() + { + var stream = new MemoryStream(_bytes); + var br = new BinaryReader(stream); + ulong read = br.PeekUInt64LittleEndian(); + Assert.Equal((ulong)0x0706050403020100, read); + Assert.Equal(0, br.BaseStream.Position); + } + + [Fact] + public void PeekUInt64BothEndianTest() + { + var stream = new MemoryStream(_bytes); + var br = new BinaryReader(stream); + BothUInt64 read = br.PeekUInt64BothEndian(); + Assert.Equal((ulong)0x0706050403020100, read.LittleEndian); + Assert.Equal((ulong)0x08090A0B0C0D0E0F, read.BigEndian); + Assert.Equal(0, br.BaseStream.Position); + } + + [Fact] + public void PeekQWORDTest() + { + var stream = new MemoryStream(_bytes); + var br = new BinaryReader(stream); + ulong read = br.PeekQWORD(); + Assert.Equal((ulong)0x0706050403020100, read); + Assert.Equal(0, br.BaseStream.Position); + } + + [Fact] + public void PeekQWORDBigEndianTest() + { + var stream = new MemoryStream(_bytes); + var br = new BinaryReader(stream); + ulong read = br.PeekQWORDBigEndian(); + Assert.Equal((ulong)0x0001020304050607, read); + Assert.Equal(0, br.BaseStream.Position); + } + + [Fact] + public void PeekQWORDLittleEndianTest() + { + var stream = new MemoryStream(_bytes); + var br = new BinaryReader(stream); + ulong read = br.PeekQWORDLittleEndian(); + Assert.Equal((ulong)0x0706050403020100, read); + Assert.Equal(0, br.BaseStream.Position); + } + + [Fact] + public void PeekQWORDBothEndianTest() + { + var stream = new MemoryStream(_bytes); + var br = new BinaryReader(stream); + BothUInt64 read = br.PeekQWORDBothEndian(); + Assert.Equal((ulong)0x0706050403020100, read.LittleEndian); + Assert.Equal((ulong)0x08090A0B0C0D0E0F, read.BigEndian); + Assert.Equal(0, br.BaseStream.Position); + } + + [Fact] + public void PeekDoubleTest() + { + var stream = new MemoryStream(_bytes); + var br = new BinaryReader(stream); + double expected = BitConverter.Int64BitsToDouble(0x0706050403020100); + double read = br.PeekDouble(); + Assert.Equal(expected, read); + Assert.Equal(0, br.BaseStream.Position); + } + + [Fact] + public void PeekDoubleBigEndianTest() + { + var stream = new MemoryStream(_bytes); + var br = new BinaryReader(stream); + double expected = BitConverter.Int64BitsToDouble(0x0001020304050607); + double read = br.PeekDoubleBigEndian(); + Assert.Equal(expected, read); + Assert.Equal(0, br.BaseStream.Position); + } + + [Fact] + public void PeekDoubleLittleEndianTest() + { + var stream = new MemoryStream(_bytes); + var br = new BinaryReader(stream); + double expected = BitConverter.Int64BitsToDouble(0x0706050403020100); + double read = br.PeekDoubleLittleEndian(); + Assert.Equal(expected, read); + Assert.Equal(0, br.BaseStream.Position); + } + + [Fact] + public void PeekGuidTest() + { + var stream = new MemoryStream(_bytes); + var br = new BinaryReader(stream); + var expected = new Guid(_bytes); + Guid read = br.PeekGuid(); + Assert.Equal(expected, read); + Assert.Equal(0, br.BaseStream.Position); + } + + [Fact] + public void PeekGuidBigEndianTest() + { + var stream = new MemoryStream(_bytes); + var br = new BinaryReader(stream); + var expected = new Guid(_guidBigEndianbytes); + Guid read = br.PeekGuidBigEndian(); + Assert.Equal(expected, read); + Assert.Equal(0, br.BaseStream.Position); + } + + [Fact] + public void PeekGuidLittleEndianTest() + { + var stream = new MemoryStream(_bytes); + var br = new BinaryReader(stream); + var expected = new Guid(_bytes); + Guid read = br.PeekGuidLittleEndian(); + Assert.Equal(expected, read); + Assert.Equal(0, br.BaseStream.Position); + } + + [Fact] + public void PeekInt128Test() + { + var stream = new MemoryStream(_bytes); + var br = new BinaryReader(stream); + var expected = (Int128)new BigInteger(_bytes); + Int128 read = br.PeekInt128(); + Assert.Equal(expected, read); + Assert.Equal(0, br.BaseStream.Position); + } + + [Fact] + public void PeekInt128BigEndianTest() + { + var stream = new MemoryStream(_bytes); + var br = new BinaryReader(stream); + var reversed = Enumerable.Reverse(_bytes).ToArray(); + var expected = (Int128)new BigInteger(reversed); + Int128 read = br.PeekInt128BigEndian(); + Assert.Equal(expected, read); + Assert.Equal(0, br.BaseStream.Position); + } + + [Fact] + public void PeekInt128LittleEndianTest() + { + var stream = new MemoryStream(_bytes); + var br = new BinaryReader(stream); + var expected = (Int128)new BigInteger(_bytes); + Int128 read = br.PeekInt128LittleEndian(); + Assert.Equal(expected, read); + Assert.Equal(0, br.BaseStream.Position); + } + + [Fact] + public void PeekUInt128Test() + { + var stream = new MemoryStream(_bytes); + var br = new BinaryReader(stream); + var expected = (UInt128)new BigInteger(_bytes); + UInt128 read = br.PeekUInt128(); + Assert.Equal(expected, read); + Assert.Equal(0, br.BaseStream.Position); + } + + [Fact] + public void PeekUInt128BigEndianTest() + { + var stream = new MemoryStream(_bytes); + var br = new BinaryReader(stream); + var reversed = Enumerable.Reverse(_bytes).ToArray(); + var expected = (UInt128)new BigInteger(reversed); + UInt128 read = br.PeekUInt128BigEndian(); + Assert.Equal(expected, read); + Assert.Equal(0, br.BaseStream.Position); + } + + [Fact] + public void PeekUInt128LittleEndianTest() + { + var stream = new MemoryStream(_bytes); + var br = new BinaryReader(stream); + var expected = (UInt128)new BigInteger(_bytes); + UInt128 read = br.PeekUInt128LittleEndian(); + Assert.Equal(expected, read); + Assert.Equal(0, br.BaseStream.Position); + } + + [Fact] + public void PeekDecimalTest() + { + var stream = new MemoryStream(_decimalBytes); + var br = new BinaryReader(stream); + decimal expected = 0.0123456789M; + decimal read = br.PeekDecimal(); + Assert.Equal(expected, read); + Assert.Equal(0, br.BaseStream.Position); + } + + [Fact] + public void PeekDecimalBigEndianTest() + { + var stream = new MemoryStream([.. Enumerable.Reverse(_decimalBytes)]); + var br = new BinaryReader(stream); + decimal expected = 0.0123456789M; + decimal read = br.PeekDecimalBigEndian(); + Assert.Equal(expected, read); + Assert.Equal(0, br.BaseStream.Position); + } + + [Fact] + public void PeekDecimalLittleEndianTest() + { + var stream = new MemoryStream(_decimalBytes); + var br = new BinaryReader(stream); + decimal expected = 0.0123456789M; + decimal read = br.PeekDecimalLittleEndian(); + Assert.Equal(expected, read); + Assert.Equal(0, br.BaseStream.Position); + } + + #endregion + + #region Try Read + + [Fact] + public void TryReadByteArrayTest() + { + var stream = new MemoryStream([]); + var br = new BinaryReader(stream); + bool actual = br.TryReadBytes(4, out byte[] read); + Assert.False(actual); + Assert.Empty(read); + } + + [Fact] + public void TryReadByteTest() + { + var stream = new MemoryStream([]); + var br = new BinaryReader(stream); + bool actual = br.TryReadByteValue(out byte read); + Assert.False(actual); + Assert.Equal(default, read); + } + + [Fact] + public void TryReadByteBothEndianTest() + { + var stream = new MemoryStream([]); + var br = new BinaryReader(stream); + bool actual = br.TryReadByteBothEndian(out BothUInt8 read); + Assert.False(actual); + Assert.Equal(default, read.LittleEndian); + Assert.Equal(default, read.BigEndian); + } + + [Fact] + public void TryReadBytesTest() + { + var stream = new MemoryStream([]); + var br = new BinaryReader(stream); + int length = 4; + bool actual = br.TryReadBytes(length, out byte[] read); + Assert.False(actual); + Assert.Empty(read); + } + + [Fact] + public void TryReadSByteTest() + { + var stream = new MemoryStream([]); + var br = new BinaryReader(stream); + bool actual = br.TryReadSByte(out sbyte read); + Assert.False(actual); + Assert.Equal(default, read); + } + + [Fact] + public void TryReadSByteBothEndianTest() + { + var stream = new MemoryStream([]); + var br = new BinaryReader(stream); + bool actual = br.TryReadSByteBothEndian(out BothInt8 read); + Assert.False(actual); + Assert.Equal(default, read.LittleEndian); + Assert.Equal(default, read.BigEndian); + } + + [Fact] + public void TryReadCharTest() + { + var stream = new MemoryStream([]); + var br = new BinaryReader(stream); + bool actual = br.TryReadChar(out char read); + Assert.False(actual); + Assert.Equal(default, read); + } + + [Fact] + public void TryReadInt16Test() + { + var stream = new MemoryStream([]); + var br = new BinaryReader(stream); + bool actual = br.TryReadInt16(out short read); + Assert.False(actual); + Assert.Equal(default, read); + } + + [Fact] + public void TryReadInt16BigEndianTest() + { + var stream = new MemoryStream([]); + var br = new BinaryReader(stream); + bool actual = br.TryReadInt16BigEndian(out short read); + Assert.False(actual); + Assert.Equal(default, read); + } + + [Fact] + public void TryReadInt16LittleEndianTest() + { + var stream = new MemoryStream([]); + var br = new BinaryReader(stream); + bool actual = br.TryReadInt16LittleEndian(out short read); + Assert.False(actual); + Assert.Equal(default, read); + } + + [Fact] + public void TryReadInt16BothEndianTest() + { + var stream = new MemoryStream([]); + var br = new BinaryReader(stream); + bool actual = br.TryReadInt16BothEndian(out BothInt16 read); + Assert.False(actual); + Assert.Equal(default, read.LittleEndian); + Assert.Equal(default, read.BigEndian); + } + + [Fact] + public void TryReadUInt16Test() + { + var stream = new MemoryStream([]); + var br = new BinaryReader(stream); + bool actual = br.TryReadUInt16(out ushort read); + Assert.False(actual); + Assert.Equal(default, read); + } + + [Fact] + public void TryReadUInt16BigEndianTest() + { + var stream = new MemoryStream([]); + var br = new BinaryReader(stream); + bool actual = br.TryReadUInt16BigEndian(out ushort read); + Assert.False(actual); + Assert.Equal(default, read); + } + + [Fact] + public void TryReadUInt16LittleEndianTest() + { + var stream = new MemoryStream([]); + var br = new BinaryReader(stream); + bool actual = br.TryReadUInt16LittleEndian(out ushort read); + Assert.False(actual); + Assert.Equal(default, read); + } + + [Fact] + public void TryReadUInt16BothEndianTest() + { + var stream = new MemoryStream([]); + var br = new BinaryReader(stream); + bool actual = br.TryReadUInt16BothEndian(out BothUInt16 read); + Assert.False(actual); + Assert.Equal(default, read.LittleEndian); + Assert.Equal(default, read.BigEndian); + } + + [Fact] + public void TryReadWORDTest() + { + var stream = new MemoryStream([]); + var br = new BinaryReader(stream); + bool actual = br.TryReadWORD(out ushort read); + Assert.False(actual); + Assert.Equal(default, read); + } + + [Fact] + public void TryReadWORDBigEndianTest() + { + var stream = new MemoryStream([]); + var br = new BinaryReader(stream); + bool actual = br.TryReadWORDBigEndian(out ushort read); + Assert.False(actual); + Assert.Equal(default, read); + } + + [Fact] + public void TryReadWORDLittleEndianTest() + { + var stream = new MemoryStream([]); + var br = new BinaryReader(stream); + bool actual = br.TryReadWORDLittleEndian(out ushort read); + Assert.False(actual); + Assert.Equal(default, read); + } + + [Fact] + public void TryReadWORDBothEndianTest() + { + var stream = new MemoryStream([]); + var br = new BinaryReader(stream); + bool actual = br.TryReadWORDBothEndian(out BothUInt16 read); + Assert.False(actual); + Assert.Equal(default, read.LittleEndian); + Assert.Equal(default, read.BigEndian); + } + + [Fact] + public void TryReadHalfTest() + { + var stream = new MemoryStream([]); + var br = new BinaryReader(stream); + bool actual = br.TryReadHalf(out Half read); + Assert.False(actual); + Assert.Equal(default, read); + } + + [Fact] + public void TryReadHalfBigEndianTest() + { + var stream = new MemoryStream([]); + var br = new BinaryReader(stream); + bool actual = br.TryReadHalfBigEndian(out Half read); + Assert.False(actual); + Assert.Equal(default, read); + } + + [Fact] + public void TryReadHalfLittleEndianTest() + { + var stream = new MemoryStream([]); + var br = new BinaryReader(stream); + bool actual = br.TryReadHalfLittleEndian(out Half read); + Assert.False(actual); + Assert.Equal(default, read); + } + + [Fact] + public void TryReadInt24Test() + { + var stream = new MemoryStream([]); + var br = new BinaryReader(stream); + bool actual = br.TryReadInt24(out Int24 read); + Assert.False(actual); + Assert.Equal(default, (int)read); + } + + [Fact] + public void TryReadInt24BigEndianTest() + { + var stream = new MemoryStream([]); + var br = new BinaryReader(stream); + bool actual = br.TryReadInt24BigEndian(out Int24 read); + Assert.False(actual); + Assert.Equal(default, (int)read); + } + + [Fact] + public void TryReadInt24LittleEndianTest() + { + var stream = new MemoryStream([]); + var br = new BinaryReader(stream); + bool actual = br.TryReadInt24LittleEndian(out Int24 read); + Assert.False(actual); + Assert.Equal(default, (int)read); + } + + [Fact] + public void TryReadUInt24Test() + { + var stream = new MemoryStream([]); + var br = new BinaryReader(stream); + bool actual = br.TryReadUInt24(out UInt24 read); + Assert.False(actual); + Assert.Equal(default, (uint)read); + } + + [Fact] + public void TryReadUInt24BigEndianTest() + { + var stream = new MemoryStream([]); + var br = new BinaryReader(stream); + bool actual = br.TryReadUInt24BigEndian(out UInt24 read); + Assert.False(actual); + Assert.Equal(default, (uint)read); + } + + [Fact] + public void TryReadUInt24LittleEndianTest() + { + var stream = new MemoryStream([]); + var br = new BinaryReader(stream); + bool actual = br.TryReadUInt24LittleEndian(out UInt24 read); + Assert.False(actual); + Assert.Equal(default, (uint)read); + } + + [Fact] + public void TryReadInt32Test() + { + var stream = new MemoryStream([]); + var br = new BinaryReader(stream); + bool actual = br.TryReadInt32(out int read); + Assert.False(actual); + Assert.Equal(default, read); + } + + [Fact] + public void TryReadInt32BigEndianTest() + { + var stream = new MemoryStream([]); + var br = new BinaryReader(stream); + bool actual = br.TryReadInt32BigEndian(out int read); + Assert.False(actual); + Assert.Equal(default, read); + } + + [Fact] + public void TryReadInt32LittleEndianTest() + { + var stream = new MemoryStream([]); + var br = new BinaryReader(stream); + bool actual = br.TryReadInt32LittleEndian(out int read); + Assert.False(actual); + Assert.Equal(default, read); + } + + [Fact] + public void TryReadInt32BothEndianTest() + { + var stream = new MemoryStream([]); + var br = new BinaryReader(stream); + bool actual = br.TryReadInt32BothEndian(out BothInt32 read); + Assert.False(actual); + Assert.Equal(default, read.LittleEndian); + Assert.Equal(default, read.BigEndian); + } + + [Fact] + public void TryReadUInt32Test() + { + var stream = new MemoryStream([]); + var br = new BinaryReader(stream); + bool actual = br.TryReadUInt32(out uint read); + Assert.False(actual); + Assert.Equal(default, read); + } + + [Fact] + public void TryReadUInt32BigEndianTest() + { + var stream = new MemoryStream([]); + var br = new BinaryReader(stream); + bool actual = br.TryReadUInt32BigEndian(out uint read); + Assert.False(actual); + Assert.Equal(default, read); + } + + [Fact] + public void TryReadUInt32LittleEndianTest() + { + var stream = new MemoryStream([]); + var br = new BinaryReader(stream); + bool actual = br.TryReadUInt32LittleEndian(out uint read); + Assert.False(actual); + Assert.Equal(default, read); + } + + [Fact] + public void TryReadUInt32BothEndianTest() + { + var stream = new MemoryStream([]); + var br = new BinaryReader(stream); + bool actual = br.TryReadUInt32BothEndian(out BothUInt32 read); + Assert.False(actual); + Assert.Equal(default, read.LittleEndian); + Assert.Equal(default, read.BigEndian); + } + + [Fact] + public void TryReadDWORDTest() + { + var stream = new MemoryStream([]); + var br = new BinaryReader(stream); + bool actual = br.TryReadDWORD(out uint read); + Assert.False(actual); + Assert.Equal(default, read); + } + + [Fact] + public void TryReadDWORDBigEndianTest() + { + var stream = new MemoryStream([]); + var br = new BinaryReader(stream); + bool actual = br.TryReadDWORDBigEndian(out uint read); + Assert.False(actual); + Assert.Equal(default, read); + } + + [Fact] + public void TryReadDWORDLittleEndianTest() + { + var stream = new MemoryStream([]); + var br = new BinaryReader(stream); + bool actual = br.TryReadDWORDLittleEndian(out uint read); + Assert.False(actual); + Assert.Equal(default, read); + } + + [Fact] + public void TryReadDWORDBothEndianTest() + { + var stream = new MemoryStream([]); + var br = new BinaryReader(stream); + bool actual = br.TryReadDWORDBothEndian(out BothUInt32 read); + Assert.False(actual); + Assert.Equal(default, read.LittleEndian); + Assert.Equal(default, read.BigEndian); + } + + [Fact] + public void TryReadSingleTest() + { + var stream = new MemoryStream([]); + var br = new BinaryReader(stream); + bool actual = br.TryReadSingle(out float read); + Assert.False(actual); + Assert.Equal(default, read); + } + + [Fact] + public void TryReadSingleBigEndianTest() + { + var stream = new MemoryStream([]); + var br = new BinaryReader(stream); + bool actual = br.TryReadSingleBigEndian(out float read); + Assert.False(actual); + Assert.Equal(default, read); + } + + [Fact] + public void TryReadSingleLittleEndianTest() + { + var stream = new MemoryStream([]); + var br = new BinaryReader(stream); + bool actual = br.TryReadSingleLittleEndian(out float read); + Assert.False(actual); + Assert.Equal(default, read); + } + + [Fact] + public void TryReadInt48Test() + { + var stream = new MemoryStream([]); + var br = new BinaryReader(stream); + bool actual = br.TryReadInt48(out Int48 read); + Assert.False(actual); + Assert.Equal(default, (long)read); + } + + [Fact] + public void TryReadInt48BigEndianTest() + { + var stream = new MemoryStream([]); + var br = new BinaryReader(stream); + bool actual = br.TryReadInt48BigEndian(out Int48 read); + Assert.False(actual); + Assert.Equal(default, (long)read); + } + + [Fact] + public void TryReadInt48LittleEndianTest() + { + var stream = new MemoryStream([]); + var br = new BinaryReader(stream); + bool actual = br.TryReadInt48LittleEndian(out Int48 read); + Assert.False(actual); + Assert.Equal(default, (long)read); + } + + [Fact] + public void TryReadUInt48Test() + { + var stream = new MemoryStream([]); + var br = new BinaryReader(stream); + bool actual = br.TryReadUInt48(out UInt48 read); + Assert.False(actual); + Assert.Equal(default, (ulong)read); + } + + [Fact] + public void TryReadUInt48BigEndianTest() + { + var stream = new MemoryStream([]); + var br = new BinaryReader(stream); + bool actual = br.TryReadUInt48BigEndian(out UInt48 read); + Assert.False(actual); + Assert.Equal(default, (ulong)read); + } + + [Fact] + public void TryReadUInt48LittleEndianTest() + { + var stream = new MemoryStream([]); + var br = new BinaryReader(stream); + bool actual = br.TryReadUInt48LittleEndian(out UInt48 read); + Assert.False(actual); + Assert.Equal(default, (ulong)read); + } + + [Fact] + public void TryReadInt64Test() + { + var stream = new MemoryStream([]); + var br = new BinaryReader(stream); + bool actual = br.TryReadInt64(out long read); + Assert.False(actual); + Assert.Equal(default, read); + } + + [Fact] + public void TryReadInt64BigEndianTest() + { + var stream = new MemoryStream([]); + var br = new BinaryReader(stream); + bool actual = br.TryReadInt64BigEndian(out long read); + Assert.False(actual); + Assert.Equal(default, read); + } + + [Fact] + public void TryReadInt64LittleEndianTest() + { + var stream = new MemoryStream([]); + var br = new BinaryReader(stream); + bool actual = br.TryReadInt64LittleEndian(out long read); + Assert.False(actual); + Assert.Equal(default, read); + } + + [Fact] + public void TryReadInt64BothEndianTest() + { + var stream = new MemoryStream([]); + var br = new BinaryReader(stream); + bool actual = br.TryReadInt64BothEndian(out BothInt64 read); + Assert.False(actual); + Assert.Equal(default, read.LittleEndian); + Assert.Equal(default, read.BigEndian); + } + + [Fact] + public void TryReadUInt64Test() + { + var stream = new MemoryStream([]); + var br = new BinaryReader(stream); + bool actual = br.TryReadUInt64(out ulong read); + Assert.False(actual); + Assert.Equal(default, read); + } + + [Fact] + public void TryReadUInt64BigEndianTest() + { + var stream = new MemoryStream([]); + var br = new BinaryReader(stream); + bool actual = br.TryReadUInt64BigEndian(out ulong read); + Assert.False(actual); + Assert.Equal(default, read); + } + + [Fact] + public void TryReadUInt64LittleEndianTest() + { + var stream = new MemoryStream([]); + var br = new BinaryReader(stream); + bool actual = br.TryReadUInt64LittleEndian(out ulong read); + Assert.False(actual); + Assert.Equal(default, read); + } + + [Fact] + public void TryReadUInt64BothEndianTest() + { + var stream = new MemoryStream([]); + var br = new BinaryReader(stream); + bool actual = br.TryReadUInt64BothEndian(out BothUInt64 read); + Assert.False(actual); + Assert.Equal(default, read.LittleEndian); + Assert.Equal(default, read.BigEndian); + } + + [Fact] + public void TryReadQWORDTest() + { + var stream = new MemoryStream([]); + var br = new BinaryReader(stream); + bool actual = br.TryReadQWORD(out ulong read); + Assert.False(actual); + Assert.Equal(default, read); + } + + [Fact] + public void TryReadQWORDBigEndianTest() + { + var stream = new MemoryStream([]); + var br = new BinaryReader(stream); + bool actual = br.TryReadQWORDBigEndian(out ulong read); + Assert.False(actual); + Assert.Equal(default, read); + } + + [Fact] + public void TryReadQWORDLittleEndianTest() + { + var stream = new MemoryStream([]); + var br = new BinaryReader(stream); + bool actual = br.TryReadQWORDLittleEndian(out ulong read); + Assert.False(actual); + Assert.Equal(default, read); + } + + [Fact] + public void TryReadQWORDBothEndianTest() + { + var stream = new MemoryStream([]); + var br = new BinaryReader(stream); + bool actual = br.TryReadQWORDBothEndian(out BothUInt64 read); + Assert.False(actual); + Assert.Equal(default, read.LittleEndian); + Assert.Equal(default, read.BigEndian); + } + + [Fact] + public void TryReadDoubleTest() + { + var stream = new MemoryStream([]); + var br = new BinaryReader(stream); + bool actual = br.TryReadDouble(out double read); + Assert.False(actual); + Assert.Equal(default, read); + } + + [Fact] + public void TryReadDoubleBigEndianTest() + { + var stream = new MemoryStream([]); + var br = new BinaryReader(stream); + bool actual = br.TryReadDoubleBigEndian(out double read); + Assert.False(actual); + Assert.Equal(default, read); + } + + [Fact] + public void TryReadDoubleLittleEndianTest() + { + var stream = new MemoryStream([]); + var br = new BinaryReader(stream); + bool actual = br.TryReadDoubleLittleEndian(out double read); + Assert.False(actual); + Assert.Equal(default, read); + } + + [Fact] + public void TryReadGuidTest() + { + var stream = new MemoryStream([]); + var br = new BinaryReader(stream); + bool actual = br.TryReadGuid(out Guid read); + Assert.False(actual); + Assert.Equal(default, read); + } + + [Fact] + public void TryReadGuidBigEndianTest() + { + var stream = new MemoryStream([]); + var br = new BinaryReader(stream); + bool actual = br.TryReadGuidBigEndian(out Guid read); + Assert.False(actual); + Assert.Equal(default, read); + } + + [Fact] + public void TryReadGuidLittleEndianTest() + { + var stream = new MemoryStream([]); + var br = new BinaryReader(stream); + bool actual = br.TryReadGuidLittleEndian(out Guid read); + Assert.False(actual); + Assert.Equal(default, read); + } + + [Fact] + public void TryReadInt128Test() + { + var stream = new MemoryStream([]); + var br = new BinaryReader(stream); + bool actual = br.TryReadInt128(out Int128 read); + Assert.False(actual); + Assert.Equal(default, read); + } + + [Fact] + public void TryReadInt128BigEndianTest() + { + var stream = new MemoryStream([]); + var br = new BinaryReader(stream); + bool actual = br.TryReadInt128BigEndian(out Int128 read); + Assert.False(actual); + Assert.Equal(default, read); + } + + [Fact] + public void TryReadInt128LittleEndianTest() + { + var stream = new MemoryStream([]); + var br = new BinaryReader(stream); + bool actual = br.TryReadInt128LittleEndian(out Int128 read); + Assert.False(actual); + Assert.Equal(default, read); + } + + [Fact] + public void TryReadUInt128Test() + { + var stream = new MemoryStream([]); + var br = new BinaryReader(stream); + bool actual = br.TryReadUInt128(out UInt128 read); + Assert.False(actual); + Assert.Equal(default, read); + } + + [Fact] + public void TryReadUInt128BigEndianTest() + { + var stream = new MemoryStream([]); + var br = new BinaryReader(stream); + bool actual = br.TryReadUInt128BigEndian(out UInt128 read); + Assert.False(actual); + Assert.Equal(default, read); + } + + [Fact] + public void TryReadUInt128LittleEndianTest() + { + var stream = new MemoryStream([]); + var br = new BinaryReader(stream); + bool actual = br.TryReadUInt128LittleEndian(out UInt128 read); + Assert.False(actual); + Assert.Equal(default, read); + } + + [Fact] + public void TryReadDecimalTest() + { + var stream = new MemoryStream([]); + var br = new BinaryReader(stream); + bool actual = br.TryReadDecimal(out decimal read); + Assert.False(actual); + Assert.Equal(default, read); + } + + [Fact] + public void TryReadDecimalBigEndianTest() + { + var stream = new MemoryStream([]); + var br = new BinaryReader(stream); + bool actual = br.TryReadDecimalBigEndian(out decimal read); + Assert.False(actual); + Assert.Equal(default, read); + } + + [Fact] + public void TryReadDecimalLittleEndianTest() + { + var stream = new MemoryStream([]); + var br = new BinaryReader(stream); + bool actual = br.TryReadDecimalLittleEndian(out decimal read); + Assert.False(actual); + Assert.Equal(default, read); + } + + #endregion + } +} diff --git a/SabreTools.Numerics.Extensions.Test/BinaryWriterExtensionsTests.cs b/SabreTools.Numerics.Extensions.Test/BinaryWriterExtensionsTests.cs new file mode 100644 index 0000000..36a4cbf --- /dev/null +++ b/SabreTools.Numerics.Extensions.Test/BinaryWriterExtensionsTests.cs @@ -0,0 +1,742 @@ +using System; +using System.IO; +using System.Linq; +using System.Numerics; +using System.Text; +using Xunit; + +namespace SabreTools.Numerics.Extensions.Test +{ + public class BinaryWriterExtensionsTests + { + /// + /// Test pattern from 0x00-0x0F + /// + private static readonly byte[] _bytes = + [ + 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, + 0x08, 0x09, 0x0A, 0x0B, 0x0C, 0x0D, 0x0E, 0x0F, + ]; + + /// + /// Represents the decimal value 0.0123456789 + /// + private static readonly byte[] _decimalBytes = + [ + 0x15, 0xCD, 0x5B, 0x07, 0x00, 0x00, 0x00, 0x00, + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x0A, 0x00, + ]; + + /// + /// Test pattern for big-endian GUID created from + /// + private static readonly byte[] _guidBigEndianbytes = + [ + 0x03, 0x02, 0x01, 0x00, 0x05, 0x04, 0x07, 0x06, + 0x08, 0x09, 0x0A, 0x0B, 0x0C, 0x0D, 0x0E, 0x0F, + ]; + + [Fact] + public void WriteByteValueTest() + { + var stream = new MemoryStream(new byte[16], 0, 16, true, true); + var bw = new BinaryWriter(stream); + byte[] expected = [.. _bytes.Take(1)]; + bw.Write((byte)0x00); + ValidateBytes(expected, stream.GetBuffer()); + } + + [Fact] + public void WriteByteBothEndianTest() + { + var stream = new MemoryStream(new byte[16], 0, 16, true, true); + var bw = new BinaryWriter(stream); + byte[] expected = [.. _bytes.Take(2)]; + + int offset = 0; + bw.WriteBothEndian(_bytes.ReadByteBothEndian(ref offset)); + ValidateBytes(expected, stream.GetBuffer()); + } + + [Fact] + public void WriteBytesTest() + { + var stream = new MemoryStream(new byte[16], 0, 16, true, true); + var bw = new BinaryWriter(stream); + byte[] expected = [.. _bytes.Take(4)]; + bw.Write([0x00, 0x01, 0x02, 0x03]); + ValidateBytes(expected, stream.GetBuffer()); + } + + [Fact] + public void WriteBytesBigEndianTest() + { + var stream = new MemoryStream(new byte[16], 0, 16, true, true); + var bw = new BinaryWriter(stream); + byte[] expected = [.. _bytes.Take(4)]; + bw.WriteBigEndian([0x03, 0x02, 0x01, 0x00]); + ValidateBytes(expected, stream.GetBuffer()); + } + + [Fact] + public void WriteSByteTest() + { + var stream = new MemoryStream(new byte[16], 0, 16, true, true); + var bw = new BinaryWriter(stream); + byte[] expected = [.. _bytes.Take(1)]; + bw.Write((sbyte)0x00); + ValidateBytes(expected, stream.GetBuffer()); + } + + [Fact] + public void WriteSByteBothEndianTest() + { + var stream = new MemoryStream(new byte[16], 0, 16, true, true); + var bw = new BinaryWriter(stream); + byte[] expected = [.. _bytes.Take(2)]; + + int offset = 0; + bw.WriteBothEndian(_bytes.ReadSByteBothEndian(ref offset)); + ValidateBytes(expected, stream.GetBuffer()); + } + + [Fact] + public void WriteCharTest() + { + var stream = new MemoryStream(new byte[16], 0, 16, true, true); + var bw = new BinaryWriter(stream); + byte[] expected = [.. _bytes.Take(1)]; + bw.Write('\0'); + ValidateBytes(expected, stream.GetBuffer()); + } + + [Fact] + public void WriteCharEncodingTest() + { + var stream = new MemoryStream(new byte[16], 0, 16, true, true); + var bw = new BinaryWriter(stream); + byte[] expected = [0x00, 0x00]; + bw.Write('\0', Encoding.Unicode); + ValidateBytes(expected, stream.GetBuffer()); + } + + [Fact] + public void WriteInt16Test() + { + var stream = new MemoryStream(new byte[16], 0, 16, true, true); + var bw = new BinaryWriter(stream); + byte[] expected = [.. _bytes.Take(2)]; + bw.Write((short)0x0100); + ValidateBytes(expected, stream.GetBuffer()); + } + + [Fact] + public void WriteInt16BigEndianTest() + { + var stream = new MemoryStream(new byte[16], 0, 16, true, true); + var bw = new BinaryWriter(stream); + byte[] expected = [.. _bytes.Take(2)]; + bool write = bw.WriteBigEndian((short)0x0001); + Assert.True(write); + ValidateBytes(expected, stream.GetBuffer()); + } + + [Fact] + public void WriteInt16LittleEndianTest() + { + var stream = new MemoryStream(new byte[16], 0, 16, true, true); + var bw = new BinaryWriter(stream); + byte[] expected = [.. _bytes.Take(2)]; + bw.WriteLittleEndian((short)0x0100); + ValidateBytes(expected, stream.GetBuffer()); + } + + [Fact] + public void WriteInt16BothEndianTest() + { + var stream = new MemoryStream(new byte[16], 0, 16, true, true); + var bw = new BinaryWriter(stream); + byte[] expected = [.. _bytes.Take(4)]; + + int offset = 0; + bw.WriteBothEndian(_bytes.ReadInt16BothEndian(ref offset)); + ValidateBytes(expected, stream.GetBuffer()); + } + + [Fact] + public void WriteUInt16Test() + { + var stream = new MemoryStream(new byte[16], 0, 16, true, true); + var bw = new BinaryWriter(stream); + byte[] expected = [.. _bytes.Take(2)]; + bw.Write((ushort)0x0100); + ValidateBytes(expected, stream.GetBuffer()); + } + + [Fact] + public void WriteUInt16BigEndianTest() + { + var stream = new MemoryStream(new byte[16], 0, 16, true, true); + var bw = new BinaryWriter(stream); + byte[] expected = [.. _bytes.Take(2)]; + bool write = bw.WriteBigEndian((ushort)0x0001); + Assert.True(write); + ValidateBytes(expected, stream.GetBuffer()); + } + + [Fact] + public void WriteUInt16LittleEndianTest() + { + var stream = new MemoryStream(new byte[16], 0, 16, true, true); + var bw = new BinaryWriter(stream); + byte[] expected = [.. _bytes.Take(2)]; + bw.WriteLittleEndian((ushort)0x0100); + ValidateBytes(expected, stream.GetBuffer()); + } + + [Fact] + public void WriteUInt16BothEndianTest() + { + var stream = new MemoryStream(new byte[16], 0, 16, true, true); + var bw = new BinaryWriter(stream); + byte[] expected = [.. _bytes.Take(4)]; + + int offset = 0; + bw.WriteBothEndian(_bytes.ReadUInt16BothEndian(ref offset)); + ValidateBytes(expected, stream.GetBuffer()); + } + + [Fact] + public void WriteHalfTest() + { + var stream = new MemoryStream(new byte[16], 0, 16, true, true); + var bw = new BinaryWriter(stream); + byte[] expected = [.. _bytes.Take(2)]; + bw.Write(BitConverter.Int16BitsToHalf(0x0100)); + ValidateBytes(expected, stream.GetBuffer()); + } + + [Fact] + public void WriteHalfBigEndianTest() + { + var stream = new MemoryStream(new byte[16], 0, 16, true, true); + var bw = new BinaryWriter(stream); + byte[] expected = [.. _bytes.Take(2)]; + bool write = bw.WriteBigEndian(BitConverter.Int16BitsToHalf(0x0001)); + Assert.True(write); + ValidateBytes(expected, stream.GetBuffer()); + } + + [Fact] + public void WriteHalfLittleEndianTest() + { + var stream = new MemoryStream(new byte[16], 0, 16, true, true); + var bw = new BinaryWriter(stream); + byte[] expected = [.. _bytes.Take(2)]; + bw.WriteLittleEndian(BitConverter.Int16BitsToHalf(0x0100)); + ValidateBytes(expected, stream.GetBuffer()); + } + + [Fact] + public void WriteInt24Test() + { + var stream = new MemoryStream(new byte[16], 0, 16, true, true); + var bw = new BinaryWriter(stream); + byte[] expected = [.. _bytes.Take(3)]; + bw.Write((Int24)0x020100); + ValidateBytes(expected, stream.GetBuffer()); + } + + [Fact] + public void WriteInt24BigEndianTest() + { + var stream = new MemoryStream(new byte[16], 0, 16, true, true); + var bw = new BinaryWriter(stream); + byte[] expected = [.. _bytes.Take(3)]; + bool write = bw.WriteBigEndian((Int24)0x000102); + Assert.True(write); + ValidateBytes(expected, stream.GetBuffer()); + } + + [Fact] + public void WriteInt24LittleEndianTest() + { + var stream = new MemoryStream(new byte[16], 0, 16, true, true); + var bw = new BinaryWriter(stream); + byte[] expected = [.. _bytes.Take(3)]; + bw.WriteLittleEndian((Int24)0x020100); + ValidateBytes(expected, stream.GetBuffer()); + } + + [Fact] + public void WriteUInt24Test() + { + var stream = new MemoryStream(new byte[16], 0, 16, true, true); + var bw = new BinaryWriter(stream); + byte[] expected = [.. _bytes.Take(3)]; + bw.Write((UInt24)0x020100); + ValidateBytes(expected, stream.GetBuffer()); + } + + [Fact] + public void WriteUInt24BigEndianTest() + { + var stream = new MemoryStream(new byte[16], 0, 16, true, true); + var bw = new BinaryWriter(stream); + byte[] expected = [.. _bytes.Take(3)]; + bool write = bw.WriteBigEndian((UInt24)0x000102); + Assert.True(write); + ValidateBytes(expected, stream.GetBuffer()); + } + + [Fact] + public void WriteUInt24LittleEndianTest() + { + var stream = new MemoryStream(new byte[16], 0, 16, true, true); + var bw = new BinaryWriter(stream); + byte[] expected = [.. _bytes.Take(3)]; + bw.WriteLittleEndian((UInt24)0x020100); + ValidateBytes(expected, stream.GetBuffer()); + } + + [Fact] + public void WriteInt32Test() + { + var stream = new MemoryStream(new byte[16], 0, 16, true, true); + var bw = new BinaryWriter(stream); + byte[] expected = [.. _bytes.Take(4)]; + bw.Write(0x03020100); + ValidateBytes(expected, stream.GetBuffer()); + } + + [Fact] + public void WriteInt32BigEndianTest() + { + var stream = new MemoryStream(new byte[16], 0, 16, true, true); + var bw = new BinaryWriter(stream); + byte[] expected = [.. _bytes.Take(4)]; + bool write = bw.WriteBigEndian(0x00010203); + Assert.True(write); + ValidateBytes(expected, stream.GetBuffer()); + } + + [Fact] + public void WriteInt32LittleEndianTest() + { + var stream = new MemoryStream(new byte[16], 0, 16, true, true); + var bw = new BinaryWriter(stream); + byte[] expected = [.. _bytes.Take(4)]; + bw.WriteLittleEndian(0x03020100); + ValidateBytes(expected, stream.GetBuffer()); + } + + [Fact] + public void WriteInt32BothEndianTest() + { + var stream = new MemoryStream(new byte[16], 0, 16, true, true); + var bw = new BinaryWriter(stream); + byte[] expected = [.. _bytes.Take(8)]; + + int offset = 0; + bw.WriteBothEndian(_bytes.ReadInt32BothEndian(ref offset)); + ValidateBytes(expected, stream.GetBuffer()); + } + + [Fact] + public void WriteUInt32Test() + { + var stream = new MemoryStream(new byte[16], 0, 16, true, true); + var bw = new BinaryWriter(stream); + byte[] expected = [.. _bytes.Take(4)]; + bw.Write((uint)0x03020100); + ValidateBytes(expected, stream.GetBuffer()); + } + + [Fact] + public void WriteUInt32BigEndianTest() + { + var stream = new MemoryStream(new byte[16], 0, 16, true, true); + var bw = new BinaryWriter(stream); + byte[] expected = [.. _bytes.Take(4)]; + bool write = bw.WriteBigEndian((uint)0x00010203); + Assert.True(write); + ValidateBytes(expected, stream.GetBuffer()); + } + + [Fact] + public void WriteUInt32LittleEndianTest() + { + var stream = new MemoryStream(new byte[16], 0, 16, true, true); + var bw = new BinaryWriter(stream); + byte[] expected = [.. _bytes.Take(4)]; + bw.WriteLittleEndian((uint)0x03020100); + ValidateBytes(expected, stream.GetBuffer()); + } + + [Fact] + public void WriteUInt32BothEndianTest() + { + var stream = new MemoryStream(new byte[16], 0, 16, true, true); + var bw = new BinaryWriter(stream); + byte[] expected = [.. _bytes.Take(8)]; + + int offset = 0; + bw.WriteBothEndian(_bytes.ReadUInt32BothEndian(ref offset)); + ValidateBytes(expected, stream.GetBuffer()); + } + + [Fact] + public void WriteSingleTest() + { + var stream = new MemoryStream(new byte[16], 0, 16, true, true); + var bw = new BinaryWriter(stream); + byte[] expected = [.. _bytes.Take(4)]; + bw.Write(BitConverter.Int32BitsToSingle(0x03020100)); + ValidateBytes(expected, stream.GetBuffer()); + } + + [Fact] + public void WriteSingleBigEndianTest() + { + var stream = new MemoryStream(new byte[16], 0, 16, true, true); + var bw = new BinaryWriter(stream); + byte[] expected = [.. _bytes.Take(4)]; + bool write = bw.WriteBigEndian(BitConverter.Int32BitsToSingle(0x00010203)); + Assert.True(write); + ValidateBytes(expected, stream.GetBuffer()); + } + + [Fact] + public void WriteSingleLittleEndianTest() + { + var stream = new MemoryStream(new byte[16], 0, 16, true, true); + var bw = new BinaryWriter(stream); + byte[] expected = [.. _bytes.Take(4)]; + bool write = bw.WriteLittleEndian(BitConverter.Int32BitsToSingle(0x03020100)); + Assert.True(write); + ValidateBytes(expected, stream.GetBuffer()); + } + + [Fact] + public void WriteInt48Test() + { + var stream = new MemoryStream(new byte[16], 0, 16, true, true); + var bw = new BinaryWriter(stream); + byte[] expected = [.. _bytes.Take(6)]; + bw.Write((Int48)0x050403020100); + ValidateBytes(expected, stream.GetBuffer()); + } + + [Fact] + public void WriteInt48BigEndianTest() + { + var stream = new MemoryStream(new byte[16], 0, 16, true, true); + var bw = new BinaryWriter(stream); + byte[] expected = [.. _bytes.Take(6)]; + bool write = bw.WriteBigEndian((Int48)0x000102030405); + Assert.True(write); + ValidateBytes(expected, stream.GetBuffer()); + } + + [Fact] + public void WriteInt48LittleEndianTest() + { + var stream = new MemoryStream(new byte[16], 0, 16, true, true); + var bw = new BinaryWriter(stream); + byte[] expected = [.. _bytes.Take(6)]; + bw.WriteLittleEndian((Int48)0x050403020100); + ValidateBytes(expected, stream.GetBuffer()); + } + + [Fact] + public void WriteUInt48Test() + { + var stream = new MemoryStream(new byte[16], 0, 16, true, true); + var bw = new BinaryWriter(stream); + byte[] expected = [.. _bytes.Take(6)]; + bw.Write((UInt48)0x050403020100); + ValidateBytes(expected, stream.GetBuffer()); + } + + [Fact] + public void WriteUInt48BigEndianTest() + { + var stream = new MemoryStream(new byte[16], 0, 16, true, true); + var bw = new BinaryWriter(stream); + byte[] expected = [.. _bytes.Take(6)]; + bool write = bw.WriteBigEndian((UInt48)0x000102030405); + Assert.True(write); + ValidateBytes(expected, stream.GetBuffer()); + } + + [Fact] + public void WriteUInt48LittleEndianTest() + { + var stream = new MemoryStream(new byte[16], 0, 16, true, true); + var bw = new BinaryWriter(stream); + byte[] expected = [.. _bytes.Take(6)]; + bw.WriteLittleEndian((UInt48)0x050403020100); + ValidateBytes(expected, stream.GetBuffer()); + } + + [Fact] + public void WriteInt64Test() + { + var stream = new MemoryStream(new byte[16], 0, 16, true, true); + var bw = new BinaryWriter(stream); + byte[] expected = [.. _bytes.Take(8)]; + bw.Write(0x0706050403020100); + ValidateBytes(expected, stream.GetBuffer()); + } + + [Fact] + public void WriteInt64BigEndianTest() + { + var stream = new MemoryStream(new byte[16], 0, 16, true, true); + var bw = new BinaryWriter(stream); + byte[] expected = [.. _bytes.Take(8)]; + bool write = bw.WriteBigEndian(0x0001020304050607); + Assert.True(write); + ValidateBytes(expected, stream.GetBuffer()); + } + + [Fact] + public void WriteInt64LittleEndianTest() + { + var stream = new MemoryStream(new byte[16], 0, 16, true, true); + var bw = new BinaryWriter(stream); + byte[] expected = [.. _bytes.Take(8)]; + bw.WriteLittleEndian(0x0706050403020100); + ValidateBytes(expected, stream.GetBuffer()); + } + + [Fact] + public void WriteInt64BothEndianTest() + { + var stream = new MemoryStream(new byte[16], 0, 16, true, true); + var bw = new BinaryWriter(stream); + byte[] expected = [.. _bytes.Take(16)]; + + int offset = 0; + bw.WriteBothEndian(_bytes.ReadInt64BothEndian(ref offset)); + ValidateBytes(expected, stream.GetBuffer()); + } + + [Fact] + public void WriteUInt64Test() + { + var stream = new MemoryStream(new byte[16], 0, 16, true, true); + var bw = new BinaryWriter(stream); + byte[] expected = [.. _bytes.Take(8)]; + bw.Write((ulong)0x0706050403020100); + ValidateBytes(expected, stream.GetBuffer()); + } + + [Fact] + public void WriteUInt64BigEndianTest() + { + var stream = new MemoryStream(new byte[16], 0, 16, true, true); + var bw = new BinaryWriter(stream); + byte[] expected = [.. _bytes.Take(8)]; + bool write = bw.WriteBigEndian((ulong)0x0001020304050607); + Assert.True(write); + ValidateBytes(expected, stream.GetBuffer()); + } + + [Fact] + public void WriteUInt64LittleEndianTest() + { + var stream = new MemoryStream(new byte[16], 0, 16, true, true); + var bw = new BinaryWriter(stream); + byte[] expected = [.. _bytes.Take(8)]; + bw.WriteLittleEndian((ulong)0x0706050403020100); + ValidateBytes(expected, stream.GetBuffer()); + } + + [Fact] + public void WriteUInt64BothEndianTest() + { + var stream = new MemoryStream(new byte[16], 0, 16, true, true); + var bw = new BinaryWriter(stream); + byte[] expected = [.. _bytes.Take(16)]; + + int offset = 0; + bw.WriteBothEndian(_bytes.ReadUInt64BothEndian(ref offset)); + ValidateBytes(expected, stream.GetBuffer()); + } + + [Fact] + public void WriteDoubleTest() + { + var stream = new MemoryStream(new byte[16], 0, 16, true, true); + var bw = new BinaryWriter(stream); + byte[] expected = [.. _bytes.Take(8)]; + bw.Write(BitConverter.Int64BitsToDouble(0x0706050403020100)); + ValidateBytes(expected, stream.GetBuffer()); + } + + [Fact] + public void WriteDoubleBigEndianTest() + { + var stream = new MemoryStream(new byte[16], 0, 16, true, true); + var bw = new BinaryWriter(stream); + byte[] expected = [.. _bytes.Take(8)]; + bool write = bw.WriteBigEndian(BitConverter.Int64BitsToDouble(0x0001020304050607)); + Assert.True(write); + ValidateBytes(expected, stream.GetBuffer()); + } + + [Fact] + public void WriteDoubleLittleEndianTest() + { + var stream = new MemoryStream(new byte[16], 0, 16, true, true); + var bw = new BinaryWriter(stream); + byte[] expected = [.. _bytes.Take(8)]; + bool write = bw.WriteLittleEndian(BitConverter.Int64BitsToDouble(0x0706050403020100)); + Assert.True(write); + ValidateBytes(expected, stream.GetBuffer()); + } + + [Fact] + public void WriteGuidTest() + { + var stream = new MemoryStream(new byte[16], 0, 16, true, true); + var bw = new BinaryWriter(stream); + byte[] expected = [.. _bytes.Take(16)]; + bool write = bw.Write(new Guid(_bytes)); + Assert.True(write); + ValidateBytes(expected, stream.GetBuffer()); + } + + [Fact] + public void WriteGuidBigEndianTest() + { + var stream = new MemoryStream(new byte[16], 0, 16, true, true); + var bw = new BinaryWriter(stream); + byte[] expected = [.. _guidBigEndianbytes.Take(16)]; + bool write = bw.WriteBigEndian(new Guid(_bytes)); + Assert.True(write); + ValidateBytes(expected, stream.GetBuffer()); + } + + [Fact] + public void WriteGuidLittleEndianTest() + { + var stream = new MemoryStream(new byte[16], 0, 16, true, true); + var bw = new BinaryWriter(stream); + byte[] expected = [.. _bytes.Take(16)]; + bool write = bw.WriteLittleEndian(new Guid(_bytes)); + Assert.True(write); + ValidateBytes(expected, stream.GetBuffer()); + } + + [Fact] + public void WriteInt128Test() + { + var stream = new MemoryStream(new byte[16], 0, 16, true, true); + var bw = new BinaryWriter(stream); + byte[] expected = [.. _bytes.Take(16)]; + bool write = bw.Write((Int128)new BigInteger(_bytes)); + Assert.True(write); + ValidateBytes(expected, stream.GetBuffer()); + } + + [Fact] + public void WriteInt128BigEndianTest() + { + var stream = new MemoryStream(new byte[16], 0, 16, true, true); + var bw = new BinaryWriter(stream); + byte[] expected = [.. _bytes.Take(16)]; + bool write = bw.WriteBigEndian((Int128)new BigInteger(Enumerable.Reverse(_bytes).ToArray())); + Assert.True(write); + ValidateBytes(expected, stream.GetBuffer()); + } + + [Fact] + public void WriteInt128LittleEndianTest() + { + var stream = new MemoryStream(new byte[16], 0, 16, true, true); + var bw = new BinaryWriter(stream); + byte[] expected = [.. _bytes.Take(16)]; + bool write = bw.WriteLittleEndian((Int128)new BigInteger(_bytes)); + Assert.True(write); + ValidateBytes(expected, stream.GetBuffer()); + } + + [Fact] + public void WriteUInt128Test() + { + var stream = new MemoryStream(new byte[16], 0, 16, true, true); + var bw = new BinaryWriter(stream); + byte[] expected = [.. _bytes.Take(16)]; + bool write = bw.Write((UInt128)new BigInteger(_bytes)); + Assert.True(write); + ValidateBytes(expected, stream.GetBuffer()); + } + + [Fact] + public void WriteUInt128BigEndianTest() + { + var stream = new MemoryStream(new byte[16], 0, 16, true, true); + var bw = new BinaryWriter(stream); + byte[] expected = [.. _bytes.Take(16)]; + bool write = bw.WriteBigEndian((UInt128)new BigInteger(Enumerable.Reverse(_bytes).ToArray())); + Assert.True(write); + ValidateBytes(expected, stream.GetBuffer()); + } + + [Fact] + public void WriteUInt128LittleEndianTest() + { + var stream = new MemoryStream(new byte[16], 0, 16, true, true); + var bw = new BinaryWriter(stream); + byte[] expected = [.. _bytes.Take(16)]; + bool write = bw.WriteLittleEndian((UInt128)new BigInteger(_bytes)); + Assert.True(write); + ValidateBytes(expected, stream.GetBuffer()); + } + + [Fact] + public void WriteDecimalTest() + { + var stream = new MemoryStream(new byte[16], 0, 16, true, true); + var bw = new BinaryWriter(stream); + byte[] expected = [.. _decimalBytes.Take(16)]; + bw.Write(0.0123456789M); + ValidateBytes(expected, stream.GetBuffer()); + } + + [Fact] + public void WriteDecimalBigEndianTest() + { + var stream = new MemoryStream(new byte[16], 0, 16, true, true); + var bw = new BinaryWriter(stream); + byte[] expected = [.. _decimalBytes.Take(16).Reverse()]; + bool write = bw.WriteBigEndian(0.0123456789M); + Assert.True(write); + ValidateBytes(expected, stream.GetBuffer()); + } + + [Fact] + public void WriteDecimalLittleEndianTest() + { + var stream = new MemoryStream(new byte[16], 0, 16, true, true); + var bw = new BinaryWriter(stream); + byte[] expected = [.. _decimalBytes.Take(16)]; + bool write = bw.WriteLittleEndian(0.0123456789M); + Assert.True(write); + ValidateBytes(expected, stream.GetBuffer()); + } + + /// + /// Validate that a set of actual bytes matches the expected bytes + /// + private static void ValidateBytes(byte[] expected, byte[] actual) + { + for (int i = 0; i < expected.Length; i++) + { + Assert.Equal(expected[i], actual[i]); + } + } + } +} diff --git a/SabreTools.Numerics.Extensions.Test/ByteArrayReaderExtensionsTests.cs b/SabreTools.Numerics.Extensions.Test/ByteArrayReaderExtensionsTests.cs new file mode 100644 index 0000000..961ee87 --- /dev/null +++ b/SabreTools.Numerics.Extensions.Test/ByteArrayReaderExtensionsTests.cs @@ -0,0 +1,2106 @@ +using System; +using System.Linq; +using System.Numerics; +using Xunit; + +namespace SabreTools.Numerics.Extensions.Test +{ + public class ByteArrayReaderExtensionsTests + { + /// + /// Test pattern from 0x00-0x0F + /// + private static readonly byte[] _bytes = + [ + 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, + 0x08, 0x09, 0x0A, 0x0B, 0x0C, 0x0D, 0x0E, 0x0F, + ]; + + /// + /// Represents the decimal value 0.0123456789 + /// + private static readonly byte[] _decimalBytes = + [ + 0x15, 0xCD, 0x5B, 0x07, 0x00, 0x00, 0x00, 0x00, + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x0A, 0x00, + ]; + + /// + /// Test pattern for big-endian GUID created from + /// + private static readonly byte[] _guidBigEndianbytes = + [ + 0x03, 0x02, 0x01, 0x00, 0x05, 0x04, 0x07, 0x06, + 0x08, 0x09, 0x0A, 0x0B, 0x0C, 0x0D, 0x0E, 0x0F, + ]; + + #region Exact Read + + [Fact] + public void ReadByteTest() + { + int offset = 0; + byte read = _bytes.ReadByte(ref offset); + Assert.Equal(0x00, read); + } + + [Fact] + public void ReadByteValueTest() + { + int offset = 0; + byte read = _bytes.ReadByteValue(ref offset); + Assert.Equal(0x00, read); + } + + [Fact] + public void ReadByteBothEndianTest() + { + int offset = 0; + BothUInt8 read = _bytes.ReadByteBothEndian(ref offset); + Assert.Equal(0x00, read.LittleEndian); + Assert.Equal(0x01, read.BigEndian); + } + + [Fact] + public void ReadBytesTest() + { + int offset = 0, length = 4; + byte[] read = _bytes.ReadBytes(ref offset, length); + Assert.Equal(length, read.Length); + Assert.True(read.SequenceEqual(_bytes.Take(length))); + } + + [Fact] + public void ReadSByteTest() + { + int offset = 0; + sbyte read = _bytes.ReadSByte(ref offset); + Assert.Equal(0x00, read); + } + + [Fact] + public void ReadSByteBothEndianTest() + { + int offset = 0; + BothInt8 read = _bytes.ReadSByteBothEndian(ref offset); + Assert.Equal(0x00, read.LittleEndian); + Assert.Equal(0x01, read.BigEndian); + } + + [Fact] + public void ReadCharTest() + { + int offset = 0; + char read = _bytes.ReadChar(ref offset); + Assert.Equal('\0', read); + } + + [Fact] + public void ReadInt16Test() + { + int offset = 0; + short read = _bytes.ReadInt16(ref offset); + Assert.Equal(0x0100, read); + } + + [Fact] + public void ReadInt16BigEndianTest() + { + int offset = 0; + short read = _bytes.ReadInt16BigEndian(ref offset); + Assert.Equal(0x0001, read); + } + + [Fact] + public void ReadInt16LittleEndianTest() + { + int offset = 0; + short read = _bytes.ReadInt16LittleEndian(ref offset); + Assert.Equal(0x0100, read); + } + + [Fact] + public void ReadInt16BothEndianTest() + { + int offset = 0; + BothInt16 read = _bytes.ReadInt16BothEndian(ref offset); + Assert.Equal(0x0100, read.LittleEndian); + Assert.Equal(0x0203, read.BigEndian); + } + + [Fact] + public void ReadUInt16Test() + { + int offset = 0; + ushort read = _bytes.ReadUInt16(ref offset); + Assert.Equal(0x0100, read); + } + + [Fact] + public void ReadUInt16BigEndianTest() + { + int offset = 0; + ushort read = _bytes.ReadUInt16BigEndian(ref offset); + Assert.Equal(0x0001, read); + } + + [Fact] + public void ReadUInt16LittleEndianTest() + { + int offset = 0; + ushort read = _bytes.ReadUInt16LittleEndian(ref offset); + Assert.Equal(0x0100, read); + } + + [Fact] + public void ReadUInt16BothEndianTest() + { + int offset = 0; + BothUInt16 read = _bytes.ReadUInt16BothEndian(ref offset); + Assert.Equal(0x0100, read.LittleEndian); + Assert.Equal(0x0203, read.BigEndian); + } + + [Fact] + public void ReadWORDTest() + { + int offset = 0; + ushort read = _bytes.ReadWORD(ref offset); + Assert.Equal(0x0100, read); + } + + [Fact] + public void ReadWORDBigEndianTest() + { + int offset = 0; + ushort read = _bytes.ReadWORDBigEndian(ref offset); + Assert.Equal(0x0001, read); + } + + [Fact] + public void ReadWORDLittleEndianTest() + { + int offset = 0; + ushort read = _bytes.ReadWORDLittleEndian(ref offset); + Assert.Equal(0x0100, read); + } + + [Fact] + public void ReadWORDBothEndianTest() + { + int offset = 0; + BothUInt16 read = _bytes.ReadWORDBothEndian(ref offset); + Assert.Equal(0x0100, read.LittleEndian); + Assert.Equal(0x0203, read.BigEndian); + } + + [Fact] + public void ReadHalfTest() + { + int offset = 0; + Half expected = BitConverter.Int16BitsToHalf(0x0100); + Half read = _bytes.ReadHalf(ref offset); + Assert.Equal(expected, read); + } + + [Fact] + public void ReadHalfBigEndianTest() + { + int offset = 0; + Half expected = BitConverter.Int16BitsToHalf(0x0001); + Half read = _bytes.ReadHalfBigEndian(ref offset); + Assert.Equal(expected, read); + } + + [Fact] + public void ReadHalfLittleEndianTest() + { + int offset = 0; + Half expected = BitConverter.Int16BitsToHalf(0x0100); + Half read = _bytes.ReadHalfLittleEndian(ref offset); + Assert.Equal(expected, read); + } + + [Fact] + public void ReadInt24Test() + { + int offset = 0; + Int24 read = _bytes.ReadInt24(ref offset); + Assert.Equal(0x020100, (int)read); + } + + [Fact] + public void ReadInt24BigEndianTest() + { + int offset = 0; + Int24 read = _bytes.ReadInt24BigEndian(ref offset); + Assert.Equal(0x000102, (int)read); + } + + [Fact] + public void ReadInt24LittleEndianTest() + { + int offset = 0; + Int24 read = _bytes.ReadInt24LittleEndian(ref offset); + Assert.Equal(0x020100, (int)read); + } + + [Fact] + public void ReadUInt24Test() + { + int offset = 0; + UInt24 read = _bytes.ReadUInt24(ref offset); + Assert.Equal((uint)0x020100, (uint)read); + } + + [Fact] + public void ReadUInt24BigEndianTest() + { + int offset = 0; + UInt24 read = _bytes.ReadUInt24BigEndian(ref offset); + Assert.Equal((uint)0x000102, (uint)read); + } + + [Fact] + public void ReadUInt24LittleEndianTest() + { + int offset = 0; + UInt24 read = _bytes.ReadUInt24LittleEndian(ref offset); + Assert.Equal((uint)0x020100, (uint)read); + } + + [Fact] + public void ReadInt32Test() + { + int offset = 0; + int read = _bytes.ReadInt32(ref offset); + Assert.Equal(0x03020100, read); + } + + [Fact] + public void ReadInt32BigEndianTest() + { + int offset = 0; + int read = _bytes.ReadInt32BigEndian(ref offset); + Assert.Equal(0x00010203, read); + } + + [Fact] + public void ReadInt32LittleEndianTest() + { + int offset = 0; + int read = _bytes.ReadInt32LittleEndian(ref offset); + Assert.Equal(0x03020100, read); + } + + [Fact] + public void ReadInt32BothEndianTest() + { + int offset = 0; + BothInt32 read = _bytes.ReadInt32BothEndian(ref offset); + Assert.Equal(0x03020100, read.LittleEndian); + Assert.Equal(0x04050607, read.BigEndian); + } + + [Fact] + public void ReadUInt32Test() + { + int offset = 0; + uint read = _bytes.ReadUInt32(ref offset); + Assert.Equal((uint)0x03020100, read); + } + + [Fact] + public void ReadUInt32BigEndianTest() + { + int offset = 0; + uint read = _bytes.ReadUInt32BigEndian(ref offset); + Assert.Equal((uint)0x00010203, read); + } + + [Fact] + public void ReadUInt32LittleEndianTest() + { + int offset = 0; + uint read = _bytes.ReadUInt32LittleEndian(ref offset); + Assert.Equal((uint)0x03020100, read); + } + + [Fact] + public void ReadUInt32BothEndianTest() + { + int offset = 0; + BothUInt32 read = _bytes.ReadUInt32BothEndian(ref offset); + Assert.Equal((uint)0x03020100, read.LittleEndian); + Assert.Equal((uint)0x04050607, read.BigEndian); + } + + [Fact] + public void ReadDWORDTest() + { + int offset = 0; + uint read = _bytes.ReadDWORD(ref offset); + Assert.Equal((uint)0x03020100, read); + } + + [Fact] + public void ReadDWORDBigEndianTest() + { + int offset = 0; + uint read = _bytes.ReadDWORDBigEndian(ref offset); + Assert.Equal((uint)0x00010203, read); + } + + [Fact] + public void ReadDWORDLittleEndianTest() + { + int offset = 0; + uint read = _bytes.ReadDWORDLittleEndian(ref offset); + Assert.Equal((uint)0x03020100, read); + } + + [Fact] + public void ReadDWORDBothEndianTest() + { + int offset = 0; + BothUInt32 read = _bytes.ReadDWORDBothEndian(ref offset); + Assert.Equal((uint)0x03020100, read.LittleEndian); + Assert.Equal((uint)0x04050607, read.BigEndian); + } + + [Fact] + public void ReadSingleTest() + { + int offset = 0; + float expected = BitConverter.Int32BitsToSingle(0x03020100); + float read = _bytes.ReadSingle(ref offset); + Assert.Equal(expected, read); + } + + [Fact] + public void ReadSingleBigEndianTest() + { + int offset = 0; + float expected = BitConverter.Int32BitsToSingle(0x00010203); + float read = _bytes.ReadSingleBigEndian(ref offset); + Assert.Equal(expected, read); + } + + [Fact] + public void ReadSingleLittleEndianTest() + { + int offset = 0; + float expected = BitConverter.Int32BitsToSingle(0x03020100); + float read = _bytes.ReadSingleLittleEndian(ref offset); + Assert.Equal(expected, read); + } + + [Fact] + public void ReadInt48Test() + { + int offset = 0; + Int48 read = _bytes.ReadInt48(ref offset); + Assert.Equal(0x050403020100, (long)read); + } + + [Fact] + public void ReadInt48BigEndianTest() + { + int offset = 0; + Int48 read = _bytes.ReadInt48BigEndian(ref offset); + Assert.Equal(0x000102030405, (long)read); + } + + [Fact] + public void ReadInt48LittleEndianTest() + { + int offset = 0; + Int48 read = _bytes.ReadInt48LittleEndian(ref offset); + Assert.Equal(0x050403020100, (long)read); + } + + [Fact] + public void ReadUInt48Test() + { + int offset = 0; + UInt48 read = _bytes.ReadUInt48(ref offset); + Assert.Equal((ulong)0x050403020100, (ulong)read); + } + + [Fact] + public void ReadUInt48BigEndianTest() + { + int offset = 0; + UInt48 read = _bytes.ReadUInt48BigEndian(ref offset); + Assert.Equal((ulong)0x000102030405, (ulong)read); + } + + [Fact] + public void ReadUInt48LittleEndianTest() + { + int offset = 0; + UInt48 read = _bytes.ReadUInt48LittleEndian(ref offset); + Assert.Equal((ulong)0x050403020100, (ulong)read); + } + + [Fact] + public void ReadInt64Test() + { + int offset = 0; + long read = _bytes.ReadInt64(ref offset); + Assert.Equal(0x0706050403020100, read); + } + + [Fact] + public void ReadInt64BigEndianTest() + { + int offset = 0; + long read = _bytes.ReadInt64BigEndian(ref offset); + Assert.Equal(0x0001020304050607, read); + } + + [Fact] + public void ReadInt64LittleEndianTest() + { + int offset = 0; + long read = _bytes.ReadInt64LittleEndian(ref offset); + Assert.Equal(0x0706050403020100, read); + } + + [Fact] + public void ReadInt64BothEndianTest() + { + int offset = 0; + BothInt64 read = _bytes.ReadInt64BothEndian(ref offset); + Assert.Equal(0x0706050403020100, read.LittleEndian); + Assert.Equal(0x08090A0B0C0D0E0F, read.BigEndian); + } + + [Fact] + public void ReadUInt64Test() + { + int offset = 0; + ulong read = _bytes.ReadUInt64(ref offset); + Assert.Equal((ulong)0x0706050403020100, read); + } + + [Fact] + public void ReadUInt64BigEndianTest() + { + int offset = 0; + ulong read = _bytes.ReadUInt64BigEndian(ref offset); + Assert.Equal((ulong)0x0001020304050607, read); + } + + [Fact] + public void ReadUInt64LittleEndianTest() + { + int offset = 0; + ulong read = _bytes.ReadUInt64LittleEndian(ref offset); + Assert.Equal((ulong)0x0706050403020100, read); + } + + [Fact] + public void ReadUInt64BothEndianTest() + { + int offset = 0; + BothUInt64 read = _bytes.ReadUInt64BothEndian(ref offset); + Assert.Equal((ulong)0x0706050403020100, read.LittleEndian); + Assert.Equal((ulong)0x08090A0B0C0D0E0F, read.BigEndian); + } + + [Fact] + public void ReadQWORDTest() + { + int offset = 0; + ulong read = _bytes.ReadQWORD(ref offset); + Assert.Equal((ulong)0x0706050403020100, read); + } + + [Fact] + public void ReadQWORDBigEndianTest() + { + int offset = 0; + ulong read = _bytes.ReadQWORDBigEndian(ref offset); + Assert.Equal((ulong)0x0001020304050607, read); + } + + [Fact] + public void ReadQWORDLittleEndianTest() + { + int offset = 0; + ulong read = _bytes.ReadQWORDLittleEndian(ref offset); + Assert.Equal((ulong)0x0706050403020100, read); + } + + [Fact] + public void ReadQWORDBothEndianTest() + { + int offset = 0; + BothUInt64 read = _bytes.ReadQWORDBothEndian(ref offset); + Assert.Equal((ulong)0x0706050403020100, read.LittleEndian); + Assert.Equal((ulong)0x08090A0B0C0D0E0F, read.BigEndian); + } + + [Fact] + public void ReadDoubleTest() + { + int offset = 0; + double expected = BitConverter.Int64BitsToDouble(0x0706050403020100); + double read = _bytes.ReadDouble(ref offset); + Assert.Equal(expected, read); + } + + [Fact] + public void ReadDoubleBigEndianTest() + { + int offset = 0; + double expected = BitConverter.Int64BitsToDouble(0x0001020304050607); + double read = _bytes.ReadDoubleBigEndian(ref offset); + Assert.Equal(expected, read); + } + + [Fact] + public void ReadDoubleLittleEndianTest() + { + int offset = 0; + double expected = BitConverter.Int64BitsToDouble(0x0706050403020100); + double read = _bytes.ReadDoubleLittleEndian(ref offset); + Assert.Equal(expected, read); + } + + [Fact] + public void ReadGuidTest() + { + int offset = 0; + var expected = new Guid(_bytes); + Guid read = _bytes.ReadGuid(ref offset); + Assert.Equal(expected, read); + } + + [Fact] + public void ReadGuidBigEndianTest() + { + int offset = 0; + var expected = new Guid(_guidBigEndianbytes); + Guid read = _bytes.ReadGuidBigEndian(ref offset); + Assert.Equal(expected, read); + } + + [Fact] + public void ReadGuidLittleEndianTest() + { + int offset = 0; + var expected = new Guid(_bytes); + Guid read = _bytes.ReadGuidLittleEndian(ref offset); + Assert.Equal(expected, read); + } + + [Fact] + public void ReadInt128Test() + { + int offset = 0; + var expected = (Int128)new BigInteger(_bytes); + Int128 read = _bytes.ReadInt128(ref offset); + Assert.Equal(expected, read); + } + + [Fact] + public void ReadInt128BigEndianTest() + { + int offset = 0; + var reversed = Enumerable.Reverse(_bytes).ToArray(); + var expected = (Int128)new BigInteger(reversed); + Int128 read = _bytes.ReadInt128BigEndian(ref offset); + Assert.Equal(expected, read); + } + + [Fact] + public void ReadInt128LittleEndianTest() + { + int offset = 0; + var expected = (Int128)new BigInteger(_bytes); + Int128 read = _bytes.ReadInt128LittleEndian(ref offset); + Assert.Equal(expected, read); + } + + [Fact] + public void ReadUInt128Test() + { + int offset = 0; + var expected = (UInt128)new BigInteger(_bytes); + UInt128 read = _bytes.ReadUInt128(ref offset); + Assert.Equal(expected, read); + } + + [Fact] + public void ReadUInt128BigEndianTest() + { + int offset = 0; + var reversed = Enumerable.Reverse(_bytes).ToArray(); + var expected = (UInt128)new BigInteger(reversed); + UInt128 read = _bytes.ReadUInt128BigEndian(ref offset); + Assert.Equal(expected, read); + } + + [Fact] + public void ReadUInt128LittleEndianTest() + { + int offset = 0; + var expected = (UInt128)new BigInteger(_bytes); + UInt128 read = _bytes.ReadUInt128LittleEndian(ref offset); + Assert.Equal(expected, read); + } + + [Fact] + public void ReadDecimalTest() + { + int offset = 0; + decimal expected = 0.0123456789M; + decimal read = _decimalBytes.ReadDecimal(ref offset); + Assert.Equal(expected, read); + } + + [Fact] + public void ReadDecimalBigEndianTest() + { + int offset = 0; + decimal expected = 0.0123456789M; + decimal read = Enumerable.Reverse(_decimalBytes).ToArray().ReadDecimalBigEndian(ref offset); + Assert.Equal(expected, read); + } + + [Fact] + public void ReadDecimalLittleEndianTest() + { + int offset = 0; + decimal expected = 0.0123456789M; + decimal read = _decimalBytes.ReadDecimalLittleEndian(ref offset); + Assert.Equal(expected, read); + } + + #endregion + + #region Peek Read + + [Fact] + public void PeekByteTest() + { + int offset = 0; + byte read = _bytes.PeekByte(ref offset); + Assert.Equal(0x00, read); + Assert.Equal(0, offset); + } + + [Fact] + public void PeekByteValueTest() + { + int offset = 0; + byte read = _bytes.PeekByteValue(ref offset); + Assert.Equal(0x00, read); + Assert.Equal(0, offset); + } + + [Fact] + public void PeekByteBothEndianTest() + { + int offset = 0; + BothUInt8 read = _bytes.PeekByteBothEndian(ref offset); + Assert.Equal(0x00, read.LittleEndian); + Assert.Equal(0x01, read.BigEndian); + Assert.Equal(0, offset); + } + + [Fact] + public void PeekBytesTest() + { + int offset = 0, length = 4; + byte[] read = _bytes.PeekBytes(ref offset, length); + Assert.Equal(length, read.Length); + Assert.True(read.SequenceEqual(_bytes.Take(length))); + Assert.Equal(0, offset); + } + + [Fact] + public void PeekSByteTest() + { + int offset = 0; + sbyte read = _bytes.PeekSByte(ref offset); + Assert.Equal(0x00, read); + Assert.Equal(0, offset); + } + + [Fact] + public void PeekSByteBothEndianTest() + { + int offset = 0; + BothInt8 read = _bytes.PeekSByteBothEndian(ref offset); + Assert.Equal(0x00, read.LittleEndian); + Assert.Equal(0x01, read.BigEndian); + Assert.Equal(0, offset); + } + + [Fact] + public void PeekCharTest() + { + int offset = 0; + char read = _bytes.PeekChar(ref offset); + Assert.Equal('\0', read); + Assert.Equal(0, offset); + } + + [Fact] + public void PeekInt16Test() + { + int offset = 0; + short read = _bytes.PeekInt16(ref offset); + Assert.Equal(0x0100, read); + Assert.Equal(0, offset); + } + + [Fact] + public void PeekInt16BigEndianTest() + { + int offset = 0; + short read = _bytes.PeekInt16BigEndian(ref offset); + Assert.Equal(0x0001, read); + Assert.Equal(0, offset); + } + + [Fact] + public void PeekInt16LittleEndianTest() + { + int offset = 0; + short read = _bytes.PeekInt16LittleEndian(ref offset); + Assert.Equal(0x0100, read); + Assert.Equal(0, offset); + } + + [Fact] + public void PeekInt16BothEndianTest() + { + int offset = 0; + BothInt16 read = _bytes.PeekInt16BothEndian(ref offset); + Assert.Equal(0x0100, read.LittleEndian); + Assert.Equal(0x0203, read.BigEndian); + Assert.Equal(0, offset); + } + + [Fact] + public void PeekUInt16Test() + { + int offset = 0; + ushort read = _bytes.PeekUInt16(ref offset); + Assert.Equal(0x0100, read); + Assert.Equal(0, offset); + } + + [Fact] + public void PeekUInt16BigEndianTest() + { + int offset = 0; + ushort read = _bytes.PeekUInt16BigEndian(ref offset); + Assert.Equal(0x0001, read); + Assert.Equal(0, offset); + } + + [Fact] + public void PeekUInt16LittleEndianTest() + { + int offset = 0; + ushort read = _bytes.PeekUInt16LittleEndian(ref offset); + Assert.Equal(0x0100, read); + Assert.Equal(0, offset); + } + + [Fact] + public void PeekUInt16BothEndianTest() + { + int offset = 0; + BothUInt16 read = _bytes.PeekUInt16BothEndian(ref offset); + Assert.Equal(0x0100, read.LittleEndian); + Assert.Equal(0x0203, read.BigEndian); + Assert.Equal(0, offset); + } + + [Fact] + public void PeekWORDTest() + { + int offset = 0; + ushort read = _bytes.PeekWORD(ref offset); + Assert.Equal(0x0100, read); + Assert.Equal(0, offset); + } + + [Fact] + public void PeekWORDBigEndianTest() + { + int offset = 0; + ushort read = _bytes.PeekWORDBigEndian(ref offset); + Assert.Equal(0x0001, read); + Assert.Equal(0, offset); + } + + [Fact] + public void PeekWORDLittleEndianTest() + { + int offset = 0; + ushort read = _bytes.PeekWORDLittleEndian(ref offset); + Assert.Equal(0x0100, read); + Assert.Equal(0, offset); + } + + [Fact] + public void PeekWORDBothEndianTest() + { + int offset = 0; + BothUInt16 read = _bytes.PeekWORDBothEndian(ref offset); + Assert.Equal(0x0100, read.LittleEndian); + Assert.Equal(0x0203, read.BigEndian); + Assert.Equal(0, offset); + } + + [Fact] + public void PeekHalfTest() + { + int offset = 0; + Half expected = BitConverter.Int16BitsToHalf(0x0100); + Half read = _bytes.PeekHalf(ref offset); + Assert.Equal(expected, read); + Assert.Equal(0, offset); + } + + [Fact] + public void PeekHalfBigEndianTest() + { + int offset = 0; + Half expected = BitConverter.Int16BitsToHalf(0x0001); + Half read = _bytes.PeekHalfBigEndian(ref offset); + Assert.Equal(expected, read); + Assert.Equal(0, offset); + } + + [Fact] + public void PeekHalfLittleEndianTest() + { + int offset = 0; + Half expected = BitConverter.Int16BitsToHalf(0x0100); + Half read = _bytes.PeekHalfLittleEndian(ref offset); + Assert.Equal(expected, read); + Assert.Equal(0, offset); + } + + [Fact] + public void PeekInt24Test() + { + int offset = 0; + Int24 read = _bytes.PeekInt24(ref offset); + Assert.Equal(0x020100, (int)read); + Assert.Equal(0, offset); + } + + [Fact] + public void PeekInt24BigEndianTest() + { + int offset = 0; + Int24 read = _bytes.PeekInt24BigEndian(ref offset); + Assert.Equal(0x000102, (int)read); + Assert.Equal(0, offset); + } + + [Fact] + public void PeekInt24LittleEndianTest() + { + int offset = 0; + Int24 read = _bytes.PeekInt24LittleEndian(ref offset); + Assert.Equal(0x020100, (int)read); + Assert.Equal(0, offset); + } + + [Fact] + public void PeekUInt24Test() + { + int offset = 0; + UInt24 read = _bytes.PeekUInt24(ref offset); + Assert.Equal((uint)0x020100, (uint)read); + Assert.Equal(0, offset); + } + + [Fact] + public void PeekUInt24BigEndianTest() + { + int offset = 0; + UInt24 read = _bytes.PeekUInt24BigEndian(ref offset); + Assert.Equal((uint)0x000102, (uint)read); + Assert.Equal(0, offset); + } + + [Fact] + public void PeekUInt24LittleEndianTest() + { + int offset = 0; + UInt24 read = _bytes.PeekUInt24LittleEndian(ref offset); + Assert.Equal((uint)0x020100, (uint)read); + Assert.Equal(0, offset); + } + + [Fact] + public void PeekInt32Test() + { + int offset = 0; + int read = _bytes.PeekInt32(ref offset); + Assert.Equal(0x03020100, read); + Assert.Equal(0, offset); + } + + [Fact] + public void PeekInt32BigEndianTest() + { + int offset = 0; + int read = _bytes.PeekInt32BigEndian(ref offset); + Assert.Equal(0x00010203, read); + Assert.Equal(0, offset); + } + + [Fact] + public void PeekInt32LittleEndianTest() + { + int offset = 0; + int read = _bytes.PeekInt32LittleEndian(ref offset); + Assert.Equal(0x03020100, read); + Assert.Equal(0, offset); + } + + [Fact] + public void PeekInt32BothEndianTest() + { + int offset = 0; + BothInt32 read = _bytes.PeekInt32BothEndian(ref offset); + Assert.Equal(0x03020100, read.LittleEndian); + Assert.Equal(0x04050607, read.BigEndian); + Assert.Equal(0, offset); + } + + [Fact] + public void PeekUInt32Test() + { + int offset = 0; + uint read = _bytes.PeekUInt32(ref offset); + Assert.Equal((uint)0x03020100, read); + Assert.Equal(0, offset); + } + + [Fact] + public void PeekUInt32BigEndianTest() + { + int offset = 0; + uint read = _bytes.PeekUInt32BigEndian(ref offset); + Assert.Equal((uint)0x00010203, read); + Assert.Equal(0, offset); + } + + [Fact] + public void PeekUInt32LittleEndianTest() + { + int offset = 0; + uint read = _bytes.PeekUInt32LittleEndian(ref offset); + Assert.Equal((uint)0x03020100, read); + Assert.Equal(0, offset); + } + + [Fact] + public void PeekUInt32BothEndianTest() + { + int offset = 0; + BothUInt32 read = _bytes.PeekUInt32BothEndian(ref offset); + Assert.Equal((uint)0x03020100, read.LittleEndian); + Assert.Equal((uint)0x04050607, read.BigEndian); + Assert.Equal(0, offset); + } + + [Fact] + public void PeekDWORDTest() + { + int offset = 0; + uint read = _bytes.PeekDWORD(ref offset); + Assert.Equal((uint)0x03020100, read); + Assert.Equal(0, offset); + } + + [Fact] + public void PeekDWORDBigEndianTest() + { + int offset = 0; + uint read = _bytes.PeekDWORDBigEndian(ref offset); + Assert.Equal((uint)0x00010203, read); + Assert.Equal(0, offset); + } + + [Fact] + public void PeekDWORDLittleEndianTest() + { + int offset = 0; + uint read = _bytes.PeekDWORDLittleEndian(ref offset); + Assert.Equal((uint)0x03020100, read); + Assert.Equal(0, offset); + } + + [Fact] + public void PeekDWORDBothEndianTest() + { + int offset = 0; + BothUInt32 read = _bytes.PeekDWORDBothEndian(ref offset); + Assert.Equal((uint)0x03020100, read.LittleEndian); + Assert.Equal((uint)0x04050607, read.BigEndian); + Assert.Equal(0, offset); + } + + [Fact] + public void PeekSingleTest() + { + int offset = 0; + float expected = BitConverter.Int32BitsToSingle(0x03020100); + float read = _bytes.PeekSingle(ref offset); + Assert.Equal(expected, read); + Assert.Equal(0, offset); + } + + [Fact] + public void PeekSingleBigEndianTest() + { + int offset = 0; + float expected = BitConverter.Int32BitsToSingle(0x00010203); + float read = _bytes.PeekSingleBigEndian(ref offset); + Assert.Equal(expected, read); + Assert.Equal(0, offset); + } + + [Fact] + public void PeekSingleLittleEndianTest() + { + int offset = 0; + float expected = BitConverter.Int32BitsToSingle(0x03020100); + float read = _bytes.PeekSingleLittleEndian(ref offset); + Assert.Equal(expected, read); + Assert.Equal(0, offset); + } + + [Fact] + public void PeekInt48Test() + { + int offset = 0; + Int48 read = _bytes.PeekInt48(ref offset); + Assert.Equal(0x050403020100, (long)read); + Assert.Equal(0, offset); + } + + [Fact] + public void PeekInt48BigEndianTest() + { + int offset = 0; + Int48 read = _bytes.PeekInt48BigEndian(ref offset); + Assert.Equal(0x000102030405, (long)read); + Assert.Equal(0, offset); + } + + [Fact] + public void PeekInt48LittleEndianTest() + { + int offset = 0; + Int48 read = _bytes.PeekInt48LittleEndian(ref offset); + Assert.Equal(0x050403020100, (long)read); + Assert.Equal(0, offset); + } + + [Fact] + public void PeekUInt48Test() + { + int offset = 0; + UInt48 read = _bytes.PeekUInt48(ref offset); + Assert.Equal((ulong)0x050403020100, (ulong)read); + Assert.Equal(0, offset); + } + + [Fact] + public void PeekUInt48BigEndianTest() + { + int offset = 0; + UInt48 read = _bytes.PeekUInt48BigEndian(ref offset); + Assert.Equal((ulong)0x000102030405, (ulong)read); + Assert.Equal(0, offset); + } + + [Fact] + public void PeekUInt48LittleEndianTest() + { + int offset = 0; + UInt48 read = _bytes.PeekUInt48LittleEndian(ref offset); + Assert.Equal((ulong)0x050403020100, (ulong)read); + Assert.Equal(0, offset); + } + + [Fact] + public void PeekInt64Test() + { + int offset = 0; + long read = _bytes.PeekInt64(ref offset); + Assert.Equal(0x0706050403020100, read); + Assert.Equal(0, offset); + } + + [Fact] + public void PeekInt64BigEndianTest() + { + int offset = 0; + long read = _bytes.PeekInt64BigEndian(ref offset); + Assert.Equal(0x0001020304050607, read); + Assert.Equal(0, offset); + } + + [Fact] + public void PeekInt64LittleEndianTest() + { + int offset = 0; + long read = _bytes.PeekInt64LittleEndian(ref offset); + Assert.Equal(0x0706050403020100, read); + Assert.Equal(0, offset); + } + + [Fact] + public void PeekInt64BothEndianTest() + { + int offset = 0; + BothInt64 read = _bytes.PeekInt64BothEndian(ref offset); + Assert.Equal(0x0706050403020100, read.LittleEndian); + Assert.Equal(0x08090A0B0C0D0E0F, read.BigEndian); + Assert.Equal(0, offset); + } + + [Fact] + public void PeekUInt64Test() + { + int offset = 0; + ulong read = _bytes.PeekUInt64(ref offset); + Assert.Equal((ulong)0x0706050403020100, read); + Assert.Equal(0, offset); + } + + [Fact] + public void PeekUInt64BigEndianTest() + { + int offset = 0; + ulong read = _bytes.PeekUInt64BigEndian(ref offset); + Assert.Equal((ulong)0x0001020304050607, read); + Assert.Equal(0, offset); + } + + [Fact] + public void PeekUInt64LittleEndianTest() + { + int offset = 0; + ulong read = _bytes.PeekUInt64LittleEndian(ref offset); + Assert.Equal((ulong)0x0706050403020100, read); + Assert.Equal(0, offset); + } + + [Fact] + public void PeekUInt64BothEndianTest() + { + int offset = 0; + BothUInt64 read = _bytes.PeekUInt64BothEndian(ref offset); + Assert.Equal((ulong)0x0706050403020100, read.LittleEndian); + Assert.Equal((ulong)0x08090A0B0C0D0E0F, read.BigEndian); + Assert.Equal(0, offset); + } + + [Fact] + public void PeekQWORDTest() + { + int offset = 0; + ulong read = _bytes.PeekQWORD(ref offset); + Assert.Equal((ulong)0x0706050403020100, read); + Assert.Equal(0, offset); + } + + [Fact] + public void PeekQWORDBigEndianTest() + { + int offset = 0; + ulong read = _bytes.PeekQWORDBigEndian(ref offset); + Assert.Equal((ulong)0x0001020304050607, read); + Assert.Equal(0, offset); + } + + [Fact] + public void PeekQWORDLittleEndianTest() + { + int offset = 0; + ulong read = _bytes.PeekQWORDLittleEndian(ref offset); + Assert.Equal((ulong)0x0706050403020100, read); + Assert.Equal(0, offset); + } + + [Fact] + public void PeekQWORDBothEndianTest() + { + int offset = 0; + BothUInt64 read = _bytes.PeekQWORDBothEndian(ref offset); + Assert.Equal((ulong)0x0706050403020100, read.LittleEndian); + Assert.Equal((ulong)0x08090A0B0C0D0E0F, read.BigEndian); + Assert.Equal(0, offset); + } + + [Fact] + public void PeekDoubleTest() + { + int offset = 0; + double expected = BitConverter.Int64BitsToDouble(0x0706050403020100); + double read = _bytes.PeekDouble(ref offset); + Assert.Equal(expected, read); + Assert.Equal(0, offset); + } + + [Fact] + public void PeekDoubleBigEndianTest() + { + int offset = 0; + double expected = BitConverter.Int64BitsToDouble(0x0001020304050607); + double read = _bytes.PeekDoubleBigEndian(ref offset); + Assert.Equal(expected, read); + Assert.Equal(0, offset); + } + + [Fact] + public void PeekDoubleLittleEndianTest() + { + int offset = 0; + double expected = BitConverter.Int64BitsToDouble(0x0706050403020100); + double read = _bytes.PeekDoubleLittleEndian(ref offset); + Assert.Equal(expected, read); + Assert.Equal(0, offset); + } + + [Fact] + public void PeekGuidTest() + { + int offset = 0; + var expected = new Guid(_bytes); + Guid read = _bytes.PeekGuid(ref offset); + Assert.Equal(expected, read); + Assert.Equal(0, offset); + } + + [Fact] + public void PeekGuidBigEndianTest() + { + int offset = 0; + var expected = new Guid(_guidBigEndianbytes); + Guid read = _bytes.PeekGuidBigEndian(ref offset); + Assert.Equal(expected, read); + Assert.Equal(0, offset); + } + + [Fact] + public void PeekGuidLittleEndianTest() + { + int offset = 0; + var expected = new Guid(_bytes); + Guid read = _bytes.PeekGuidLittleEndian(ref offset); + Assert.Equal(expected, read); + Assert.Equal(0, offset); + } + + [Fact] + public void PeekInt128Test() + { + int offset = 0; + var expected = (Int128)new BigInteger(_bytes); + Int128 read = _bytes.PeekInt128(ref offset); + Assert.Equal(expected, read); + Assert.Equal(0, offset); + } + + [Fact] + public void PeekInt128BigEndianTest() + { + int offset = 0; + var reversed = Enumerable.Reverse(_bytes).ToArray(); + var expected = (Int128)new BigInteger(reversed); + Int128 read = _bytes.PeekInt128BigEndian(ref offset); + Assert.Equal(expected, read); + Assert.Equal(0, offset); + } + + [Fact] + public void PeekInt128LittleEndianTest() + { + int offset = 0; + var expected = (Int128)new BigInteger(_bytes); + Int128 read = _bytes.PeekInt128LittleEndian(ref offset); + Assert.Equal(expected, read); + Assert.Equal(0, offset); + } + + [Fact] + public void PeekUInt128Test() + { + int offset = 0; + var expected = (UInt128)new BigInteger(_bytes); + UInt128 read = _bytes.PeekUInt128(ref offset); + Assert.Equal(expected, read); + Assert.Equal(0, offset); + } + + [Fact] + public void PeekUInt128BigEndianTest() + { + int offset = 0; + var reversed = Enumerable.Reverse(_bytes).ToArray(); + var expected = (UInt128)new BigInteger(reversed); + UInt128 read = _bytes.PeekUInt128BigEndian(ref offset); + Assert.Equal(expected, read); + Assert.Equal(0, offset); + } + + [Fact] + public void PeekUInt128LittleEndianTest() + { + int offset = 0; + var expected = (UInt128)new BigInteger(_bytes); + UInt128 read = _bytes.PeekUInt128LittleEndian(ref offset); + Assert.Equal(expected, read); + Assert.Equal(0, offset); + } + + [Fact] + public void PeekDecimalTest() + { + int offset = 0; + decimal expected = 0.0123456789M; + decimal read = _decimalBytes.PeekDecimal(ref offset); + Assert.Equal(expected, read); + Assert.Equal(0, offset); + } + + [Fact] + public void PeekDecimalBigEndianTest() + { + int offset = 0; + decimal expected = 0.0123456789M; + decimal read = Enumerable.Reverse(_decimalBytes).ToArray().PeekDecimalBigEndian(ref offset); + Assert.Equal(expected, read); + Assert.Equal(0, offset); + } + + [Fact] + public void PeekDecimalLittleEndianTest() + { + int offset = 0; + decimal expected = 0.0123456789M; + decimal read = _decimalBytes.PeekDecimalLittleEndian(ref offset); + Assert.Equal(expected, read); + Assert.Equal(0, offset); + } + + #endregion + + #region Try Read + + [Fact] + public void TryReadByteTest() + { + int offset = 0; + bool actual = Array.Empty().TryReadByte(ref offset, out byte read); + Assert.False(actual); + Assert.Equal(default, read); + } + + [Fact] + public void TryReadByteValueTest() + { + int offset = 0; + bool actual = Array.Empty().TryReadByteValue(ref offset, out byte read); + Assert.False(actual); + Assert.Equal(default, read); + } + + [Fact] + public void TryReadByteBothEndianTest() + { + int offset = 0; + bool actual = Array.Empty().TryReadByteBothEndian(ref offset, out BothUInt8 read); + Assert.False(actual); + Assert.Equal(default, read.LittleEndian); + Assert.Equal(default, read.BigEndian); + } + + [Fact] + public void TryReadBytesTest() + { + int offset = 0, length = 4; + bool actual = Array.Empty().TryReadBytes(ref offset, length, out byte[] read); + Assert.False(actual); + Assert.Empty(read); + } + + [Fact] + public void TryReadSByteTest() + { + int offset = 0; + bool actual = Array.Empty().TryReadSByte(ref offset, out sbyte read); + Assert.False(actual); + Assert.Equal(default, read); + } + + [Fact] + public void TryReadSByteBothEndianTest() + { + int offset = 0; + bool actual = Array.Empty().TryReadSByteBothEndian(ref offset, out BothInt8 read); + Assert.False(actual); + Assert.Equal(default, read.LittleEndian); + Assert.Equal(default, read.BigEndian); + } + + [Fact] + public void TryReadCharTest() + { + int offset = 0; + bool actual = Array.Empty().TryReadChar(ref offset, out char read); + Assert.False(actual); + Assert.Equal(default, read); + } + + [Fact] + public void TryReadInt16Test() + { + int offset = 0; + bool actual = Array.Empty().TryReadInt16(ref offset, out short read); + Assert.False(actual); + Assert.Equal(default, read); + } + + [Fact] + public void TryReadInt16BigEndianTest() + { + int offset = 0; + bool actual = Array.Empty().TryReadInt16BigEndian(ref offset, out short read); + Assert.False(actual); + Assert.Equal(default, read); + } + + [Fact] + public void TryReadInt16LittleEndianTest() + { + int offset = 0; + bool actual = Array.Empty().TryReadInt16LittleEndian(ref offset, out short read); + Assert.False(actual); + Assert.Equal(default, read); + } + + [Fact] + public void TryReadInt16BothEndianTest() + { + int offset = 0; + bool actual = Array.Empty().TryReadInt16BothEndian(ref offset, out BothInt16 read); + Assert.False(actual); + Assert.Equal(default, read.LittleEndian); + Assert.Equal(default, read.BigEndian); + } + + [Fact] + public void TryReadUInt16Test() + { + int offset = 0; + bool actual = Array.Empty().TryReadUInt16(ref offset, out ushort read); + Assert.False(actual); + Assert.Equal(default, read); + } + + [Fact] + public void TryReadUInt16BigEndianTest() + { + int offset = 0; + bool actual = Array.Empty().TryReadUInt16BigEndian(ref offset, out ushort read); + Assert.False(actual); + Assert.Equal(default, read); + } + + [Fact] + public void TryReadUInt16LittleEndianTest() + { + int offset = 0; + bool actual = Array.Empty().TryReadUInt16LittleEndian(ref offset, out ushort read); + Assert.False(actual); + Assert.Equal(default, read); + } + + [Fact] + public void TryReadUInt16BothEndianTest() + { + int offset = 0; + bool actual = Array.Empty().TryReadUInt16BothEndian(ref offset, out BothUInt16 read); + Assert.False(actual); + Assert.Equal(default, read.LittleEndian); + Assert.Equal(default, read.BigEndian); + } + + [Fact] + public void TryReadWORDTest() + { + int offset = 0; + bool actual = Array.Empty().TryReadWORD(ref offset, out ushort read); + Assert.False(actual); + Assert.Equal(default, read); + } + + [Fact] + public void TryReadWORDBigEndianTest() + { + int offset = 0; + bool actual = Array.Empty().TryReadWORDBigEndian(ref offset, out ushort read); + Assert.False(actual); + Assert.Equal(default, read); + } + + [Fact] + public void TryReadWORDLittleEndianTest() + { + int offset = 0; + bool actual = Array.Empty().TryReadWORDLittleEndian(ref offset, out ushort read); + Assert.False(actual); + Assert.Equal(default, read); + } + + [Fact] + public void TryReadWORDBothEndianTest() + { + int offset = 0; + bool actual = Array.Empty().TryReadWORDBothEndian(ref offset, out BothUInt16 read); + Assert.False(actual); + Assert.Equal(default, read.LittleEndian); + Assert.Equal(default, read.BigEndian); + } + + [Fact] + public void TryReadHalfTest() + { + int offset = 0; + bool actual = Array.Empty().TryReadHalf(ref offset, out Half read); + Assert.False(actual); + Assert.Equal(default, read); + } + + [Fact] + public void TryReadHalfBigEndianTest() + { + int offset = 0; + bool actual = Array.Empty().TryReadHalfBigEndian(ref offset, out Half read); + Assert.False(actual); + Assert.Equal(default, read); + } + + [Fact] + public void TryReadHalfLittleEndianTest() + { + int offset = 0; + bool actual = Array.Empty().TryReadHalfLittleEndian(ref offset, out Half read); + Assert.False(actual); + Assert.Equal(default, read); + } + + [Fact] + public void TryReadInt24Test() + { + int offset = 0; + bool actual = Array.Empty().TryReadInt24(ref offset, out Int24 read); + Assert.False(actual); + Assert.Equal(default, (int)read); + } + + [Fact] + public void TryReadInt24BigEndianTest() + { + int offset = 0; + bool actual = Array.Empty().TryReadInt24BigEndian(ref offset, out Int24 read); + Assert.False(actual); + Assert.Equal(default, (int)read); + } + + [Fact] + public void TryReadInt24LittleEndianTest() + { + int offset = 0; + bool actual = Array.Empty().TryReadInt24LittleEndian(ref offset, out Int24 read); + Assert.False(actual); + Assert.Equal(default, (int)read); + } + + [Fact] + public void TryReadUInt24Test() + { + int offset = 0; + bool actual = Array.Empty().TryReadUInt24(ref offset, out UInt24 read); + Assert.False(actual); + Assert.Equal(default, (uint)read); + } + + [Fact] + public void TryReadUInt24BigEndianTest() + { + int offset = 0; + bool actual = Array.Empty().TryReadUInt24BigEndian(ref offset, out UInt24 read); + Assert.False(actual); + Assert.Equal(default, (uint)read); + } + + [Fact] + public void TryReadUInt24LittleEndianTest() + { + int offset = 0; + bool actual = Array.Empty().TryReadUInt24LittleEndian(ref offset, out UInt24 read); + Assert.False(actual); + Assert.Equal(default, (uint)read); + } + + [Fact] + public void TryReadInt32Test() + { + int offset = 0; + bool actual = Array.Empty().TryReadInt32(ref offset, out int read); + Assert.False(actual); + Assert.Equal(default, read); + } + + [Fact] + public void TryReadInt32BigEndianTest() + { + int offset = 0; + bool actual = Array.Empty().TryReadInt32BigEndian(ref offset, out int read); + Assert.False(actual); + Assert.Equal(default, read); + } + + [Fact] + public void TryReadInt32LittleEndianTest() + { + int offset = 0; + bool actual = Array.Empty().TryReadInt32LittleEndian(ref offset, out int read); + Assert.False(actual); + Assert.Equal(default, read); + } + + [Fact] + public void TryReadInt32BothEndianTest() + { + int offset = 0; + bool actual = Array.Empty().TryReadInt32BothEndian(ref offset, out BothInt32 read); + Assert.False(actual); + Assert.Equal(default, read.LittleEndian); + Assert.Equal(default, read.BigEndian); + } + + [Fact] + public void TryReadUInt32Test() + { + int offset = 0; + bool actual = Array.Empty().TryReadUInt32(ref offset, out uint read); + Assert.False(actual); + Assert.Equal(default, read); + } + + [Fact] + public void TryReadUInt32BigEndianTest() + { + int offset = 0; + bool actual = Array.Empty().TryReadUInt32BigEndian(ref offset, out uint read); + Assert.False(actual); + Assert.Equal(default, read); + } + + [Fact] + public void TryReadUInt32LittleEndianTest() + { + int offset = 0; + bool actual = Array.Empty().TryReadUInt32LittleEndian(ref offset, out uint read); + Assert.False(actual); + Assert.Equal(default, read); + } + + [Fact] + public void TryReadUInt32BothEndianTest() + { + int offset = 0; + bool actual = Array.Empty().TryReadUInt32BothEndian(ref offset, out BothUInt32 read); + Assert.False(actual); + Assert.Equal(default, read.LittleEndian); + Assert.Equal(default, read.BigEndian); + } + + [Fact] + public void TryReadDWORDTest() + { + int offset = 0; + bool actual = Array.Empty().TryReadDWORD(ref offset, out uint read); + Assert.False(actual); + Assert.Equal(default, read); + } + + [Fact] + public void TryReadDWORDBigEndianTest() + { + int offset = 0; + bool actual = Array.Empty().TryReadDWORDBigEndian(ref offset, out uint read); + Assert.False(actual); + Assert.Equal(default, read); + } + + [Fact] + public void TryReadDWORDLittleEndianTest() + { + int offset = 0; + bool actual = Array.Empty().TryReadDWORDLittleEndian(ref offset, out uint read); + Assert.False(actual); + Assert.Equal(default, read); + } + + [Fact] + public void TryReadDWORDBothEndianTest() + { + int offset = 0; + bool actual = Array.Empty().TryReadDWORDBothEndian(ref offset, out BothUInt32 read); + Assert.False(actual); + Assert.Equal(default, read.LittleEndian); + Assert.Equal(default, read.BigEndian); + } + + [Fact] + public void TryReadSingleTest() + { + int offset = 0; + bool actual = Array.Empty().TryReadSingle(ref offset, out float read); + Assert.False(actual); + Assert.Equal(default, read); + } + + [Fact] + public void TryReadSingleBigEndianTest() + { + int offset = 0; + bool actual = Array.Empty().TryReadSingleBigEndian(ref offset, out float read); + Assert.False(actual); + Assert.Equal(default, read); + } + + [Fact] + public void TryReadSingleLittleEndianTest() + { + int offset = 0; + bool actual = Array.Empty().TryReadSingleLittleEndian(ref offset, out float read); + Assert.False(actual); + Assert.Equal(default, read); + } + + [Fact] + public void TryReadInt48Test() + { + int offset = 0; + bool actual = Array.Empty().TryReadInt48(ref offset, out Int48 read); + Assert.False(actual); + Assert.Equal(default, (long)read); + } + + [Fact] + public void TryReadInt48BigEndianTest() + { + int offset = 0; + bool actual = Array.Empty().TryReadInt48BigEndian(ref offset, out Int48 read); + Assert.False(actual); + Assert.Equal(default, (long)read); + } + + [Fact] + public void TryReadInt48LittleEndianTest() + { + int offset = 0; + bool actual = Array.Empty().TryReadInt48LittleEndian(ref offset, out Int48 read); + Assert.False(actual); + Assert.Equal(default, (long)read); + } + + [Fact] + public void TryReadUInt48Test() + { + int offset = 0; + bool actual = Array.Empty().TryReadUInt48(ref offset, out UInt48 read); + Assert.False(actual); + Assert.Equal(default, (ulong)read); + } + + [Fact] + public void TryReadUInt48BigEndianTest() + { + int offset = 0; + bool actual = Array.Empty().TryReadUInt48BigEndian(ref offset, out UInt48 read); + Assert.False(actual); + Assert.Equal(default, (ulong)read); + } + + [Fact] + public void TryReadUInt48LittleEndianTest() + { + int offset = 0; + bool actual = Array.Empty().TryReadUInt48LittleEndian(ref offset, out UInt48 read); + Assert.False(actual); + Assert.Equal(default, (ulong)read); + } + + [Fact] + public void TryReadInt64Test() + { + int offset = 0; + bool actual = Array.Empty().TryReadInt64(ref offset, out long read); + Assert.False(actual); + Assert.Equal(default, read); + } + + [Fact] + public void TryReadInt64BigEndianTest() + { + int offset = 0; + bool actual = Array.Empty().TryReadInt64BigEndian(ref offset, out long read); + Assert.False(actual); + Assert.Equal(default, read); + } + + [Fact] + public void TryReadInt64LittleEndianTest() + { + int offset = 0; + bool actual = Array.Empty().TryReadInt64LittleEndian(ref offset, out long read); + Assert.False(actual); + Assert.Equal(default, read); + } + + [Fact] + public void TryReadInt64BothEndianTest() + { + int offset = 0; + bool actual = Array.Empty().TryReadInt64BothEndian(ref offset, out BothInt64 read); + Assert.False(actual); + Assert.Equal(default, read.LittleEndian); + Assert.Equal(default, read.BigEndian); + } + + [Fact] + public void TryReadUInt64Test() + { + int offset = 0; + bool actual = Array.Empty().TryReadUInt64(ref offset, out ulong read); + Assert.False(actual); + Assert.Equal(default, read); + } + + [Fact] + public void TryReadUInt64BigEndianTest() + { + int offset = 0; + bool actual = Array.Empty().TryReadUInt64BigEndian(ref offset, out ulong read); + Assert.False(actual); + Assert.Equal(default, read); + } + + [Fact] + public void TryReadUInt64LittleEndianTest() + { + int offset = 0; + bool actual = Array.Empty().TryReadUInt64LittleEndian(ref offset, out ulong read); + Assert.False(actual); + Assert.Equal(default, read); + } + + [Fact] + public void TryReadUInt64BothEndianTest() + { + int offset = 0; + bool actual = Array.Empty().TryReadUInt64BothEndian(ref offset, out BothUInt64 read); + Assert.False(actual); + Assert.Equal(default, read.LittleEndian); + Assert.Equal(default, read.BigEndian); + } + + [Fact] + public void TryReadQWORDTest() + { + int offset = 0; + bool actual = Array.Empty().TryReadQWORD(ref offset, out ulong read); + Assert.False(actual); + Assert.Equal(default, read); + } + + [Fact] + public void TryReadQWORDBigEndianTest() + { + int offset = 0; + bool actual = Array.Empty().TryReadQWORDBigEndian(ref offset, out ulong read); + Assert.False(actual); + Assert.Equal(default, read); + } + + [Fact] + public void TryReadQWORDLittleEndianTest() + { + int offset = 0; + bool actual = Array.Empty().TryReadQWORDLittleEndian(ref offset, out ulong read); + Assert.False(actual); + Assert.Equal(default, read); + } + + [Fact] + public void TryReadQWORDBothEndianTest() + { + int offset = 0; + bool actual = Array.Empty().TryReadQWORDBothEndian(ref offset, out BothUInt64 read); + Assert.False(actual); + Assert.Equal(default, read.LittleEndian); + Assert.Equal(default, read.BigEndian); + } + + [Fact] + public void TryReadDoubleTest() + { + int offset = 0; + bool actual = Array.Empty().TryReadDouble(ref offset, out double read); + Assert.False(actual); + Assert.Equal(default, read); + } + + [Fact] + public void TryReadDoubleBigEndianTest() + { + int offset = 0; + bool actual = Array.Empty().TryReadDoubleBigEndian(ref offset, out double read); + Assert.False(actual); + Assert.Equal(default, read); + } + + [Fact] + public void TryReadDoubleLittleEndianTest() + { + int offset = 0; + bool actual = Array.Empty().TryReadDoubleLittleEndian(ref offset, out double read); + Assert.False(actual); + Assert.Equal(default, read); + } + + [Fact] + public void TryReadGuidTest() + { + int offset = 0; + bool actual = Array.Empty().TryReadGuid(ref offset, out Guid read); + Assert.False(actual); + Assert.Equal(default, read); + } + + [Fact] + public void TryReadGuidBigEndianTest() + { + int offset = 0; + bool actual = Array.Empty().TryReadGuidBigEndian(ref offset, out Guid read); + Assert.False(actual); + Assert.Equal(default, read); + } + + [Fact] + public void TryReadGuidLittleEndianTest() + { + int offset = 0; + bool actual = Array.Empty().TryReadGuidLittleEndian(ref offset, out Guid read); + Assert.False(actual); + Assert.Equal(default, read); + } + + [Fact] + public void TryReadInt128Test() + { + int offset = 0; + bool actual = Array.Empty().TryReadInt128(ref offset, out Int128 read); + Assert.False(actual); + Assert.Equal(default, read); + } + + [Fact] + public void TryReadInt128BigEndianTest() + { + int offset = 0; + bool actual = Array.Empty().TryReadInt128BigEndian(ref offset, out Int128 read); + Assert.False(actual); + Assert.Equal(default, read); + } + + [Fact] + public void TryReadInt128LittleEndianTest() + { + int offset = 0; + bool actual = Array.Empty().TryReadInt128LittleEndian(ref offset, out Int128 read); + Assert.False(actual); + Assert.Equal(default, read); + } + + [Fact] + public void TryReadUInt128Test() + { + int offset = 0; + bool actual = Array.Empty().TryReadUInt128(ref offset, out UInt128 read); + Assert.False(actual); + Assert.Equal(default, read); + } + + [Fact] + public void TryReadUInt128BigEndianTest() + { + int offset = 0; + bool actual = Array.Empty().TryReadUInt128BigEndian(ref offset, out UInt128 read); + Assert.False(actual); + Assert.Equal(default, read); + } + + [Fact] + public void TryReadUInt128LittleEndianTest() + { + int offset = 0; + bool actual = Array.Empty().TryReadUInt128LittleEndian(ref offset, out UInt128 read); + Assert.False(actual); + Assert.Equal(default, read); + } + + [Fact] + public void TryReadDecimalTest() + { + int offset = 0; + bool actual = Array.Empty().TryReadDecimal(ref offset, out decimal read); + Assert.False(actual); + Assert.Equal(default, read); + } + + [Fact] + public void TryReadDecimalBigEndianTest() + { + int offset = 0; + bool actual = Array.Empty().TryReadDecimalBigEndian(ref offset, out decimal read); + Assert.False(actual); + Assert.Equal(default, read); + } + + [Fact] + public void TryReadDecimalLittleEndianTest() + { + int offset = 0; + bool actual = Array.Empty().TryReadDecimalLittleEndian(ref offset, out decimal read); + Assert.False(actual); + Assert.Equal(default, read); + } + + #endregion + } +} diff --git a/SabreTools.Numerics.Extensions.Test/ByteArrayWriterExtensionsTests.cs b/SabreTools.Numerics.Extensions.Test/ByteArrayWriterExtensionsTests.cs new file mode 100644 index 0000000..7539522 --- /dev/null +++ b/SabreTools.Numerics.Extensions.Test/ByteArrayWriterExtensionsTests.cs @@ -0,0 +1,772 @@ +using System; +using System.Linq; +using System.Numerics; +using System.Text; +using Xunit; + +namespace SabreTools.Numerics.Extensions.Test +{ + public class ByteArrayWriterExtensionsTests + { + /// + /// Test pattern from 0x00-0x0F + /// + private static readonly byte[] _bytes = + [ + 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, + 0x08, 0x09, 0x0A, 0x0B, 0x0C, 0x0D, 0x0E, 0x0F, + ]; + + /// + /// Represents the decimal value 0.0123456789 + /// + private static readonly byte[] _decimalBytes = + [ + 0x15, 0xCD, 0x5B, 0x07, 0x00, 0x00, 0x00, 0x00, + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x0A, 0x00, + ]; + + /// + /// Test pattern for big-endian GUID created from + /// + private static readonly byte[] _guidBigEndianbytes = + [ + 0x03, 0x02, 0x01, 0x00, 0x05, 0x04, 0x07, 0x06, + 0x08, 0x09, 0x0A, 0x0B, 0x0C, 0x0D, 0x0E, 0x0F, + ]; + + [Fact] + public void WriteByteTest() + { + byte[] buffer = new byte[16]; + int offset = 0; + byte[] expected = [.. _bytes.Take(1)]; + bool write = buffer.Write(ref offset, (byte)0x00); + Assert.True(write); + ValidateBytes(expected, buffer); + } + + [Fact] + public void WriteByteBothEndianTest() + { + byte[] buffer = new byte[16]; + int offset = 0; + byte[] expected = [.. _bytes.Take(2)]; + + int readOffset = 0; + buffer.WriteBothEndian(ref offset, _bytes.ReadByteBothEndian(ref readOffset)); + ValidateBytes(expected, buffer); + } + + [Fact] + public void WriteBytesTest() + { + byte[] buffer = new byte[16]; + int offset = 0; + byte[] expected = [.. _bytes.Take(4)]; + bool write = buffer.Write(ref offset, [0x00, 0x01, 0x02, 0x03]); + Assert.True(write); + ValidateBytes(expected, buffer); + } + + [Fact] + public void WriteBytesBigEndianTest() + { + byte[] buffer = new byte[16]; + int offset = 0; + byte[] expected = [.. _bytes.Take(4)]; + bool write = buffer.WriteBigEndian(ref offset, [0x03, 0x02, 0x01, 0x00]); + Assert.True(write); + ValidateBytes(expected, buffer); + } + + [Fact] + public void WriteSByteTest() + { + byte[] buffer = new byte[16]; + int offset = 0; + byte[] expected = [.. _bytes.Take(1)]; + bool write = buffer.Write(ref offset, (sbyte)0x00); + Assert.True(write); + ValidateBytes(expected, buffer); + } + + [Fact] + public void WriteSByteBothEndianTest() + { + byte[] buffer = new byte[16]; + int offset = 0; + byte[] expected = [.. _bytes.Take(2)]; + + int readOffset = 0; + buffer.WriteBothEndian(ref offset, _bytes.ReadSByteBothEndian(ref readOffset)); + ValidateBytes(expected, buffer); + } + + [Fact] + public void WriteCharTest() + { + byte[] buffer = new byte[16]; + int offset = 0; + byte[] expected = [.. _bytes.Take(1)]; + bool write = buffer.Write(ref offset, '\0'); + Assert.True(write); + ValidateBytes(expected, buffer); + } + + [Fact] + public void WriteCharEncodingTest() + { + byte[] buffer = new byte[16]; + int offset = 0; + byte[] expected = [0x00, 0x00]; + bool write = buffer.Write(ref offset, '\0', Encoding.Unicode); + Assert.True(write); + ValidateBytes(expected, buffer); + } + + [Fact] + public void WriteInt16Test() + { + byte[] buffer = new byte[16]; + int offset = 0; + byte[] expected = [.. _bytes.Take(2)]; + bool write = buffer.Write(ref offset, (short)0x0100); + Assert.True(write); + ValidateBytes(expected, buffer); + } + + [Fact] + public void WriteInt16BigEndianTest() + { + byte[] buffer = new byte[16]; + int offset = 0; + byte[] expected = [.. _bytes.Take(2)]; + bool write = buffer.WriteBigEndian(ref offset, (short)0x0001); + Assert.True(write); + ValidateBytes(expected, buffer); + } + + [Fact] + public void WriteInt16LittleEndianTest() + { + byte[] buffer = new byte[16]; + int offset = 0; + byte[] expected = [.. _bytes.Take(2)]; + bool write = buffer.WriteLittleEndian(ref offset, (short)0x0100); + Assert.True(write); + ValidateBytes(expected, buffer); + } + + [Fact] + public void WriteInt16BothEndianTest() + { + byte[] buffer = new byte[16]; + int offset = 0; + byte[] expected = [.. _bytes.Take(4)]; + + int readOffset = 0; + buffer.WriteBothEndian(ref offset, _bytes.ReadInt16BothEndian(ref readOffset)); + ValidateBytes(expected, buffer); + } + + [Fact] + public void WriteUInt16Test() + { + byte[] buffer = new byte[16]; + int offset = 0; + byte[] expected = [.. _bytes.Take(2)]; + bool write = buffer.Write(ref offset, (ushort)0x0100); + Assert.True(write); + ValidateBytes(expected, buffer); + } + + [Fact] + public void WriteUInt16BigEndianTest() + { + byte[] buffer = new byte[16]; + int offset = 0; + byte[] expected = [.. _bytes.Take(2)]; + bool write = buffer.WriteBigEndian(ref offset, (ushort)0x0001); + Assert.True(write); + ValidateBytes(expected, buffer); + } + + [Fact] + public void WriteUInt16LittleEndianTest() + { + byte[] buffer = new byte[16]; + int offset = 0; + byte[] expected = [.. _bytes.Take(2)]; + bool write = buffer.WriteLittleEndian(ref offset, (ushort)0x0100); + Assert.True(write); + ValidateBytes(expected, buffer); + } + + [Fact] + public void WriteUInt16BothEndianTest() + { + byte[] buffer = new byte[16]; + int offset = 0; + byte[] expected = [.. _bytes.Take(4)]; + + int readOffset = 0; + buffer.WriteBothEndian(ref offset, _bytes.ReadUInt16BothEndian(ref readOffset)); + ValidateBytes(expected, buffer); + } + + [Fact] + public void WriteHalfTest() + { + byte[] buffer = new byte[16]; + int offset = 0; + byte[] expected = [.. _bytes.Take(2)]; + bool write = buffer.Write(ref offset, BitConverter.Int16BitsToHalf(0x0100)); + Assert.True(write); + ValidateBytes(expected, buffer); + } + + [Fact] + public void WriteHalfBigEndianTest() + { + byte[] buffer = new byte[16]; + int offset = 0; + byte[] expected = [.. _bytes.Take(2)]; + bool write = buffer.WriteBigEndian(ref offset, BitConverter.Int16BitsToHalf(0x0001)); + Assert.True(write); + ValidateBytes(expected, buffer); + } + + [Fact] + public void WriteHalfLittleEndianTest() + { + byte[] buffer = new byte[16]; + int offset = 0; + byte[] expected = [.. _bytes.Take(2)]; + bool write = buffer.WriteLittleEndian(ref offset, BitConverter.Int16BitsToHalf(0x0100)); + Assert.True(write); + ValidateBytes(expected, buffer); + } + + [Fact] + public void WriteInt24Test() + { + byte[] buffer = new byte[16]; + int offset = 0; + byte[] expected = [.. _bytes.Take(3)]; + bool write = buffer.Write(ref offset, (Int24)0x020100); + Assert.True(write); + ValidateBytes(expected, buffer); + } + + [Fact] + public void WriteInt24BigEndianTest() + { + byte[] buffer = new byte[16]; + int offset = 0; + byte[] expected = [.. _bytes.Take(3)]; + bool write = buffer.WriteBigEndian(ref offset, (Int24)0x000102); + Assert.True(write); + ValidateBytes(expected, buffer); + } + + [Fact] + public void WriteInt24LittleEndianTest() + { + byte[] buffer = new byte[16]; + int offset = 0; + byte[] expected = [.. _bytes.Take(3)]; + bool write = buffer.WriteLittleEndian(ref offset, (Int24)0x020100); + Assert.True(write); + ValidateBytes(expected, buffer); + } + + [Fact] + public void WriteUInt24Test() + { + byte[] buffer = new byte[16]; + int offset = 0; + byte[] expected = [.. _bytes.Take(3)]; + bool write = buffer.Write(ref offset, (UInt24)0x020100); + Assert.True(write); + ValidateBytes(expected, buffer); + } + + [Fact] + public void WriteUInt24BigEndianTest() + { + byte[] buffer = new byte[16]; + int offset = 0; + byte[] expected = [.. _bytes.Take(3)]; + bool write = buffer.WriteBigEndian(ref offset, (UInt24)0x000102); + Assert.True(write); + ValidateBytes(expected, buffer); + } + + [Fact] + public void WriteUInt24LittleEndianTest() + { + byte[] buffer = new byte[16]; + int offset = 0; + byte[] expected = [.. _bytes.Take(3)]; + bool write = buffer.WriteLittleEndian(ref offset, (UInt24)0x020100); + Assert.True(write); + ValidateBytes(expected, buffer); + } + + [Fact] + public void WriteInt32Test() + { + byte[] buffer = new byte[16]; + int offset = 0; + byte[] expected = [.. _bytes.Take(4)]; + bool write = buffer.Write(ref offset, 0x03020100); + Assert.True(write); + ValidateBytes(expected, buffer); + } + + [Fact] + public void WriteInt32BigEndianTest() + { + byte[] buffer = new byte[16]; + int offset = 0; + byte[] expected = [.. _bytes.Take(4)]; + bool write = buffer.WriteBigEndian(ref offset, 0x00010203); + Assert.True(write); + ValidateBytes(expected, buffer); + } + + [Fact] + public void WriteInt32LittleEndianTest() + { + byte[] buffer = new byte[16]; + int offset = 0; + byte[] expected = [.. _bytes.Take(4)]; + bool write = buffer.WriteLittleEndian(ref offset, 0x03020100); + Assert.True(write); + ValidateBytes(expected, buffer); + } + + [Fact] + public void WriteInt32BothEndianTest() + { + byte[] buffer = new byte[16]; + int offset = 0; + byte[] expected = [.. _bytes.Take(8)]; + + int readOffset = 0; + buffer.WriteBothEndian(ref offset, _bytes.ReadInt32BothEndian(ref readOffset)); + ValidateBytes(expected, buffer); + } + + [Fact] + public void WriteUInt32Test() + { + byte[] buffer = new byte[16]; + int offset = 0; + byte[] expected = [.. _bytes.Take(4)]; + bool write = buffer.Write(ref offset, (uint)0x03020100); + Assert.True(write); + ValidateBytes(expected, buffer); + } + + [Fact] + public void WriteUInt32BigEndianTest() + { + byte[] buffer = new byte[16]; + int offset = 0; + byte[] expected = [.. _bytes.Take(4)]; + bool write = buffer.WriteBigEndian(ref offset, (uint)0x00010203); + Assert.True(write); + ValidateBytes(expected, buffer); + } + + [Fact] + public void WriteUInt32LittleEndianTest() + { + byte[] buffer = new byte[16]; + int offset = 0; + byte[] expected = [.. _bytes.Take(4)]; + bool write = buffer.WriteLittleEndian(ref offset, (uint)0x03020100); + Assert.True(write); + ValidateBytes(expected, buffer); + } + + [Fact] + public void WriteUInt32BothEndianTest() + { + byte[] buffer = new byte[16]; + int offset = 0; + byte[] expected = [.. _bytes.Take(8)]; + + int readOffset = 0; + buffer.WriteBothEndian(ref offset, _bytes.ReadUInt32BothEndian(ref readOffset)); + ValidateBytes(expected, buffer); + } + + [Fact] + public void WriteSingleTest() + { + byte[] buffer = new byte[16]; + int offset = 0; + byte[] expected = [.. _bytes.Take(4)]; + bool write = buffer.Write(ref offset, BitConverter.Int32BitsToSingle(0x03020100)); + Assert.True(write); + ValidateBytes(expected, buffer); + } + + [Fact] + public void WriteSingleBigEndianTest() + { + byte[] buffer = new byte[16]; + int offset = 0; + byte[] expected = [.. _bytes.Take(4)]; + bool write = buffer.WriteBigEndian(ref offset, BitConverter.Int32BitsToSingle(0x00010203)); + Assert.True(write); + ValidateBytes(expected, buffer); + } + + [Fact] + public void WriteSingleLittleEndianTest() + { + byte[] buffer = new byte[16]; + int offset = 0; + byte[] expected = [.. _bytes.Take(4)]; + bool write = buffer.WriteLittleEndian(ref offset, BitConverter.Int32BitsToSingle(0x03020100)); + Assert.True(write); + ValidateBytes(expected, buffer); + } + + [Fact] + public void WriteInt48Test() + { + byte[] buffer = new byte[16]; + int offset = 0; + byte[] expected = [.. _bytes.Take(6)]; + bool write = buffer.Write(ref offset, (Int48)0x050403020100); + Assert.True(write); + ValidateBytes(expected, buffer); + } + + [Fact] + public void WriteInt48BigEndianTest() + { + byte[] buffer = new byte[16]; + int offset = 0; + byte[] expected = [.. _bytes.Take(6)]; + bool write = buffer.WriteBigEndian(ref offset, (Int48)0x000102030405); + Assert.True(write); + ValidateBytes(expected, buffer); + } + + [Fact] + public void WriteInt48LittleEndianTest() + { + byte[] buffer = new byte[16]; + int offset = 0; + byte[] expected = [.. _bytes.Take(6)]; + bool write = buffer.WriteLittleEndian(ref offset, (Int48)0x050403020100); + Assert.True(write); + ValidateBytes(expected, buffer); + } + + [Fact] + public void WriteUInt48Test() + { + byte[] buffer = new byte[16]; + int offset = 0; + byte[] expected = [.. _bytes.Take(6)]; + bool write = buffer.Write(ref offset, (UInt48)0x050403020100); + Assert.True(write); + ValidateBytes(expected, buffer); + } + + [Fact] + public void WriteUInt48BigEndianTest() + { + byte[] buffer = new byte[16]; + int offset = 0; + byte[] expected = [.. _bytes.Take(6)]; + bool write = buffer.WriteBigEndian(ref offset, (UInt48)0x000102030405); + Assert.True(write); + ValidateBytes(expected, buffer); + } + + [Fact] + public void WriteUInt48LittleEndianTest() + { + byte[] buffer = new byte[16]; + int offset = 0; + byte[] expected = [.. _bytes.Take(6)]; + bool write = buffer.WriteLittleEndian(ref offset, (UInt48)0x050403020100); + Assert.True(write); + ValidateBytes(expected, buffer); + } + + [Fact] + public void WriteInt64Test() + { + byte[] buffer = new byte[16]; + int offset = 0; + byte[] expected = [.. _bytes.Take(8)]; + bool write = buffer.Write(ref offset, 0x0706050403020100); + Assert.True(write); + ValidateBytes(expected, buffer); + } + + [Fact] + public void WriteInt64BigEndianTest() + { + byte[] buffer = new byte[16]; + int offset = 0; + byte[] expected = [.. _bytes.Take(8)]; + bool write = buffer.WriteBigEndian(ref offset, 0x0001020304050607); + Assert.True(write); + ValidateBytes(expected, buffer); + } + + [Fact] + public void WriteInt64LittleEndianTest() + { + byte[] buffer = new byte[16]; + int offset = 0; + byte[] expected = [.. _bytes.Take(8)]; + bool write = buffer.WriteLittleEndian(ref offset, 0x0706050403020100); + Assert.True(write); + ValidateBytes(expected, buffer); + } + + [Fact] + public void WriteInt64BothEndianTest() + { + byte[] buffer = new byte[16]; + int offset = 0; + byte[] expected = [.. _bytes.Take(16)]; + + int readOffset = 0; + buffer.WriteBothEndian(ref offset, _bytes.ReadInt64BothEndian(ref readOffset)); + ValidateBytes(expected, buffer); + } + + [Fact] + public void WriteUInt64Test() + { + byte[] buffer = new byte[16]; + int offset = 0; + byte[] expected = [.. _bytes.Take(8)]; + bool write = buffer.Write(ref offset, (ulong)0x0706050403020100); + Assert.True(write); + ValidateBytes(expected, buffer); + } + + [Fact] + public void WriteUInt64BigEndianTest() + { + byte[] buffer = new byte[16]; + int offset = 0; + byte[] expected = [.. _bytes.Take(8)]; + bool write = buffer.WriteBigEndian(ref offset, (ulong)0x0001020304050607); + Assert.True(write); + ValidateBytes(expected, buffer); + } + + [Fact] + public void WriteUInt64LittleEndianTest() + { + byte[] buffer = new byte[16]; + int offset = 0; + byte[] expected = [.. _bytes.Take(8)]; + bool write = buffer.WriteLittleEndian(ref offset, (ulong)0x0706050403020100); + Assert.True(write); + ValidateBytes(expected, buffer); + } + + [Fact] + public void WriteUInt64BothEndianTest() + { + byte[] buffer = new byte[16]; + int offset = 0; + byte[] expected = [.. _bytes.Take(16)]; + + int readOffset = 0; + buffer.WriteBothEndian(ref offset, _bytes.ReadUInt64BothEndian(ref readOffset)); + ValidateBytes(expected, buffer); + } + + [Fact] + public void WriteDoubleTest() + { + byte[] buffer = new byte[16]; + int offset = 0; + byte[] expected = [.. _bytes.Take(8)]; + bool write = buffer.Write(ref offset, BitConverter.Int64BitsToDouble(0x0706050403020100)); + Assert.True(write); + ValidateBytes(expected, buffer); + } + + [Fact] + public void WriteDoubleBigEndianTest() + { + byte[] buffer = new byte[16]; + int offset = 0; + byte[] expected = [.. _bytes.Take(8)]; + bool write = buffer.WriteBigEndian(ref offset, BitConverter.Int64BitsToDouble(0x0001020304050607)); + Assert.True(write); + ValidateBytes(expected, buffer); + } + + [Fact] + public void WriteDoubleLittleEndianTest() + { + byte[] buffer = new byte[16]; + int offset = 0; + byte[] expected = [.. _bytes.Take(8)]; + bool write = buffer.WriteLittleEndian(ref offset, BitConverter.Int64BitsToDouble(0x0706050403020100)); + Assert.True(write); + ValidateBytes(expected, buffer); + } + + [Fact] + public void WriteGuidTest() + { + byte[] buffer = new byte[16]; + int offset = 0; + byte[] expected = [.. _bytes.Take(16)]; + bool write = buffer.Write(ref offset, new Guid(_bytes)); + Assert.True(write); + ValidateBytes(expected, buffer); + } + + [Fact] + public void WriteGuidBigEndianTest() + { + byte[] buffer = new byte[16]; + int offset = 0; + byte[] expected = [.. _guidBigEndianbytes.Take(16)]; + bool write = buffer.WriteBigEndian(ref offset, new Guid(_bytes)); + Assert.True(write); + ValidateBytes(expected, buffer); + } + + [Fact] + public void WriteGuidLittleEndianTest() + { + byte[] buffer = new byte[16]; + int offset = 0; + byte[] expected = [.. _bytes.Take(16)]; + bool write = buffer.WriteLittleEndian(ref offset, new Guid(_bytes)); + Assert.True(write); + ValidateBytes(expected, buffer); + } + + [Fact] + public void WriteInt128Test() + { + byte[] buffer = new byte[16]; + int offset = 0; + byte[] expected = [.. _bytes.Take(16)]; + bool write = buffer.Write(ref offset, (Int128)new BigInteger(_bytes)); + Assert.True(write); + ValidateBytes(expected, buffer); + } + + [Fact] + public void WriteInt128BigEndianTest() + { + byte[] buffer = new byte[16]; + int offset = 0; + byte[] expected = [.. _bytes.Take(16)]; + bool write = buffer.WriteBigEndian(ref offset, (Int128)new BigInteger(Enumerable.Reverse(_bytes).ToArray())); + Assert.True(write); + ValidateBytes(expected, buffer); + } + + [Fact] + public void WriteInt128LittleEndianTest() + { + byte[] buffer = new byte[16]; + int offset = 0; + byte[] expected = [.. _bytes.Take(16)]; + bool write = buffer.WriteLittleEndian(ref offset, (Int128)new BigInteger(_bytes)); + Assert.True(write); + ValidateBytes(expected, buffer); + } + + [Fact] + public void WriteUInt128Test() + { + byte[] buffer = new byte[16]; + int offset = 0; + byte[] expected = [.. _bytes.Take(16)]; + bool write = buffer.Write(ref offset, (UInt128)new BigInteger(_bytes)); + Assert.True(write); + ValidateBytes(expected, buffer); + } + + [Fact] + public void WriteUInt128BigEndianTest() + { + byte[] buffer = new byte[16]; + int offset = 0; + byte[] expected = [.. _bytes.Take(16)]; + bool write = buffer.WriteBigEndian(ref offset, (UInt128)new BigInteger(Enumerable.Reverse(_bytes).ToArray())); + Assert.True(write); + ValidateBytes(expected, buffer); + } + + [Fact] + public void WriteUInt128LittleEndianTest() + { + byte[] buffer = new byte[16]; + int offset = 0; + byte[] expected = [.. _bytes.Take(16)]; + bool write = buffer.WriteLittleEndian(ref offset, (UInt128)new BigInteger(_bytes)); + Assert.True(write); + ValidateBytes(expected, buffer); + } + + [Fact] + public void WriteDecimalTest() + { + byte[] buffer = new byte[16]; + int offset = 0; + byte[] expected = [.. _decimalBytes.Take(16)]; + bool write = buffer.Write(ref offset, 0.0123456789M); + Assert.True(write); + ValidateBytes(expected, buffer); + } + + [Fact] + public void WriteDecimalBigEndianTest() + { + byte[] buffer = new byte[16]; + int offset = 0; + byte[] expected = [.. _decimalBytes.Take(16).Reverse()]; + bool write = buffer.WriteBigEndian(ref offset, 0.0123456789M); + Assert.True(write); + ValidateBytes(expected, buffer); + } + + [Fact] + public void WriteDecimalLittleEndianTest() + { + byte[] buffer = new byte[16]; + int offset = 0; + byte[] expected = [.. _decimalBytes.Take(16)]; + bool write = buffer.WriteLittleEndian(ref offset, 0.0123456789M); + Assert.True(write); + ValidateBytes(expected, buffer); + } + + /// + /// Validate that a set of actual bytes matches the expected bytes + /// + private static void ValidateBytes(byte[] expected, byte[] actual) + { + for (int i = 0; i < expected.Length; i++) + { + Assert.Equal(expected[i], actual[i]); + } + } + } +} diff --git a/SabreTools.Numerics.Extensions.Test/SabreTools.Numerics.Extensions.Test.csproj b/SabreTools.Numerics.Extensions.Test/SabreTools.Numerics.Extensions.Test.csproj new file mode 100644 index 0000000..e50c553 --- /dev/null +++ b/SabreTools.Numerics.Extensions.Test/SabreTools.Numerics.Extensions.Test.csproj @@ -0,0 +1,23 @@ + + + + net8.0;net9.0;net10.0 + false + latest + enable + true + CS0618 + + + + + + + + + + + + + + \ No newline at end of file diff --git a/SabreTools.Numerics.Extensions.Test/StreamReaderExtensionsTests.cs b/SabreTools.Numerics.Extensions.Test/StreamReaderExtensionsTests.cs new file mode 100644 index 0000000..09196b5 --- /dev/null +++ b/SabreTools.Numerics.Extensions.Test/StreamReaderExtensionsTests.cs @@ -0,0 +1,2094 @@ +using System; +using System.IO; +using System.Linq; +using System.Numerics; +using Xunit; + +namespace SabreTools.Numerics.Extensions.Test +{ + public class StreamReaderExtensionsTests + { + /// + /// Test pattern from 0x00-0x0F + /// + private static readonly byte[] _bytes = + [ + 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, + 0x08, 0x09, 0x0A, 0x0B, 0x0C, 0x0D, 0x0E, 0x0F, + ]; + + /// + /// Represents the decimal value 0.0123456789 + /// + private static readonly byte[] _decimalBytes = + [ + 0x15, 0xCD, 0x5B, 0x07, 0x00, 0x00, 0x00, 0x00, + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x0A, 0x00, + ]; + + /// + /// Test pattern for big-endian GUID created from + /// + private static readonly byte[] _guidBigEndianbytes = + [ + 0x03, 0x02, 0x01, 0x00, 0x05, 0x04, 0x07, 0x06, + 0x08, 0x09, 0x0A, 0x0B, 0x0C, 0x0D, 0x0E, 0x0F, + ]; + + #region Exact Read + + [Fact] + public void ReadByteArrayTest() + { + byte[] arr = new byte[4]; + var stream = new MemoryStream(_bytes); + int read = stream.Read(arr, 0, 4); + Assert.Equal(4, read); + Assert.True(arr.SequenceEqual(_bytes.Take(4))); + } + + [Fact] + public void ReadByteValueTest() + { + var stream = new MemoryStream(_bytes); + byte read = stream.ReadByteValue(); + Assert.Equal(0x00, read); + } + + [Fact] + public void ReadBytesTest() + { + var stream = new MemoryStream(_bytes); + int length = 4; + byte[] read = stream.ReadBytes(length); + Assert.Equal(length, read.Length); + Assert.True(read.SequenceEqual(_bytes.Take(length))); + } + + [Fact] + public void ReadByteBothEndianTest() + { + var stream = new MemoryStream(_bytes); + BothUInt8 read = stream.ReadByteBothEndian(); + Assert.Equal(0x00, read.LittleEndian); + Assert.Equal(0x01, read.BigEndian); + } + + [Fact] + public void ReadSByteTest() + { + var stream = new MemoryStream(_bytes); + sbyte read = stream.ReadSByte(); + Assert.Equal(0x00, read); + } + + [Fact] + public void ReadSByteBothEndianTest() + { + var stream = new MemoryStream(_bytes); + BothInt8 read = stream.ReadSByteBothEndian(); + Assert.Equal(0x00, read.LittleEndian); + Assert.Equal(0x01, read.BigEndian); + } + + [Fact] + public void ReadCharTest() + { + var stream = new MemoryStream(_bytes); + char read = stream.ReadChar(); + Assert.Equal('\0', read); + } + + [Fact] + public void ReadInt16Test() + { + var stream = new MemoryStream(_bytes); + short read = stream.ReadInt16(); + Assert.Equal(0x0100, read); + } + + [Fact] + public void ReadInt16BigEndianTest() + { + var stream = new MemoryStream(_bytes); + short read = stream.ReadInt16BigEndian(); + Assert.Equal(0x0001, read); + } + + [Fact] + public void ReadInt16LittleEndianTest() + { + var stream = new MemoryStream(_bytes); + short read = stream.ReadInt16LittleEndian(); + Assert.Equal(0x0100, read); + } + + [Fact] + public void ReadInt16BothEndianTest() + { + var stream = new MemoryStream(_bytes); + BothInt16 read = stream.ReadInt16BothEndian(); + Assert.Equal(0x0100, read.LittleEndian); + Assert.Equal(0x0203, read.BigEndian); + } + + [Fact] + public void ReadUInt16Test() + { + var stream = new MemoryStream(_bytes); + ushort read = stream.ReadUInt16(); + Assert.Equal(0x0100, read); + } + + [Fact] + public void ReadUInt16BigEndianTest() + { + var stream = new MemoryStream(_bytes); + ushort read = stream.ReadUInt16BigEndian(); + Assert.Equal(0x0001, read); + } + + [Fact] + public void ReadUInt16LittleEndianTest() + { + var stream = new MemoryStream(_bytes); + ushort read = stream.ReadUInt16LittleEndian(); + Assert.Equal(0x0100, read); + } + + [Fact] + public void ReadUInt16BothEndianTest() + { + var stream = new MemoryStream(_bytes); + BothUInt16 read = stream.ReadUInt16BothEndian(); + Assert.Equal(0x0100, read.LittleEndian); + Assert.Equal(0x0203, read.BigEndian); + } + + [Fact] + public void ReadWORDTest() + { + var stream = new MemoryStream(_bytes); + ushort read = stream.ReadWORD(); + Assert.Equal(0x0100, read); + } + + [Fact] + public void ReadWORDBigEndianTest() + { + var stream = new MemoryStream(_bytes); + ushort read = stream.ReadWORDBigEndian(); + Assert.Equal(0x0001, read); + } + + [Fact] + public void ReadWORDLittleEndianTest() + { + var stream = new MemoryStream(_bytes); + ushort read = stream.ReadWORDLittleEndian(); + Assert.Equal(0x0100, read); + } + + [Fact] + public void ReadWORDBothEndianTest() + { + var stream = new MemoryStream(_bytes); + BothUInt16 read = stream.ReadWORDBothEndian(); + Assert.Equal(0x0100, read.LittleEndian); + Assert.Equal(0x0203, read.BigEndian); + } + + [Fact] + public void ReadHalfTest() + { + var stream = new MemoryStream(_bytes); + Half expected = BitConverter.Int16BitsToHalf(0x0100); + Half read = stream.ReadHalf(); + Assert.Equal(expected, read); + } + + [Fact] + public void ReadHalfBigEndianTest() + { + var stream = new MemoryStream(_bytes); + Half expected = BitConverter.Int16BitsToHalf(0x0001); + Half read = stream.ReadHalfBigEndian(); + Assert.Equal(expected, read); + } + + [Fact] + public void ReadHalfLittleEndianTest() + { + var stream = new MemoryStream(_bytes); + Half expected = BitConverter.Int16BitsToHalf(0x0100); + Half read = stream.ReadHalfLittleEndian(); + Assert.Equal(expected, read); + } + + [Fact] + public void ReadInt24Test() + { + var stream = new MemoryStream(_bytes); + Int24 read = stream.ReadInt24(); + Assert.Equal(0x020100, (int)read); + } + + [Fact] + public void ReadInt24BigEndianTest() + { + var stream = new MemoryStream(_bytes); + Int24 read = stream.ReadInt24BigEndian(); + Assert.Equal(0x000102, (int)read); + } + + [Fact] + public void ReadInt24LittleEndianTest() + { + var stream = new MemoryStream(_bytes); + Int24 read = stream.ReadInt24LittleEndian(); + Assert.Equal(0x020100, (int)read); + } + + [Fact] + public void ReadUInt24Test() + { + var stream = new MemoryStream(_bytes); + UInt24 read = stream.ReadUInt24(); + Assert.Equal((uint)0x020100, (uint)read); + } + + [Fact] + public void ReadUInt24BigEndianTest() + { + var stream = new MemoryStream(_bytes); + UInt24 read = stream.ReadUInt24BigEndian(); + Assert.Equal((uint)0x000102, (uint)read); + } + + [Fact] + public void ReadUInt24LittleEndianTest() + { + var stream = new MemoryStream(_bytes); + UInt24 read = stream.ReadUInt24LittleEndian(); + Assert.Equal((uint)0x020100, (uint)read); + } + + [Fact] + public void ReadInt32Test() + { + var stream = new MemoryStream(_bytes); + int read = stream.ReadInt32(); + Assert.Equal(0x03020100, read); + } + + [Fact] + public void ReadInt32BigEndianTest() + { + var stream = new MemoryStream(_bytes); + int read = stream.ReadInt32BigEndian(); + Assert.Equal(0x00010203, read); + } + + [Fact] + public void ReadInt32LittleEndianTest() + { + var stream = new MemoryStream(_bytes); + int read = stream.ReadInt32LittleEndian(); + Assert.Equal(0x03020100, read); + } + + [Fact] + public void ReadInt32BothEndianTest() + { + var stream = new MemoryStream(_bytes); + BothInt32 read = stream.ReadInt32BothEndian(); + Assert.Equal(0x03020100, read.LittleEndian); + Assert.Equal(0x04050607, read.BigEndian); + } + + [Fact] + public void ReadUInt32Test() + { + var stream = new MemoryStream(_bytes); + uint read = stream.ReadUInt32(); + Assert.Equal((uint)0x03020100, read); + } + + [Fact] + public void ReadUInt32BigEndianTest() + { + var stream = new MemoryStream(_bytes); + uint read = stream.ReadUInt32BigEndian(); + Assert.Equal((uint)0x00010203, read); + } + + [Fact] + public void ReadUInt32LittleEndianTest() + { + var stream = new MemoryStream(_bytes); + uint read = stream.ReadUInt32LittleEndian(); + Assert.Equal((uint)0x03020100, read); + } + + [Fact] + public void ReadUInt32BothEndianTest() + { + var stream = new MemoryStream(_bytes); + BothUInt32 read = stream.ReadUInt32BothEndian(); + Assert.Equal((uint)0x03020100, read.LittleEndian); + Assert.Equal((uint)0x04050607, read.BigEndian); + } + + [Fact] + public void ReadDWORDTest() + { + var stream = new MemoryStream(_bytes); + uint read = stream.ReadDWORD(); + Assert.Equal((uint)0x03020100, read); + } + + [Fact] + public void ReadDWORDBigEndianTest() + { + var stream = new MemoryStream(_bytes); + uint read = stream.ReadDWORDBigEndian(); + Assert.Equal((uint)0x00010203, read); + } + + [Fact] + public void ReadDWORDLittleEndianTest() + { + var stream = new MemoryStream(_bytes); + uint read = stream.ReadDWORDLittleEndian(); + Assert.Equal((uint)0x03020100, read); + } + + [Fact] + public void ReadDWORDBothEndianTest() + { + var stream = new MemoryStream(_bytes); + BothUInt32 read = stream.ReadDWORDBothEndian(); + Assert.Equal((uint)0x03020100, read.LittleEndian); + Assert.Equal((uint)0x04050607, read.BigEndian); + } + + [Fact] + public void ReadSingleTest() + { + var stream = new MemoryStream(_bytes); + float expected = BitConverter.Int32BitsToSingle(0x03020100); + float read = stream.ReadSingle(); + Assert.Equal(expected, read); + } + + [Fact] + public void ReadSingleBigEndianTest() + { + var stream = new MemoryStream(_bytes); + float expected = BitConverter.Int32BitsToSingle(0x00010203); + float read = stream.ReadSingleBigEndian(); + Assert.Equal(expected, read); + } + + [Fact] + public void ReadSingleLittleEndianTest() + { + var stream = new MemoryStream(_bytes); + float expected = BitConverter.Int32BitsToSingle(0x03020100); + float read = stream.ReadSingleLittleEndian(); + Assert.Equal(expected, read); + } + + [Fact] + public void ReadInt48Test() + { + MemoryStream? stream = new MemoryStream(_bytes); + Int48 read = stream.ReadInt48(); + Assert.Equal(0x050403020100, (long)read); + } + + [Fact] + public void ReadInt48BigEndianTest() + { + var stream = new MemoryStream(_bytes); + Int48 read = stream.ReadInt48BigEndian(); + Assert.Equal(0x000102030405, (long)read); + } + + [Fact] + public void ReadInt48LittleEndianTest() + { + var stream = new MemoryStream(_bytes); + Int48 read = stream.ReadInt48LittleEndian(); + Assert.Equal(0x050403020100, (long)read); + } + + [Fact] + public void ReadUInt48Test() + { + var stream = new MemoryStream(_bytes); + UInt48 read = stream.ReadUInt48(); + Assert.Equal((ulong)0x050403020100, (ulong)read); + } + + [Fact] + public void ReadUInt48BigEndianTest() + { + var stream = new MemoryStream(_bytes); + UInt48 read = stream.ReadUInt48BigEndian(); + Assert.Equal((ulong)0x000102030405, (ulong)read); + } + + [Fact] + public void ReadUInt48LittleEndianTest() + { + var stream = new MemoryStream(_bytes); + UInt48 read = stream.ReadUInt48LittleEndian(); + Assert.Equal((ulong)0x050403020100, (ulong)read); + } + + [Fact] + public void ReadInt64Test() + { + var stream = new MemoryStream(_bytes); + long read = stream.ReadInt64(); + Assert.Equal(0x0706050403020100, read); + } + + [Fact] + public void ReadInt64BigEndianTest() + { + var stream = new MemoryStream(_bytes); + long read = stream.ReadInt64BigEndian(); + Assert.Equal(0x0001020304050607, read); + } + + [Fact] + public void ReadInt64LittleEndianTest() + { + var stream = new MemoryStream(_bytes); + long read = stream.ReadInt64LittleEndian(); + Assert.Equal(0x0706050403020100, read); + } + + [Fact] + public void ReadInt64BothEndianTest() + { + var stream = new MemoryStream(_bytes); + BothInt64 read = stream.ReadInt64BothEndian(); + Assert.Equal(0x0706050403020100, read.LittleEndian); + Assert.Equal(0x08090A0B0C0D0E0F, read.BigEndian); + } + + [Fact] + public void ReadUInt64Test() + { + var stream = new MemoryStream(_bytes); + ulong read = stream.ReadUInt64(); + Assert.Equal((ulong)0x0706050403020100, read); + } + + [Fact] + public void ReadUInt64BigEndianTest() + { + var stream = new MemoryStream(_bytes); + ulong read = stream.ReadUInt64BigEndian(); + Assert.Equal((ulong)0x0001020304050607, read); + } + + [Fact] + public void ReadUInt64LittleEndianTest() + { + var stream = new MemoryStream(_bytes); + ulong read = stream.ReadUInt64LittleEndian(); + Assert.Equal((ulong)0x0706050403020100, read); + } + + [Fact] + public void ReadUInt64BothEndianTest() + { + var stream = new MemoryStream(_bytes); + BothUInt64 read = stream.ReadUInt64BothEndian(); + Assert.Equal((ulong)0x0706050403020100, read.LittleEndian); + Assert.Equal((ulong)0x08090A0B0C0D0E0F, read.BigEndian); + } + + [Fact] + public void ReadQWORDTest() + { + var stream = new MemoryStream(_bytes); + ulong read = stream.ReadQWORD(); + Assert.Equal((ulong)0x0706050403020100, read); + } + + [Fact] + public void ReadQWORDBigEndianTest() + { + var stream = new MemoryStream(_bytes); + ulong read = stream.ReadQWORDBigEndian(); + Assert.Equal((ulong)0x0001020304050607, read); + } + + [Fact] + public void ReadQWORDLittleEndianTest() + { + var stream = new MemoryStream(_bytes); + ulong read = stream.ReadQWORDLittleEndian(); + Assert.Equal((ulong)0x0706050403020100, read); + } + + [Fact] + public void ReadQWORDBothEndianTest() + { + var stream = new MemoryStream(_bytes); + BothUInt64 read = stream.ReadQWORDBothEndian(); + Assert.Equal((ulong)0x0706050403020100, read.LittleEndian); + Assert.Equal((ulong)0x08090A0B0C0D0E0F, read.BigEndian); + } + + [Fact] + public void ReadDoubleTest() + { + var stream = new MemoryStream(_bytes); + double expected = BitConverter.Int64BitsToDouble(0x0706050403020100); + double read = stream.ReadDouble(); + Assert.Equal(expected, read); + } + + [Fact] + public void ReadDoubleBigEndianTest() + { + var stream = new MemoryStream(_bytes); + double expected = BitConverter.Int64BitsToDouble(0x0001020304050607); + double read = stream.ReadDoubleBigEndian(); + Assert.Equal(expected, read); + } + + [Fact] + public void ReadDoubleLittleEndianTest() + { + var stream = new MemoryStream(_bytes); + double expected = BitConverter.Int64BitsToDouble(0x0706050403020100); + double read = stream.ReadDoubleLittleEndian(); + Assert.Equal(expected, read); + } + + [Fact] + public void ReadGuidTest() + { + var stream = new MemoryStream(_bytes); + var expected = new Guid(_bytes); + Guid read = stream.ReadGuid(); + Assert.Equal(expected, read); + } + + [Fact] + public void ReadGuidBigEndianTest() + { + var stream = new MemoryStream(_bytes); + var expected = new Guid(_guidBigEndianbytes); + Guid read = stream.ReadGuidBigEndian(); + Assert.Equal(expected, read); + } + + [Fact] + public void ReadGuidLittleEndianTest() + { + var stream = new MemoryStream(_bytes); + var expected = new Guid(_bytes); + Guid read = stream.ReadGuidLittleEndian(); + Assert.Equal(expected, read); + } + + [Fact] + public void ReadInt128Test() + { + var stream = new MemoryStream(_bytes); + var expected = (Int128)new BigInteger(_bytes); + Int128 read = stream.ReadInt128(); + Assert.Equal(expected, read); + } + + [Fact] + public void ReadInt128BigEndianTest() + { + var stream = new MemoryStream(_bytes); + var reversed = Enumerable.Reverse(_bytes).ToArray(); + var expected = (Int128)new BigInteger(reversed); + Int128 read = stream.ReadInt128BigEndian(); + Assert.Equal(expected, read); + } + + [Fact] + public void ReadInt128LittleEndianTest() + { + var stream = new MemoryStream(_bytes); + var expected = (Int128)new BigInteger(_bytes); + Int128 read = stream.ReadInt128LittleEndian(); + Assert.Equal(expected, read); + } + + [Fact] + public void ReadUInt128Test() + { + var stream = new MemoryStream(_bytes); + var expected = (UInt128)new BigInteger(_bytes); + UInt128 read = stream.ReadUInt128(); + Assert.Equal(expected, read); + } + + [Fact] + public void ReadUInt128BigEndianTest() + { + var stream = new MemoryStream(_bytes); + var reversed = Enumerable.Reverse(_bytes).ToArray(); + var expected = (UInt128)new BigInteger(reversed); + UInt128 read = stream.ReadUInt128BigEndian(); + Assert.Equal(expected, read); + } + + [Fact] + public void ReadUInt128LittleEndianTest() + { + var stream = new MemoryStream(_bytes); + var expected = (UInt128)new BigInteger(_bytes); + UInt128 read = stream.ReadUInt128LittleEndian(); + Assert.Equal(expected, read); + } + + [Fact] + public void ReadDecimalTest() + { + var stream = new MemoryStream(_decimalBytes); + decimal expected = 0.0123456789M; + decimal read = stream.ReadDecimal(); + Assert.Equal(expected, read); + } + + [Fact] + public void ReadDecimalBigEndianTest() + { + var stream = new MemoryStream([.. Enumerable.Reverse(_decimalBytes)]); + decimal expected = 0.0123456789M; + decimal read = stream.ReadDecimalBigEndian(); + Assert.Equal(expected, read); + } + + [Fact] + public void ReadDecimalLittleEndianTest() + { + var stream = new MemoryStream(_decimalBytes); + decimal expected = 0.0123456789M; + decimal read = stream.ReadDecimalLittleEndian(); + Assert.Equal(expected, read); + } + + #endregion + + #region Peek Read + + [Fact] + public void PeekByteValueTest() + { + var stream = new MemoryStream(_bytes); + byte read = stream.PeekByteValue(); + Assert.Equal(0x00, read); + Assert.Equal(0, stream.Position); + } + + [Fact] + public void PeekByteBothEndianTest() + { + var stream = new MemoryStream(_bytes); + BothUInt8 read = stream.PeekByteBothEndian(); + Assert.Equal(0x00, read.LittleEndian); + Assert.Equal(0x01, read.BigEndian); + Assert.Equal(0, stream.Position); + } + + [Fact] + public void PeekBytesTest() + { + var stream = new MemoryStream(_bytes); + int length = 4; + byte[] read = stream.PeekBytes(length); + Assert.Equal(length, read.Length); + Assert.True(read.SequenceEqual(_bytes.Take(length))); + Assert.Equal(0, stream.Position); + } + + [Fact] + public void PeekSByteTest() + { + var stream = new MemoryStream(_bytes); + sbyte read = stream.PeekSByte(); + Assert.Equal(0x00, read); + Assert.Equal(0, stream.Position); + } + + [Fact] + public void PeekSByteBothEndianTest() + { + var stream = new MemoryStream(_bytes); + BothInt8 read = stream.PeekSByteBothEndian(); + Assert.Equal(0x00, read.LittleEndian); + Assert.Equal(0x01, read.BigEndian); + Assert.Equal(0, stream.Position); + } + + [Fact] + public void PeekCharTest() + { + var stream = new MemoryStream(_bytes); + char read = stream.PeekChar(); + Assert.Equal('\0', read); + Assert.Equal(0, stream.Position); + } + + [Fact] + public void PeekInt16Test() + { + var stream = new MemoryStream(_bytes); + short read = stream.PeekInt16(); + Assert.Equal(0x0100, read); + Assert.Equal(0, stream.Position); + } + + [Fact] + public void PeekInt16BigEndianTest() + { + var stream = new MemoryStream(_bytes); + short read = stream.PeekInt16BigEndian(); + Assert.Equal(0x0001, read); + Assert.Equal(0, stream.Position); + } + + [Fact] + public void PeekInt16LittleEndianTest() + { + var stream = new MemoryStream(_bytes); + short read = stream.PeekInt16LittleEndian(); + Assert.Equal(0x0100, read); + Assert.Equal(0, stream.Position); + } + + [Fact] + public void PeekInt16BothEndianTest() + { + var stream = new MemoryStream(_bytes); + BothInt16 read = stream.PeekInt16BothEndian(); + Assert.Equal(0x0100, read.LittleEndian); + Assert.Equal(0x0203, read.BigEndian); + Assert.Equal(0, stream.Position); + } + + [Fact] + public void PeekUInt16Test() + { + var stream = new MemoryStream(_bytes); + ushort read = stream.PeekUInt16(); + Assert.Equal(0x0100, read); + Assert.Equal(0, stream.Position); + } + + [Fact] + public void PeekUInt16BigEndianTest() + { + var stream = new MemoryStream(_bytes); + ushort read = stream.PeekUInt16BigEndian(); + Assert.Equal(0x0001, read); + Assert.Equal(0, stream.Position); + } + + [Fact] + public void PeekUInt16LittleEndianTest() + { + var stream = new MemoryStream(_bytes); + ushort read = stream.PeekUInt16LittleEndian(); + Assert.Equal(0x0100, read); + Assert.Equal(0, stream.Position); + } + + [Fact] + public void PeekUInt16BothEndianTest() + { + var stream = new MemoryStream(_bytes); + BothUInt16 read = stream.PeekUInt16BothEndian(); + Assert.Equal(0x0100, read.LittleEndian); + Assert.Equal(0x0203, read.BigEndian); + Assert.Equal(0, stream.Position); + } + + [Fact] + public void PeekWORDTest() + { + var stream = new MemoryStream(_bytes); + ushort read = stream.PeekWORD(); + Assert.Equal(0x0100, read); + Assert.Equal(0, stream.Position); + } + + [Fact] + public void PeekWORDBigEndianTest() + { + var stream = new MemoryStream(_bytes); + ushort read = stream.PeekWORDBigEndian(); + Assert.Equal(0x0001, read); + Assert.Equal(0, stream.Position); + } + + [Fact] + public void PeekWORDLittleEndianTest() + { + var stream = new MemoryStream(_bytes); + ushort read = stream.PeekWORDLittleEndian(); + Assert.Equal(0x0100, read); + Assert.Equal(0, stream.Position); + } + + [Fact] + public void PeekWORDBothEndianTest() + { + var stream = new MemoryStream(_bytes); + BothUInt16 read = stream.PeekWORDBothEndian(); + Assert.Equal(0x0100, read.LittleEndian); + Assert.Equal(0x0203, read.BigEndian); + Assert.Equal(0, stream.Position); + } + + [Fact] + public void PeekHalfTest() + { + var stream = new MemoryStream(_bytes); + Half expected = BitConverter.Int16BitsToHalf(0x0100); + Half read = stream.PeekHalf(); + Assert.Equal(expected, read); + Assert.Equal(0, stream.Position); + } + + [Fact] + public void PeekHalfBigEndianTest() + { + var stream = new MemoryStream(_bytes); + Half expected = BitConverter.Int16BitsToHalf(0x0001); + Half read = stream.PeekHalfBigEndian(); + Assert.Equal(expected, read); + Assert.Equal(0, stream.Position); + } + + [Fact] + public void PeekHalfLittleEndianTest() + { + var stream = new MemoryStream(_bytes); + Half expected = BitConverter.Int16BitsToHalf(0x0100); + Half read = stream.PeekHalfLittleEndian(); + Assert.Equal(expected, read); + Assert.Equal(0, stream.Position); + } + + [Fact] + public void PeekInt24Test() + { + var stream = new MemoryStream(_bytes); + Int24 read = stream.PeekInt24(); + Assert.Equal(0x020100, (int)read); + Assert.Equal(0, stream.Position); + } + + [Fact] + public void PeekInt24BigEndianTest() + { + var stream = new MemoryStream(_bytes); + Int24 read = stream.PeekInt24BigEndian(); + Assert.Equal(0x000102, (int)read); + Assert.Equal(0, stream.Position); + } + + [Fact] + public void PeekInt24LittleEndianTest() + { + var stream = new MemoryStream(_bytes); + Int24 read = stream.PeekInt24LittleEndian(); + Assert.Equal(0x020100, (int)read); + Assert.Equal(0, stream.Position); + } + + [Fact] + public void PeekUInt24Test() + { + var stream = new MemoryStream(_bytes); + UInt24 read = stream.PeekUInt24(); + Assert.Equal((uint)0x020100, (uint)read); + Assert.Equal(0, stream.Position); + } + + [Fact] + public void PeekUInt24BigEndianTest() + { + var stream = new MemoryStream(_bytes); + UInt24 read = stream.PeekUInt24BigEndian(); + Assert.Equal((uint)0x000102, (uint)read); + Assert.Equal(0, stream.Position); + } + + [Fact] + public void PeekUInt24LittleEndianTest() + { + var stream = new MemoryStream(_bytes); + UInt24 read = stream.PeekUInt24LittleEndian(); + Assert.Equal((uint)0x020100, (uint)read); + Assert.Equal(0, stream.Position); + } + + [Fact] + public void PeekInt32Test() + { + var stream = new MemoryStream(_bytes); + int read = stream.PeekInt32(); + Assert.Equal(0x03020100, read); + Assert.Equal(0, stream.Position); + } + + [Fact] + public void PeekInt32BigEndianTest() + { + var stream = new MemoryStream(_bytes); + int read = stream.PeekInt32BigEndian(); + Assert.Equal(0x00010203, read); + Assert.Equal(0, stream.Position); + } + + [Fact] + public void PeekInt32LittleEndianTest() + { + var stream = new MemoryStream(_bytes); + int read = stream.PeekInt32LittleEndian(); + Assert.Equal(0x03020100, read); + Assert.Equal(0, stream.Position); + } + + [Fact] + public void PeekInt32BothEndianTest() + { + var stream = new MemoryStream(_bytes); + BothInt32 read = stream.PeekInt32BothEndian(); + Assert.Equal(0x03020100, read.LittleEndian); + Assert.Equal(0x04050607, read.BigEndian); + Assert.Equal(0, stream.Position); + } + + [Fact] + public void PeekUInt32Test() + { + var stream = new MemoryStream(_bytes); + uint read = stream.PeekUInt32(); + Assert.Equal((uint)0x03020100, read); + Assert.Equal(0, stream.Position); + } + + [Fact] + public void PeekUInt32BigEndianTest() + { + var stream = new MemoryStream(_bytes); + uint read = stream.PeekUInt32BigEndian(); + Assert.Equal((uint)0x00010203, read); + Assert.Equal(0, stream.Position); + } + + [Fact] + public void PeekUInt32LittleEndianTest() + { + var stream = new MemoryStream(_bytes); + uint read = stream.PeekUInt32LittleEndian(); + Assert.Equal((uint)0x03020100, read); + Assert.Equal(0, stream.Position); + } + + [Fact] + public void PeekUInt32BothEndianTest() + { + var stream = new MemoryStream(_bytes); + BothUInt32 read = stream.PeekUInt32BothEndian(); + Assert.Equal((uint)0x03020100, read.LittleEndian); + Assert.Equal((uint)0x04050607, read.BigEndian); + Assert.Equal(0, stream.Position); + } + + [Fact] + public void PeekDWORDTest() + { + var stream = new MemoryStream(_bytes); + uint read = stream.PeekDWORD(); + Assert.Equal((uint)0x03020100, read); + Assert.Equal(0, stream.Position); + } + + [Fact] + public void PeekDWORDBigEndianTest() + { + var stream = new MemoryStream(_bytes); + uint read = stream.PeekDWORDBigEndian(); + Assert.Equal((uint)0x00010203, read); + Assert.Equal(0, stream.Position); + } + + [Fact] + public void PeekDWORDLittleEndianTest() + { + var stream = new MemoryStream(_bytes); + uint read = stream.PeekDWORDLittleEndian(); + Assert.Equal((uint)0x03020100, read); + Assert.Equal(0, stream.Position); + } + + [Fact] + public void PeekDWORDBothEndianTest() + { + var stream = new MemoryStream(_bytes); + BothUInt32 read = stream.PeekDWORDBothEndian(); + Assert.Equal((uint)0x03020100, read.LittleEndian); + Assert.Equal((uint)0x04050607, read.BigEndian); + Assert.Equal(0, stream.Position); + } + + [Fact] + public void PeekSingleTest() + { + var stream = new MemoryStream(_bytes); + float expected = BitConverter.Int32BitsToSingle(0x03020100); + float read = stream.PeekSingle(); + Assert.Equal(expected, read); + Assert.Equal(0, stream.Position); + } + + [Fact] + public void PeekSingleBigEndianTest() + { + var stream = new MemoryStream(_bytes); + float expected = BitConverter.Int32BitsToSingle(0x00010203); + float read = stream.PeekSingleBigEndian(); + Assert.Equal(expected, read); + Assert.Equal(0, stream.Position); + } + + [Fact] + public void PeekSingleLittleEndianTest() + { + var stream = new MemoryStream(_bytes); + float expected = BitConverter.Int32BitsToSingle(0x03020100); + float read = stream.PeekSingleLittleEndian(); + Assert.Equal(expected, read); + Assert.Equal(0, stream.Position); + } + + [Fact] + public void PeekInt48Test() + { + var stream = new MemoryStream(_bytes); + Int48 read = stream.PeekInt48(); + Assert.Equal(0x050403020100, (long)read); + Assert.Equal(0, stream.Position); + } + + [Fact] + public void PeekInt48BigEndianTest() + { + var stream = new MemoryStream(_bytes); + Int48 read = stream.PeekInt48BigEndian(); + Assert.Equal(0x000102030405, (long)read); + Assert.Equal(0, stream.Position); + } + + [Fact] + public void PeekInt48LittleEndianTest() + { + var stream = new MemoryStream(_bytes); + Int48 read = stream.PeekInt48LittleEndian(); + Assert.Equal(0x050403020100, (long)read); + Assert.Equal(0, stream.Position); + } + + [Fact] + public void PeekUInt48Test() + { + var stream = new MemoryStream(_bytes); + UInt48 read = stream.PeekUInt48(); + Assert.Equal((ulong)0x050403020100, (ulong)read); + Assert.Equal(0, stream.Position); + } + + [Fact] + public void PeekUInt48BigEndianTest() + { + var stream = new MemoryStream(_bytes); + UInt48 read = stream.PeekUInt48BigEndian(); + Assert.Equal((ulong)0x000102030405, (ulong)read); + Assert.Equal(0, stream.Position); + } + + [Fact] + public void PeekUInt48LittleEndianTest() + { + var stream = new MemoryStream(_bytes); + UInt48 read = stream.PeekUInt48LittleEndian(); + Assert.Equal((ulong)0x050403020100, (ulong)read); + Assert.Equal(0, stream.Position); + } + + [Fact] + public void PeekInt64Test() + { + var stream = new MemoryStream(_bytes); + long read = stream.PeekInt64(); + Assert.Equal(0x0706050403020100, read); + Assert.Equal(0, stream.Position); + } + + [Fact] + public void PeekInt64BigEndianTest() + { + var stream = new MemoryStream(_bytes); + long read = stream.PeekInt64BigEndian(); + Assert.Equal(0x0001020304050607, read); + Assert.Equal(0, stream.Position); + } + + [Fact] + public void PeekInt64LittleEndianTest() + { + var stream = new MemoryStream(_bytes); + long read = stream.PeekInt64LittleEndian(); + Assert.Equal(0x0706050403020100, read); + Assert.Equal(0, stream.Position); + } + + [Fact] + public void PeekInt64BothEndianTest() + { + var stream = new MemoryStream(_bytes); + BothInt64 read = stream.PeekInt64BothEndian(); + Assert.Equal(0x0706050403020100, read.LittleEndian); + Assert.Equal(0x08090A0B0C0D0E0F, read.BigEndian); + Assert.Equal(0, stream.Position); + } + + [Fact] + public void PeekUInt64Test() + { + var stream = new MemoryStream(_bytes); + ulong read = stream.PeekUInt64(); + Assert.Equal((ulong)0x0706050403020100, read); + Assert.Equal(0, stream.Position); + } + + [Fact] + public void PeekUInt64BigEndianTest() + { + var stream = new MemoryStream(_bytes); + ulong read = stream.PeekUInt64BigEndian(); + Assert.Equal((ulong)0x0001020304050607, read); + Assert.Equal(0, stream.Position); + } + + [Fact] + public void PeekUInt64LittleEndianTest() + { + var stream = new MemoryStream(_bytes); + ulong read = stream.PeekUInt64LittleEndian(); + Assert.Equal((ulong)0x0706050403020100, read); + Assert.Equal(0, stream.Position); + } + + [Fact] + public void PeekUInt64BothEndianTest() + { + var stream = new MemoryStream(_bytes); + BothUInt64 read = stream.PeekUInt64BothEndian(); + Assert.Equal((ulong)0x0706050403020100, read.LittleEndian); + Assert.Equal((ulong)0x08090A0B0C0D0E0F, read.BigEndian); + Assert.Equal(0, stream.Position); + } + + [Fact] + public void PeekQWORDTest() + { + var stream = new MemoryStream(_bytes); + ulong read = stream.PeekQWORD(); + Assert.Equal((ulong)0x0706050403020100, read); + Assert.Equal(0, stream.Position); + } + + [Fact] + public void PeekQWORDBigEndianTest() + { + var stream = new MemoryStream(_bytes); + ulong read = stream.PeekQWORDBigEndian(); + Assert.Equal((ulong)0x0001020304050607, read); + Assert.Equal(0, stream.Position); + } + + [Fact] + public void PeekQWORDLittleEndianTest() + { + var stream = new MemoryStream(_bytes); + ulong read = stream.PeekQWORDLittleEndian(); + Assert.Equal((ulong)0x0706050403020100, read); + Assert.Equal(0, stream.Position); + } + + [Fact] + public void PeekQWORDBothEndianTest() + { + var stream = new MemoryStream(_bytes); + BothUInt64 read = stream.PeekQWORDBothEndian(); + Assert.Equal((ulong)0x0706050403020100, read.LittleEndian); + Assert.Equal((ulong)0x08090A0B0C0D0E0F, read.BigEndian); + Assert.Equal(0, stream.Position); + } + + [Fact] + public void PeekDoubleTest() + { + var stream = new MemoryStream(_bytes); + double expected = BitConverter.Int64BitsToDouble(0x0706050403020100); + double read = stream.PeekDouble(); + Assert.Equal(expected, read); + Assert.Equal(0, stream.Position); + } + + [Fact] + public void PeekDoubleBigEndianTest() + { + var stream = new MemoryStream(_bytes); + double expected = BitConverter.Int64BitsToDouble(0x0001020304050607); + double read = stream.PeekDoubleBigEndian(); + Assert.Equal(expected, read); + Assert.Equal(0, stream.Position); + } + + [Fact] + public void PeekDoubleLittleEndianTest() + { + var stream = new MemoryStream(_bytes); + double expected = BitConverter.Int64BitsToDouble(0x0706050403020100); + double read = stream.PeekDoubleLittleEndian(); + Assert.Equal(expected, read); + Assert.Equal(0, stream.Position); + } + + [Fact] + public void PeekGuidTest() + { + var stream = new MemoryStream(_bytes); + var expected = new Guid(_bytes); + Guid read = stream.PeekGuid(); + Assert.Equal(expected, read); + Assert.Equal(0, stream.Position); + } + + [Fact] + public void PeekGuidBigEndianTest() + { + var stream = new MemoryStream(_bytes); + var expected = new Guid(_guidBigEndianbytes); + Guid read = stream.PeekGuidBigEndian(); + Assert.Equal(expected, read); + Assert.Equal(0, stream.Position); + } + + [Fact] + public void PeekGuidLittleEndianTest() + { + var stream = new MemoryStream(_bytes); + var expected = new Guid(_bytes); + Guid read = stream.PeekGuidLittleEndian(); + Assert.Equal(expected, read); + Assert.Equal(0, stream.Position); + } + + [Fact] + public void PeekInt128Test() + { + var stream = new MemoryStream(_bytes); + var expected = (Int128)new BigInteger(_bytes); + Int128 read = stream.PeekInt128(); + Assert.Equal(expected, read); + Assert.Equal(0, stream.Position); + } + + [Fact] + public void PeekInt128BigEndianTest() + { + var stream = new MemoryStream(_bytes); + var reversed = Enumerable.Reverse(_bytes).ToArray(); + var expected = (Int128)new BigInteger(reversed); + Int128 read = stream.PeekInt128BigEndian(); + Assert.Equal(expected, read); + Assert.Equal(0, stream.Position); + } + + [Fact] + public void PeekInt128LittleEndianTest() + { + var stream = new MemoryStream(_bytes); + var expected = (Int128)new BigInteger(_bytes); + Int128 read = stream.PeekInt128LittleEndian(); + Assert.Equal(expected, read); + Assert.Equal(0, stream.Position); + } + + [Fact] + public void PeekUInt128Test() + { + var stream = new MemoryStream(_bytes); + var expected = (UInt128)new BigInteger(_bytes); + UInt128 read = stream.PeekUInt128(); + Assert.Equal(expected, read); + Assert.Equal(0, stream.Position); + } + + [Fact] + public void PeekUInt128BigEndianTest() + { + var stream = new MemoryStream(_bytes); + var reversed = Enumerable.Reverse(_bytes).ToArray(); + var expected = (UInt128)new BigInteger(reversed); + UInt128 read = stream.PeekUInt128BigEndian(); + Assert.Equal(expected, read); + Assert.Equal(0, stream.Position); + } + + [Fact] + public void PeekUInt128LittleEndianTest() + { + var stream = new MemoryStream(_bytes); + var expected = (UInt128)new BigInteger(_bytes); + UInt128 read = stream.PeekUInt128LittleEndian(); + Assert.Equal(expected, read); + Assert.Equal(0, stream.Position); + } + + [Fact] + public void PeekDecimalTest() + { + var stream = new MemoryStream(_decimalBytes); + decimal expected = 0.0123456789M; + decimal read = stream.PeekDecimal(); + Assert.Equal(expected, read); + Assert.Equal(0, stream.Position); + } + + [Fact] + public void PeekDecimalBigEndianTest() + { + var stream = new MemoryStream([.. Enumerable.Reverse(_decimalBytes)]); + decimal expected = 0.0123456789M; + decimal read = stream.PeekDecimalBigEndian(); + Assert.Equal(expected, read); + Assert.Equal(0, stream.Position); + } + + [Fact] + public void PeekDecimalLittleEndianTest() + { + var stream = new MemoryStream(_decimalBytes); + decimal expected = 0.0123456789M; + decimal read = stream.PeekDecimalLittleEndian(); + Assert.Equal(expected, read); + Assert.Equal(0, stream.Position); + } + + #endregion + + #region Try Read + + [Fact] + public void TryReadByteValueTest() + { + var stream = new MemoryStream([]); + bool actual = stream.TryReadByteValue(out byte read); + Assert.False(actual); + Assert.Equal(default, read); + } + + [Fact] + public void TryReadBytesTest() + { + var stream = new MemoryStream([]); + int length = 4; + bool actual = stream.TryReadBytes(length, out byte[] read); + Assert.False(actual); + Assert.Empty(read); + } + + [Fact] + public void TryReadByteBothEndianTest() + { + var stream = new MemoryStream([]); + bool actual = stream.TryReadByteBothEndian(out BothUInt8 read); + Assert.False(actual); + Assert.Equal(default, read.LittleEndian); + Assert.Equal(default, read.BigEndian); + } + + [Fact] + public void TryReadSByteTest() + { + var stream = new MemoryStream([]); + bool actual = stream.TryReadSByte(out sbyte read); + Assert.False(actual); + Assert.Equal(default, read); + } + + [Fact] + public void TryReadSByteBothEndianTest() + { + var stream = new MemoryStream([]); + bool actual = stream.TryReadSByteBothEndian(out BothInt8 read); + Assert.False(actual); + Assert.Equal(default, read.LittleEndian); + Assert.Equal(default, read.BigEndian); + } + + [Fact] + public void TryReadCharTest() + { + var stream = new MemoryStream([]); + bool actual = stream.TryReadChar(out char read); + Assert.False(actual); + Assert.Equal(default, read); + } + + [Fact] + public void TryReadInt16Test() + { + var stream = new MemoryStream([]); + bool actual = stream.TryReadInt16(out short read); + Assert.False(actual); + Assert.Equal(default, read); + } + + [Fact] + public void TryReadInt16BigEndianTest() + { + var stream = new MemoryStream([]); + bool actual = stream.TryReadInt16BigEndian(out short read); + Assert.False(actual); + Assert.Equal(default, read); + } + + [Fact] + public void TryReadInt16LittleEndianTest() + { + var stream = new MemoryStream([]); + bool actual = stream.TryReadInt16LittleEndian(out short read); + Assert.False(actual); + Assert.Equal(default, read); + } + + [Fact] + public void TryReadInt16BothEndianTest() + { + var stream = new MemoryStream([]); + bool actual = stream.TryReadInt16BothEndian(out BothInt16 read); + Assert.False(actual); + Assert.Equal(default, read.LittleEndian); + Assert.Equal(default, read.BigEndian); + } + + [Fact] + public void TryReadUInt16Test() + { + var stream = new MemoryStream([]); + bool actual = stream.TryReadUInt16(out ushort read); + Assert.False(actual); + Assert.Equal(default, read); + } + + [Fact] + public void TryReadUInt16BigEndianTest() + { + var stream = new MemoryStream([]); + bool actual = stream.TryReadUInt16BigEndian(out ushort read); + Assert.False(actual); + Assert.Equal(default, read); + } + + [Fact] + public void TryReadUInt16LittleEndianTest() + { + var stream = new MemoryStream([]); + bool actual = stream.TryReadUInt16LittleEndian(out ushort read); + Assert.False(actual); + Assert.Equal(default, read); + } + + [Fact] + public void TryReadUInt16BothEndianTest() + { + var stream = new MemoryStream([]); + bool actual = stream.TryReadUInt16BothEndian(out BothUInt16 read); + Assert.False(actual); + Assert.Equal(default, read.LittleEndian); + Assert.Equal(default, read.BigEndian); + } + + [Fact] + public void TryReadWORDTest() + { + var stream = new MemoryStream([]); + bool actual = stream.TryReadWORD(out ushort read); + Assert.False(actual); + Assert.Equal(default, read); + } + + [Fact] + public void TryReadWORDBigEndianTest() + { + var stream = new MemoryStream([]); + bool actual = stream.TryReadWORDBigEndian(out ushort read); + Assert.False(actual); + Assert.Equal(default, read); + } + + [Fact] + public void TryReadWORDLittleEndianTest() + { + var stream = new MemoryStream([]); + bool actual = stream.TryReadWORDLittleEndian(out ushort read); + Assert.False(actual); + Assert.Equal(default, read); + } + + [Fact] + public void TryReadWORDBothEndianTest() + { + var stream = new MemoryStream([]); + bool actual = stream.TryReadWORDBothEndian(out BothUInt16 read); + Assert.False(actual); + Assert.Equal(default, read.LittleEndian); + Assert.Equal(default, read.BigEndian); + } + + [Fact] + public void TryReadHalfTest() + { + var stream = new MemoryStream([]); + bool actual = stream.TryReadHalf(out Half read); + Assert.False(actual); + Assert.Equal(default, read); + } + + [Fact] + public void TryReadHalfBigEndianTest() + { + var stream = new MemoryStream([]); + bool actual = stream.TryReadHalfBigEndian(out Half read); + Assert.False(actual); + Assert.Equal(default, read); + } + + [Fact] + public void TryReadHalfLittleEndianTest() + { + var stream = new MemoryStream([]); + bool actual = stream.TryReadHalfLittleEndian(out Half read); + Assert.False(actual); + Assert.Equal(default, read); + } + + [Fact] + public void TryReadInt24Test() + { + var stream = new MemoryStream([]); + bool actual = stream.TryReadInt24(out Int24 read); + Assert.False(actual); + Assert.Equal(default, (int)read); + } + + [Fact] + public void TryReadInt24BigEndianTest() + { + var stream = new MemoryStream([]); + bool actual = stream.TryReadInt24BigEndian(out Int24 read); + Assert.False(actual); + Assert.Equal(default, (int)read); + } + + [Fact] + public void TryReadInt24LittleEndianTest() + { + var stream = new MemoryStream([]); + bool actual = stream.TryReadInt24LittleEndian(out Int24 read); + Assert.False(actual); + Assert.Equal(default, (int)read); + } + + [Fact] + public void TryReadUInt24Test() + { + var stream = new MemoryStream([]); + bool actual = stream.TryReadUInt24(out UInt24 read); + Assert.False(actual); + Assert.Equal(default, (uint)read); + } + + [Fact] + public void TryReadUInt24BigEndianTest() + { + var stream = new MemoryStream([]); + bool actual = stream.TryReadUInt24BigEndian(out UInt24 read); + Assert.False(actual); + Assert.Equal(default, (uint)read); + } + + [Fact] + public void TryReadUInt24LittleEndianTest() + { + var stream = new MemoryStream([]); + bool actual = stream.TryReadUInt24LittleEndian(out UInt24 read); + Assert.False(actual); + Assert.Equal(default, (uint)read); + } + + [Fact] + public void TryReadInt32Test() + { + var stream = new MemoryStream([]); + bool actual = stream.TryReadInt32(out int read); + Assert.False(actual); + Assert.Equal(default, read); + } + + [Fact] + public void TryReadInt32BigEndianTest() + { + var stream = new MemoryStream([]); + bool actual = stream.TryReadInt32BigEndian(out int read); + Assert.False(actual); + Assert.Equal(default, read); + } + + [Fact] + public void TryReadInt32LittleEndianTest() + { + var stream = new MemoryStream([]); + bool actual = stream.TryReadInt32LittleEndian(out int read); + Assert.False(actual); + Assert.Equal(default, read); + } + + [Fact] + public void TryReadInt32BothEndianTest() + { + var stream = new MemoryStream([]); + bool actual = stream.TryReadInt32BothEndian(out BothInt32 read); + Assert.False(actual); + Assert.Equal(default, read.LittleEndian); + Assert.Equal(default, read.BigEndian); + } + + [Fact] + public void TryReadUInt32Test() + { + var stream = new MemoryStream([]); + bool actual = stream.TryReadUInt32(out uint read); + Assert.False(actual); + Assert.Equal(default, read); + } + + [Fact] + public void TryReadUInt32BigEndianTest() + { + var stream = new MemoryStream([]); + bool actual = stream.TryReadUInt32BigEndian(out uint read); + Assert.False(actual); + Assert.Equal(default, read); + } + + [Fact] + public void TryReadUInt32LittleEndianTest() + { + var stream = new MemoryStream([]); + bool actual = stream.TryReadUInt32LittleEndian(out uint read); + Assert.False(actual); + Assert.Equal(default, read); + } + + [Fact] + public void TryReadUInt32BothEndianTest() + { + var stream = new MemoryStream([]); + bool actual = stream.TryReadUInt32BothEndian(out BothUInt32 read); + Assert.False(actual); + Assert.Equal(default, read.LittleEndian); + Assert.Equal(default, read.BigEndian); + } + + [Fact] + public void TryReadDWORDTest() + { + var stream = new MemoryStream([]); + bool actual = stream.TryReadDWORD(out uint read); + Assert.False(actual); + Assert.Equal(default, read); + } + + [Fact] + public void TryReadDWORDBigEndianTest() + { + var stream = new MemoryStream([]); + bool actual = stream.TryReadDWORDBigEndian(out uint read); + Assert.False(actual); + Assert.Equal(default, read); + } + + [Fact] + public void TryReadDWORDLittleEndianTest() + { + var stream = new MemoryStream([]); + bool actual = stream.TryReadDWORDLittleEndian(out uint read); + Assert.False(actual); + Assert.Equal(default, read); + } + + [Fact] + public void TryReadDWORDBothEndianTest() + { + var stream = new MemoryStream([]); + bool actual = stream.TryReadDWORDBothEndian(out BothUInt32 read); + Assert.False(actual); + Assert.Equal(default, read.LittleEndian); + Assert.Equal(default, read.BigEndian); + } + + [Fact] + public void TryReadSingleTest() + { + var stream = new MemoryStream([]); + bool actual = stream.TryReadSingle(out float read); + Assert.False(actual); + Assert.Equal(default, read); + } + + [Fact] + public void TryReadSingleBigEndianTest() + { + var stream = new MemoryStream([]); + bool actual = stream.TryReadSingleBigEndian(out float read); + Assert.False(actual); + Assert.Equal(default, read); + } + + [Fact] + public void TryReadSingleLittleEndianTest() + { + var stream = new MemoryStream([]); + bool actual = stream.TryReadSingleLittleEndian(out float read); + Assert.False(actual); + Assert.Equal(default, read); + } + + [Fact] + public void TryReadInt48Test() + { + var stream = new MemoryStream([]); + bool actual = stream.TryReadInt48(out Int48 read); + Assert.False(actual); + Assert.Equal(default, (long)read); + } + + [Fact] + public void TryReadInt48BigEndianTest() + { + var stream = new MemoryStream([]); + bool actual = stream.TryReadInt48BigEndian(out Int48 read); + Assert.False(actual); + Assert.Equal(default, (long)read); + } + + [Fact] + public void TryReadInt48LittleEndianTest() + { + var stream = new MemoryStream([]); + bool actual = stream.TryReadInt48LittleEndian(out Int48 read); + Assert.False(actual); + Assert.Equal(default, (long)read); + } + + [Fact] + public void TryReadUInt48Test() + { + var stream = new MemoryStream([]); + bool actual = stream.TryReadUInt48(out UInt48 read); + Assert.False(actual); + Assert.Equal(default, (ulong)read); + } + + [Fact] + public void TryReadUInt48BigEndianTest() + { + var stream = new MemoryStream([]); + bool actual = stream.TryReadUInt48BigEndian(out UInt48 read); + Assert.False(actual); + Assert.Equal(default, (ulong)read); + } + + [Fact] + public void TryReadUInt48LittleEndianTest() + { + var stream = new MemoryStream([]); + bool actual = stream.TryReadUInt48LittleEndian(out UInt48 read); + Assert.False(actual); + Assert.Equal(default, (ulong)read); + } + + [Fact] + public void TryReadInt64Test() + { + var stream = new MemoryStream([]); + bool actual = stream.TryReadInt64(out long read); + Assert.False(actual); + Assert.Equal(default, read); + } + + [Fact] + public void TryReadInt64BigEndianTest() + { + var stream = new MemoryStream([]); + bool actual = stream.TryReadInt64BigEndian(out long read); + Assert.False(actual); + Assert.Equal(default, read); + } + + [Fact] + public void TryReadInt64LittleEndianTest() + { + var stream = new MemoryStream([]); + bool actual = stream.TryReadInt64LittleEndian(out long read); + Assert.False(actual); + Assert.Equal(default, read); + } + + [Fact] + public void TryReadInt64BothEndianTest() + { + var stream = new MemoryStream([]); + bool actual = stream.TryReadInt64BothEndian(out BothInt64 read); + Assert.False(actual); + Assert.Equal(default, read.LittleEndian); + Assert.Equal(default, read.BigEndian); + } + + [Fact] + public void TryReadUInt64Test() + { + var stream = new MemoryStream([]); + bool actual = stream.TryReadUInt64(out ulong read); + Assert.False(actual); + Assert.Equal(default, read); + } + + [Fact] + public void TryReadUInt64BigEndianTest() + { + var stream = new MemoryStream([]); + bool actual = stream.TryReadUInt64BigEndian(out ulong read); + Assert.False(actual); + Assert.Equal(default, read); + } + + [Fact] + public void TryReadUInt64LittleEndianTest() + { + var stream = new MemoryStream([]); + bool actual = stream.TryReadUInt64LittleEndian(out ulong read); + Assert.False(actual); + Assert.Equal(default, read); + } + + [Fact] + public void TryReadUInt64BothEndianTest() + { + var stream = new MemoryStream([]); + bool actual = stream.TryReadUInt64BothEndian(out BothUInt64 read); + Assert.False(actual); + Assert.Equal(default, read.LittleEndian); + Assert.Equal(default, read.BigEndian); + } + + [Fact] + public void TryReadQWORDTest() + { + var stream = new MemoryStream([]); + bool actual = stream.TryReadQWORD(out ulong read); + Assert.False(actual); + Assert.Equal(default, read); + } + + [Fact] + public void TryReadQWORDBigEndianTest() + { + var stream = new MemoryStream([]); + bool actual = stream.TryReadQWORDBigEndian(out ulong read); + Assert.False(actual); + Assert.Equal(default, read); + } + + [Fact] + public void TryReadQWORDLittleEndianTest() + { + var stream = new MemoryStream([]); + bool actual = stream.TryReadQWORDLittleEndian(out ulong read); + Assert.False(actual); + Assert.Equal(default, read); + } + + [Fact] + public void TryReadQWORDBothEndianTest() + { + var stream = new MemoryStream([]); + bool actual = stream.TryReadQWORDBothEndian(out BothUInt64 read); + Assert.False(actual); + Assert.Equal(default, read.LittleEndian); + Assert.Equal(default, read.BigEndian); + } + + [Fact] + public void TryReadDoubleTest() + { + var stream = new MemoryStream([]); + bool actual = stream.TryReadDouble(out double read); + Assert.False(actual); + Assert.Equal(default, read); + } + + [Fact] + public void TryReadDoubleBigEndianTest() + { + var stream = new MemoryStream([]); + bool actual = stream.TryReadDoubleBigEndian(out double read); + Assert.False(actual); + Assert.Equal(default, read); + } + + [Fact] + public void TryReadDoubleLittleEndianTest() + { + var stream = new MemoryStream([]); + bool actual = stream.TryReadDoubleLittleEndian(out double read); + Assert.False(actual); + Assert.Equal(default, read); + } + + [Fact] + public void TryReadGuidTest() + { + var stream = new MemoryStream([]); + bool actual = stream.TryReadGuid(out Guid read); + Assert.False(actual); + Assert.Equal(default, read); + } + + [Fact] + public void TryReadGuidBigEndianTest() + { + var stream = new MemoryStream([]); + bool actual = stream.TryReadGuidBigEndian(out Guid read); + Assert.False(actual); + Assert.Equal(default, read); + } + + [Fact] + public void TryReadGuidLittleEndianTest() + { + var stream = new MemoryStream([]); + bool actual = stream.TryReadGuidLittleEndian(out Guid read); + Assert.False(actual); + Assert.Equal(default, read); + } + + [Fact] + public void TryReadInt128Test() + { + var stream = new MemoryStream([]); + bool actual = stream.TryReadInt128(out Int128 read); + Assert.False(actual); + Assert.Equal(default, read); + } + + [Fact] + public void TryReadInt128BigEndianTest() + { + var stream = new MemoryStream([]); + bool actual = stream.TryReadInt128BigEndian(out Int128 read); + Assert.False(actual); + Assert.Equal(default, read); + } + + [Fact] + public void TryReadInt128LittleEndianTest() + { + var stream = new MemoryStream([]); + bool actual = stream.TryReadInt128LittleEndian(out Int128 read); + Assert.False(actual); + Assert.Equal(default, read); + } + + [Fact] + public void TryReadUInt128Test() + { + var stream = new MemoryStream([]); + bool actual = stream.TryReadUInt128(out UInt128 read); + Assert.False(actual); + Assert.Equal(default, read); + } + + [Fact] + public void TryReadUInt128BigEndianTest() + { + var stream = new MemoryStream([]); + bool actual = stream.TryReadUInt128BigEndian(out UInt128 read); + Assert.False(actual); + Assert.Equal(default, read); + } + + [Fact] + public void TryReadUInt128LittleEndianTest() + { + var stream = new MemoryStream([]); + bool actual = stream.TryReadUInt128LittleEndian(out UInt128 read); + Assert.False(actual); + Assert.Equal(default, read); + } + + [Fact] + public void TryReadDecimalTest() + { + var stream = new MemoryStream([]); + bool actual = stream.TryReadDecimal(out decimal read); + Assert.False(actual); + Assert.Equal(default, read); + } + + [Fact] + public void TryReadDecimalBigEndianTest() + { + var stream = new MemoryStream([]); + bool actual = stream.TryReadDecimalBigEndian(out decimal read); + Assert.False(actual); + Assert.Equal(default, read); + } + + [Fact] + public void TryReadDecimalLittleEndianTest() + { + var stream = new MemoryStream([]); + bool actual = stream.TryReadDecimalLittleEndian(out decimal read); + Assert.False(actual); + Assert.Equal(default, read); + } + + #endregion + } +} diff --git a/SabreTools.Numerics.Extensions.Test/StreamWriterExtensionsTests.cs b/SabreTools.Numerics.Extensions.Test/StreamWriterExtensionsTests.cs new file mode 100644 index 0000000..de418fc --- /dev/null +++ b/SabreTools.Numerics.Extensions.Test/StreamWriterExtensionsTests.cs @@ -0,0 +1,706 @@ +using System; +using System.IO; +using System.Linq; +using System.Numerics; +using System.Text; +using Xunit; + +namespace SabreTools.Numerics.Extensions.Test +{ + public class StreamWriterExtensionsTests + { + /// + /// Test pattern from 0x00-0x0F + /// + private static readonly byte[] _bytes = + [ + 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, + 0x08, 0x09, 0x0A, 0x0B, 0x0C, 0x0D, 0x0E, 0x0F, + ]; + + /// + /// Represents the decimal value 0.0123456789 + /// + private static readonly byte[] _decimalBytes = + [ + 0x15, 0xCD, 0x5B, 0x07, 0x00, 0x00, 0x00, 0x00, + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x0A, 0x00, + ]; + + /// + /// Test pattern for big-endian GUID created from + /// + private static readonly byte[] _guidBigEndianbytes = + [ + 0x03, 0x02, 0x01, 0x00, 0x05, 0x04, 0x07, 0x06, + 0x08, 0x09, 0x0A, 0x0B, 0x0C, 0x0D, 0x0E, 0x0F, + ]; + + [Fact] + public void WriteByteValueTest() + { + var stream = new MemoryStream(new byte[16], 0, 16, true, true); + byte[] expected = [.. _bytes.Take(1)]; + bool write = stream.Write((byte)0x00); + Assert.True(write); + ValidateBytes(expected, stream.GetBuffer()); + } + + [Fact] + public void WriteByteBothEndianTest() + { + var stream = new MemoryStream(new byte[16], 0, 16, true, true); + byte[] expected = [.. _bytes.Take(2)]; + + int offset = 0; + stream.WriteBothEndian(_bytes.ReadByteBothEndian(ref offset)); + ValidateBytes(expected, stream.GetBuffer()); + } + + [Fact] + public void WriteBytesTest() + { + var stream = new MemoryStream(new byte[16], 0, 16, true, true); + byte[] expected = [.. _bytes.Take(4)]; + bool write = StreamWriterExtensions.Write(stream, [0x00, 0x01, 0x02, 0x03]); + Assert.True(write); + ValidateBytes(expected, stream.GetBuffer()); + } + + [Fact] + public void WriteBytesBigEndianTest() + { + var stream = new MemoryStream(new byte[16], 0, 16, true, true); + byte[] expected = [.. _bytes.Take(4)]; + stream.WriteBigEndian([0x03, 0x02, 0x01, 0x00]); + ValidateBytes(expected, stream.GetBuffer()); + } + + [Fact] + public void WriteSByteTest() + { + var stream = new MemoryStream(new byte[16], 0, 16, true, true); + byte[] expected = [.. _bytes.Take(1)]; + bool write = stream.Write((sbyte)0x00); + Assert.True(write); + ValidateBytes(expected, stream.GetBuffer()); + } + + [Fact] + public void WriteSByteBothEndianTest() + { + var stream = new MemoryStream(new byte[16], 0, 16, true, true); + byte[] expected = [.. _bytes.Take(2)]; + + int offset = 0; + stream.WriteBothEndian(_bytes.ReadSByteBothEndian(ref offset)); + ValidateBytes(expected, stream.GetBuffer()); + } + + [Fact] + public void WriteCharTest() + { + var stream = new MemoryStream(new byte[16], 0, 16, true, true); + byte[] expected = [.. _bytes.Take(1)]; + bool write = stream.Write('\0'); + Assert.True(write); + ValidateBytes(expected, stream.GetBuffer()); + } + + [Fact] + public void WriteCharEncodingTest() + { + var stream = new MemoryStream(new byte[16], 0, 16, true, true); + byte[] expected = [0x00, 0x00]; + stream.Write('\0', Encoding.Unicode); + ValidateBytes(expected, stream.GetBuffer()); + } + + [Fact] + public void WriteInt16Test() + { + var stream = new MemoryStream(new byte[16], 0, 16, true, true); + byte[] expected = [.. _bytes.Take(2)]; + bool write = stream.Write((short)0x0100); + Assert.True(write); + ValidateBytes(expected, stream.GetBuffer()); + } + + [Fact] + public void WriteInt16BigEndianTest() + { + var stream = new MemoryStream(new byte[16], 0, 16, true, true); + byte[] expected = [.. _bytes.Take(2)]; + bool write = stream.WriteBigEndian((short)0x0001); + Assert.True(write); + ValidateBytes(expected, stream.GetBuffer()); + } + + [Fact] + public void WriteInt16LittleEndianTest() + { + var stream = new MemoryStream(new byte[16], 0, 16, true, true); + byte[] expected = [.. _bytes.Take(2)]; + bool write = stream.WriteLittleEndian((short)0x0100); + Assert.True(write); + ValidateBytes(expected, stream.GetBuffer()); + } + + [Fact] + public void WriteInt16BothEndianTest() + { + var stream = new MemoryStream(new byte[16], 0, 16, true, true); + byte[] expected = [.. _bytes.Take(4)]; + + int offset = 0; + stream.WriteBothEndian(_bytes.ReadInt16BothEndian(ref offset)); + ValidateBytes(expected, stream.GetBuffer()); + } + + [Fact] + public void WriteUInt16Test() + { + var stream = new MemoryStream(new byte[16], 0, 16, true, true); + byte[] expected = [.. _bytes.Take(2)]; + bool write = stream.Write((ushort)0x0100); + Assert.True(write); + ValidateBytes(expected, stream.GetBuffer()); + } + + [Fact] + public void WriteUInt16BigEndianTest() + { + var stream = new MemoryStream(new byte[16], 0, 16, true, true); + byte[] expected = [.. _bytes.Take(2)]; + bool write = stream.WriteBigEndian((ushort)0x0001); + Assert.True(write); + ValidateBytes(expected, stream.GetBuffer()); + } + + [Fact] + public void WriteUInt16LittleEndianTest() + { + var stream = new MemoryStream(new byte[16], 0, 16, true, true); + byte[] expected = [.. _bytes.Take(2)]; + bool write = stream.WriteLittleEndian((ushort)0x0100); + Assert.True(write); + ValidateBytes(expected, stream.GetBuffer()); + } + + [Fact] + public void WriteUInt16BothEndianTest() + { + var stream = new MemoryStream(new byte[16], 0, 16, true, true); + byte[] expected = [.. _bytes.Take(4)]; + + int offset = 0; + stream.WriteBothEndian(_bytes.ReadUInt16BothEndian(ref offset)); + ValidateBytes(expected, stream.GetBuffer()); + } + + [Fact] + public void WriteHalfTest() + { + var stream = new MemoryStream(new byte[16], 0, 16, true, true); + byte[] expected = [.. _bytes.Take(2)]; + bool write = stream.Write(BitConverter.Int16BitsToHalf(0x0100)); + Assert.True(write); + ValidateBytes(expected, stream.GetBuffer()); + } + + [Fact] + public void WriteHalfBigEndianTest() + { + var stream = new MemoryStream(new byte[16], 0, 16, true, true); + byte[] expected = [.. _bytes.Take(2)]; + bool write = stream.WriteBigEndian(BitConverter.Int16BitsToHalf(0x0001)); + Assert.True(write); + ValidateBytes(expected, stream.GetBuffer()); + } + + [Fact] + public void WriteHalfLittleEndianTest() + { + var stream = new MemoryStream(new byte[16], 0, 16, true, true); + byte[] expected = [.. _bytes.Take(2)]; + bool write = stream.WriteLittleEndian(BitConverter.Int16BitsToHalf(0x0100)); + Assert.True(write); + ValidateBytes(expected, stream.GetBuffer()); + } + + [Fact] + public void WriteInt24Test() + { + var stream = new MemoryStream(new byte[16], 0, 16, true, true); + byte[] expected = [.. _bytes.Take(3)]; + bool write = stream.Write((Int24)0x020100); + Assert.True(write); + ValidateBytes(expected, stream.GetBuffer()); + } + + [Fact] + public void WriteInt24BigEndianTest() + { + var stream = new MemoryStream(new byte[16], 0, 16, true, true); + byte[] expected = [.. _bytes.Take(3)]; + bool write = stream.WriteBigEndian((Int24)0x000102); + Assert.True(write); + ValidateBytes(expected, stream.GetBuffer()); + } + + [Fact] + public void WriteInt24LittleEndianTest() + { + var stream = new MemoryStream(new byte[16], 0, 16, true, true); + byte[] expected = [.. _bytes.Take(3)]; + bool write = stream.WriteLittleEndian((Int24)0x020100); + Assert.True(write); + ValidateBytes(expected, stream.GetBuffer()); + } + + [Fact] + public void WriteUInt24Test() + { + var stream = new MemoryStream(new byte[16], 0, 16, true, true); + byte[] expected = [.. _bytes.Take(3)]; + bool write = stream.Write((UInt24)0x020100); + Assert.True(write); + ValidateBytes(expected, stream.GetBuffer()); + } + + [Fact] + public void WriteUInt24BigEndianTest() + { + var stream = new MemoryStream(new byte[16], 0, 16, true, true); + byte[] expected = [.. _bytes.Take(3)]; + bool write = stream.WriteBigEndian((UInt24)0x000102); + Assert.True(write); + ValidateBytes(expected, stream.GetBuffer()); + } + + [Fact] + public void WriteUInt24LittleEndianTest() + { + var stream = new MemoryStream(new byte[16], 0, 16, true, true); + byte[] expected = [.. _bytes.Take(3)]; + bool write = stream.WriteLittleEndian((UInt24)0x020100); + Assert.True(write); + ValidateBytes(expected, stream.GetBuffer()); + } + + [Fact] + public void WriteInt32Test() + { + var stream = new MemoryStream(new byte[16], 0, 16, true, true); + byte[] expected = [.. _bytes.Take(4)]; + bool write = stream.Write(0x03020100); + Assert.True(write); + ValidateBytes(expected, stream.GetBuffer()); + } + + [Fact] + public void WriteInt32BigEndianTest() + { + var stream = new MemoryStream(new byte[16], 0, 16, true, true); + byte[] expected = [.. _bytes.Take(4)]; + bool write = stream.WriteBigEndian(0x00010203); + Assert.True(write); + ValidateBytes(expected, stream.GetBuffer()); + } + + [Fact] + public void WriteInt32LittleEndianTest() + { + var stream = new MemoryStream(new byte[16], 0, 16, true, true); + byte[] expected = [.. _bytes.Take(4)]; + bool write = stream.WriteLittleEndian(0x03020100); + Assert.True(write); + ValidateBytes(expected, stream.GetBuffer()); + } + + [Fact] + public void WriteInt32BothEndianTest() + { + var stream = new MemoryStream(new byte[16], 0, 16, true, true); + byte[] expected = [.. _bytes.Take(8)]; + + int offset = 0; + stream.WriteBothEndian(_bytes.ReadInt32BothEndian(ref offset)); + ValidateBytes(expected, stream.GetBuffer()); + } + + [Fact] + public void WriteUInt32Test() + { + var stream = new MemoryStream(new byte[16], 0, 16, true, true); + byte[] expected = [.. _bytes.Take(4)]; + bool write = stream.Write((uint)0x03020100); + Assert.True(write); + ValidateBytes(expected, stream.GetBuffer()); + } + + [Fact] + public void WriteUInt32BigEndianTest() + { + var stream = new MemoryStream(new byte[16], 0, 16, true, true); + byte[] expected = [.. _bytes.Take(4)]; + bool write = stream.WriteBigEndian((uint)0x00010203); + Assert.True(write); + ValidateBytes(expected, stream.GetBuffer()); + } + + [Fact] + public void WriteUInt32LittleEndianTest() + { + var stream = new MemoryStream(new byte[16], 0, 16, true, true); + byte[] expected = [.. _bytes.Take(4)]; + bool write = stream.WriteLittleEndian((uint)0x03020100); + Assert.True(write); + ValidateBytes(expected, stream.GetBuffer()); + } + + [Fact] + public void WriteUInt32BothEndianTest() + { + var stream = new MemoryStream(new byte[16], 0, 16, true, true); + byte[] expected = [.. _bytes.Take(8)]; + + int offset = 0; + stream.WriteBothEndian(_bytes.ReadUInt32BothEndian(ref offset)); + ValidateBytes(expected, stream.GetBuffer()); + } + + [Fact] + public void WriteSingleTest() + { + var stream = new MemoryStream(new byte[16], 0, 16, true, true); + byte[] expected = [.. _bytes.Take(4)]; + bool write = stream.Write(BitConverter.Int32BitsToSingle(0x03020100)); + Assert.True(write); + ValidateBytes(expected, stream.GetBuffer()); + } + + [Fact] + public void WriteSingleBigEndianTest() + { + var stream = new MemoryStream(new byte[16], 0, 16, true, true); + byte[] expected = [.. _bytes.Take(4)]; + bool write = stream.WriteBigEndian(BitConverter.Int32BitsToSingle(0x00010203)); + Assert.True(write); + ValidateBytes(expected, stream.GetBuffer()); + } + + [Fact] + public void WriteSingleLittleEndianTest() + { + var stream = new MemoryStream(new byte[16], 0, 16, true, true); + byte[] expected = [.. _bytes.Take(4)]; + bool write = stream.WriteLittleEndian(BitConverter.Int32BitsToSingle(0x03020100)); + Assert.True(write); + ValidateBytes(expected, stream.GetBuffer()); + } + + [Fact] + public void WriteInt48Test() + { + var stream = new MemoryStream(new byte[16], 0, 16, true, true); + byte[] expected = [.. _bytes.Take(6)]; + bool write = stream.Write((Int48)0x050403020100); + Assert.True(write); + ValidateBytes(expected, stream.GetBuffer()); + } + + [Fact] + public void WriteInt48BigEndianTest() + { + var stream = new MemoryStream(new byte[16], 0, 16, true, true); + byte[] expected = [.. _bytes.Take(6)]; + bool write = stream.WriteBigEndian((Int48)0x000102030405); + Assert.True(write); + ValidateBytes(expected, stream.GetBuffer()); + } + + [Fact] + public void WriteInt48LittleEndianTest() + { + var stream = new MemoryStream(new byte[16], 0, 16, true, true); + byte[] expected = [.. _bytes.Take(6)]; + bool write = stream.WriteLittleEndian((Int48)0x050403020100); + Assert.True(write); + ValidateBytes(expected, stream.GetBuffer()); + } + + [Fact] + public void WriteUInt48Test() + { + var stream = new MemoryStream(new byte[16], 0, 16, true, true); + byte[] expected = [.. _bytes.Take(6)]; + bool write = stream.Write((UInt48)0x050403020100); + Assert.True(write); + ValidateBytes(expected, stream.GetBuffer()); + } + + [Fact] + public void WriteUInt48BigEndianTest() + { + var stream = new MemoryStream(new byte[16], 0, 16, true, true); + byte[] expected = [.. _bytes.Take(6)]; + bool write = stream.WriteBigEndian((UInt48)0x000102030405); + Assert.True(write); + ValidateBytes(expected, stream.GetBuffer()); + } + + [Fact] + public void WriteUInt48LittleEndianTest() + { + var stream = new MemoryStream(new byte[16], 0, 16, true, true); + byte[] expected = [.. _bytes.Take(6)]; + bool write = stream.WriteLittleEndian((UInt48)0x050403020100); + Assert.True(write); + ValidateBytes(expected, stream.GetBuffer()); + } + + [Fact] + public void WriteInt64Test() + { + var stream = new MemoryStream(new byte[16], 0, 16, true, true); + byte[] expected = [.. _bytes.Take(8)]; + bool write = stream.Write(0x0706050403020100); + Assert.True(write); + ValidateBytes(expected, stream.GetBuffer()); + } + + [Fact] + public void WriteInt64BigEndianTest() + { + var stream = new MemoryStream(new byte[16], 0, 16, true, true); + byte[] expected = [.. _bytes.Take(8)]; + bool write = stream.WriteBigEndian(0x0001020304050607); + Assert.True(write); + ValidateBytes(expected, stream.GetBuffer()); + } + + [Fact] + public void WriteInt64LittleEndianTest() + { + var stream = new MemoryStream(new byte[16], 0, 16, true, true); + byte[] expected = [.. _bytes.Take(8)]; + bool write = stream.WriteLittleEndian(0x0706050403020100); + Assert.True(write); + ValidateBytes(expected, stream.GetBuffer()); + } + + [Fact] + public void WriteInt64BothEndianTest() + { + var stream = new MemoryStream(new byte[16], 0, 16, true, true); + byte[] expected = [.. _bytes.Take(16)]; + + int offset = 0; + stream.WriteBothEndian(_bytes.ReadInt64BothEndian(ref offset)); + ValidateBytes(expected, stream.GetBuffer()); + } + + [Fact] + public void WriteUInt64Test() + { + var stream = new MemoryStream(new byte[16], 0, 16, true, true); + byte[] expected = [.. _bytes.Take(8)]; + bool write = stream.Write((ulong)0x0706050403020100); + Assert.True(write); + ValidateBytes(expected, stream.GetBuffer()); + } + + [Fact] + public void WriteUInt64BigEndianTest() + { + var stream = new MemoryStream(new byte[16], 0, 16, true, true); + byte[] expected = [.. _bytes.Take(8)]; + bool write = stream.WriteBigEndian((ulong)0x0001020304050607); + Assert.True(write); + ValidateBytes(expected, stream.GetBuffer()); + } + + [Fact] + public void WriteUInt64LittleEndianTest() + { + var stream = new MemoryStream(new byte[16], 0, 16, true, true); + byte[] expected = [.. _bytes.Take(8)]; + bool write = stream.WriteLittleEndian((ulong)0x0706050403020100); + Assert.True(write); + ValidateBytes(expected, stream.GetBuffer()); + } + + [Fact] + public void WriteUInt64BothEndianTest() + { + var stream = new MemoryStream(new byte[16], 0, 16, true, true); + byte[] expected = [.. _bytes.Take(16)]; + + int offset = 0; + stream.WriteBothEndian(_bytes.ReadUInt64BothEndian(ref offset)); + ValidateBytes(expected, stream.GetBuffer()); + } + + [Fact] + public void WriteDoubleTest() + { + var stream = new MemoryStream(new byte[16], 0, 16, true, true); + byte[] expected = [.. _bytes.Take(8)]; + bool write = stream.Write(BitConverter.Int64BitsToDouble(0x0706050403020100)); + Assert.True(write); + ValidateBytes(expected, stream.GetBuffer()); + } + + [Fact] + public void WriteDoubleBigEndianTest() + { + var stream = new MemoryStream(new byte[16], 0, 16, true, true); + byte[] expected = [.. _bytes.Take(8)]; + bool write = stream.WriteBigEndian(BitConverter.Int64BitsToDouble(0x0001020304050607)); + Assert.True(write); + ValidateBytes(expected, stream.GetBuffer()); + } + + [Fact] + public void WriteDoubleLittleEndianTest() + { + var stream = new MemoryStream(new byte[16], 0, 16, true, true); + byte[] expected = [.. _bytes.Take(8)]; + bool write = stream.WriteLittleEndian(BitConverter.Int64BitsToDouble(0x0706050403020100)); + Assert.True(write); + ValidateBytes(expected, stream.GetBuffer()); + } + + [Fact] + public void WriteGuidTest() + { + var stream = new MemoryStream(new byte[16], 0, 16, true, true); + byte[] expected = [.. _bytes.Take(16)]; + bool write = stream.Write(new Guid(_bytes)); + Assert.True(write); + ValidateBytes(expected, stream.GetBuffer()); + } + + [Fact] + public void WriteGuidBigEndianTest() + { + var stream = new MemoryStream(new byte[16], 0, 16, true, true); + byte[] expected = [.. _guidBigEndianbytes.Take(16)]; + bool write = stream.WriteBigEndian(new Guid(_bytes)); + Assert.True(write); + ValidateBytes(expected, stream.GetBuffer()); + } + + [Fact] + public void WriteGuidLittleEndianTest() + { + var stream = new MemoryStream(new byte[16], 0, 16, true, true); + byte[] expected = [.. _bytes.Take(16)]; + bool write = stream.WriteLittleEndian(new Guid(_bytes)); + Assert.True(write); + ValidateBytes(expected, stream.GetBuffer()); + } + + [Fact] + public void WriteInt128Test() + { + var stream = new MemoryStream(new byte[16], 0, 16, true, true); + byte[] expected = [.. _bytes.Take(16)]; + bool write = stream.Write((Int128)new BigInteger(_bytes)); + Assert.True(write); + ValidateBytes(expected, stream.GetBuffer()); + } + + [Fact] + public void WriteInt128BigEndianTest() + { + var stream = new MemoryStream(new byte[16], 0, 16, true, true); + byte[] expected = [.. _bytes.Take(16)]; + bool write = stream.WriteBigEndian((Int128)new BigInteger(Enumerable.Reverse(_bytes).ToArray())); + Assert.True(write); + ValidateBytes(expected, stream.GetBuffer()); + } + + [Fact] + public void WriteInt128LittleEndianTest() + { + var stream = new MemoryStream(new byte[16], 0, 16, true, true); + byte[] expected = [.. _bytes.Take(16)]; + bool write = stream.WriteLittleEndian((Int128)new BigInteger(_bytes)); + Assert.True(write); + ValidateBytes(expected, stream.GetBuffer()); + } + + [Fact] + public void WriteUInt128Test() + { + var stream = new MemoryStream(new byte[16], 0, 16, true, true); + byte[] expected = [.. _bytes.Take(16)]; + bool write = stream.Write((UInt128)new BigInteger(_bytes)); + Assert.True(write); + ValidateBytes(expected, stream.GetBuffer()); + } + + [Fact] + public void WriteUInt128BigEndianTest() + { + var stream = new MemoryStream(new byte[16], 0, 16, true, true); + byte[] expected = [.. _bytes.Take(16)]; + bool write = stream.WriteBigEndian((UInt128)new BigInteger(Enumerable.Reverse(_bytes).ToArray())); + Assert.True(write); + ValidateBytes(expected, stream.GetBuffer()); + } + + [Fact] + public void WriteUInt128LittleEndianTest() + { + var stream = new MemoryStream(new byte[16], 0, 16, true, true); + byte[] expected = [.. _bytes.Take(16)]; + bool write = stream.WriteLittleEndian((UInt128)new BigInteger(_bytes)); + Assert.True(write); + ValidateBytes(expected, stream.GetBuffer()); + } + + [Fact] + public void WriteDecimalTest() + { + var stream = new MemoryStream(new byte[16], 0, 16, true, true); + byte[] expected = [.. _decimalBytes.Take(16)]; + bool write = stream.Write(0.0123456789M); + Assert.True(write); + ValidateBytes(expected, stream.GetBuffer()); + } + + [Fact] + public void WriteDecimalBigEndianTest() + { + var stream = new MemoryStream(new byte[16], 0, 16, true, true); + byte[] expected = [.. _decimalBytes.Take(16).Reverse()]; + bool write = stream.WriteBigEndian(0.0123456789M); + Assert.True(write); + ValidateBytes(expected, stream.GetBuffer()); + } + + [Fact] + public void WriteDecimalLittleEndianTest() + { + var stream = new MemoryStream(new byte[16], 0, 16, true, true); + byte[] expected = [.. _decimalBytes.Take(16)]; + bool write = stream.WriteLittleEndian(0.0123456789M); + Assert.True(write); + ValidateBytes(expected, stream.GetBuffer()); + } + + /// + /// Validate that a set of actual bytes matches the expected bytes + /// + private static void ValidateBytes(byte[] expected, byte[] actual) + { + for (int i = 0; i < expected.Length; i++) + { + Assert.Equal(expected[i], actual[i]); + } + } + } +} diff --git a/SabreTools.Numerics.Extensions/BinaryReaderExtensions.cs b/SabreTools.Numerics.Extensions/BinaryReaderExtensions.cs new file mode 100644 index 0000000..3d85c67 --- /dev/null +++ b/SabreTools.Numerics.Extensions/BinaryReaderExtensions.cs @@ -0,0 +1,2466 @@ +using System; +using System.IO; + +namespace SabreTools.Numerics.Extensions +{ + /// + /// Extensions for BinaryReader + /// + public static class BinaryReaderExtensions + { + #region Exact Read + + /// + /// Reads in both-endian format + public static BothUInt8 ReadByteBothEndian(this BinaryReader reader) + { + byte le = reader.ReadByte(); + byte be = reader.ReadByte(); + return new BothUInt8(le, be); + } + + /// + /// Reads in both-endian format + public static BothInt8 ReadSByteBothEndian(this BinaryReader reader) + { + sbyte le = reader.ReadSByte(); + sbyte be = reader.ReadSByte(); + return new BothInt8(le, be); + } + + /// + /// Reads in big-endian format + public static short ReadInt16BigEndian(this BinaryReader reader) + { + byte[] buffer = reader.ReadBytes(2); + return buffer.ToInt16BigEndian(); + } + + /// + /// Reads in little-endian format + public static short ReadInt16LittleEndian(this BinaryReader reader) + { + byte[] buffer = reader.ReadBytes(2); + return buffer.ToInt16LittleEndian(); + } + + /// + /// Reads in both-endian format + public static BothInt16 ReadInt16BothEndian(this BinaryReader reader) + { + short le = reader.ReadInt16LittleEndian(); + short be = reader.ReadInt16BigEndian(); + return new BothInt16(le, be); + } + + /// + /// Reads in big-endian format + public static ushort ReadUInt16BigEndian(this BinaryReader reader) + { + byte[] buffer = reader.ReadBytes(2); + return buffer.ToUInt16BigEndian(); + } + + /// + /// Reads in little-endian format + public static ushort ReadUInt16LittleEndian(this BinaryReader reader) + { + byte[] buffer = reader.ReadBytes(2); + return buffer.ToUInt16LittleEndian(); + } + + /// + /// Reads in both-endian format + public static BothUInt16 ReadUInt16BothEndian(this BinaryReader reader) + { + ushort le = reader.ReadUInt16LittleEndian(); + ushort be = reader.ReadUInt16BigEndian(); + return new BothUInt16(le, be); + } + + /// + /// Read a WORD (2-byte) from the base stream + /// + public static ushort ReadWORD(this BinaryReader reader) + => reader.ReadUInt16(); + + /// + /// Read a WORD (2-byte) from the base stream + /// + /// Reads in big-endian format + public static ushort ReadWORDBigEndian(this BinaryReader reader) + => reader.ReadUInt16BigEndian(); + + /// + /// Read a WORD (2-byte) from the base stream + /// + /// Reads in little-endian format + public static ushort ReadWORDLittleEndian(this BinaryReader reader) + => reader.ReadUInt16LittleEndian(); + + /// + /// Read a WORD (2-byte) from the base stream + /// + /// Reads in both-endian format + public static BothUInt16 ReadWORDBothEndian(this BinaryReader reader) + => reader.ReadUInt16BothEndian(); + +#if NET5_0_OR_GREATER + /// + /// Read a Half from the underlying stream + /// + /// Reads in machine native format + public static Half ReadHalf(this BinaryReader reader) + { + if (BitConverter.IsLittleEndian) + return reader.ReadHalfLittleEndian(); + else + return reader.ReadHalfBigEndian(); + } + + /// + /// Reads in big-endian format + public static Half ReadHalfBigEndian(this BinaryReader reader) + { + byte[] buffer = reader.ReadBytes(2); + return buffer.ToHalfBigEndian(); + } + + /// + /// Reads in little-endian format + public static Half ReadHalfLittleEndian(this BinaryReader reader) + { + byte[] buffer = reader.ReadBytes(2); + return buffer.ToHalfLittleEndian(); + } +#endif + + /// + /// Read an Int24 from the base stream + /// + /// Reads in machine native format + public static Int24 ReadInt24(this BinaryReader reader) + { + if (BitConverter.IsLittleEndian) + return reader.ReadInt24LittleEndian(); + else + return reader.ReadInt24BigEndian(); + } + + /// + /// Read an Int24 from the base stream + /// + /// Reads in big-endian format + public static Int24 ReadInt24BigEndian(this BinaryReader reader) + { + byte[] buffer = reader.ReadBytes(3); + return buffer.ToInt24BigEndian(); + } + + /// + /// Read an Int24 from the base stream + /// + /// Reads in little-endian format + public static Int24 ReadInt24LittleEndian(this BinaryReader reader) + { + byte[] buffer = reader.ReadBytes(3); + return buffer.ToInt24LittleEndian(); + } + + /// + /// Read a UInt24 from the base stream + /// + /// Reads in machine native format + public static UInt24 ReadUInt24(this BinaryReader reader) + { + if (BitConverter.IsLittleEndian) + return reader.ReadUInt24LittleEndian(); + else + return reader.ReadUInt24BigEndian(); + } + + /// + /// Read a UInt24 from the base stream + /// + /// Reads in big-endian format + public static UInt24 ReadUInt24BigEndian(this BinaryReader reader) + { + byte[] buffer = reader.ReadBytes(3); + return buffer.ToUInt24BigEndian(); + } + + /// + /// Read a UInt24 from the base stream + /// + /// Reads in little-endian format + public static UInt24 ReadUInt24LittleEndian(this BinaryReader reader) + { + byte[] buffer = reader.ReadBytes(3); + return buffer.ToUInt24LittleEndian(); + } + + /// + /// Reads in big-endian format + public static int ReadInt32BigEndian(this BinaryReader reader) + { + byte[] buffer = reader.ReadBytes(4); + return buffer.ToInt32BigEndian(); + } + + /// + /// Reads in little-endian format + public static int ReadInt32LittleEndian(this BinaryReader reader) + { + byte[] buffer = reader.ReadBytes(4); + return buffer.ToInt32LittleEndian(); + } + + /// + /// Reads in both-endian format + public static BothInt32 ReadInt32BothEndian(this BinaryReader reader) + { + int le = reader.ReadInt32LittleEndian(); + int be = reader.ReadInt32BigEndian(); + return new BothInt32(le, be); + } + + /// + /// Reads in big-endian format + public static uint ReadUInt32BigEndian(this BinaryReader reader) + { + byte[] buffer = reader.ReadBytes(4); + return buffer.ToUInt32BigEndian(); + } + + /// + /// Reads in little-endian format + public static uint ReadUInt32LittleEndian(this BinaryReader reader) + { + byte[] buffer = reader.ReadBytes(4); + return buffer.ToUInt32LittleEndian(); + } + + /// + /// Reads in both-endian format + public static BothUInt32 ReadUInt32BothEndian(this BinaryReader reader) + { + uint le = reader.ReadUInt32LittleEndian(); + uint be = reader.ReadUInt32BigEndian(); + return new BothUInt32(le, be); + } + + /// + /// Read a DWORD (4-byte) from the base stream + /// + public static uint ReadDWORD(this BinaryReader reader) + => reader.ReadUInt32(); + + /// + /// Read a DWORD (4-byte) from the base stream + /// + /// Reads in big-endian format + public static uint ReadDWORDBigEndian(this BinaryReader reader) + => reader.ReadUInt32BigEndian(); + + /// + /// Read a DWORD (4-byte) from the base stream + /// + /// Reads in little-endian format + public static uint ReadDWORDLittleEndian(this BinaryReader reader) + => reader.ReadUInt32LittleEndian(); + + /// + /// Read a DWORD (4-byte) from the base stream + /// + /// Reads in both-endian format + public static BothUInt32 ReadDWORDBothEndian(this BinaryReader reader) + => reader.ReadUInt32BothEndian(); + + /// + /// Reads in big-endian format + public static float ReadSingleBigEndian(this BinaryReader reader) + { + byte[] buffer = reader.ReadBytes(4); + return buffer.ToSingleBigEndian(); + } + + /// + /// Reads in little-endian format + public static float ReadSingleLittleEndian(this BinaryReader reader) + { + byte[] buffer = reader.ReadBytes(4); + return buffer.ToSingleLittleEndian(); + } + + /// + /// Read an Int48 from the base stream + /// + /// Reads in machine native format + public static Int48 ReadInt48(this BinaryReader reader) + { + if (BitConverter.IsLittleEndian) + return reader.ReadInt48LittleEndian(); + else + return reader.ReadInt48BigEndian(); + } + + /// + /// Read an Int48 from the base stream + /// + /// Reads in big-endian format + public static Int48 ReadInt48BigEndian(this BinaryReader reader) + { + byte[] buffer = reader.ReadBytes(6); + return buffer.ToInt48BigEndian(); + } + + /// + /// Read an Int48 from the base stream + /// + /// Reads in little-endian format + public static Int48 ReadInt48LittleEndian(this BinaryReader reader) + { + byte[] buffer = reader.ReadBytes(6); + return buffer.ToInt48LittleEndian(); + } + + /// + /// Read a UInt48 from the base stream + /// + /// Reads in machine native format + public static UInt48 ReadUInt48(this BinaryReader reader) + { + if (BitConverter.IsLittleEndian) + return reader.ReadUInt48LittleEndian(); + else + return reader.ReadUInt48BigEndian(); + } + + /// + /// Read a UInt48 from the base stream + /// + /// Reads in big-endian format + public static UInt48 ReadUInt48BigEndian(this BinaryReader reader) + { + byte[] buffer = reader.ReadBytes(6); + return buffer.ToUInt48BigEndian(); + } + + /// + /// Read an UInt48 from the base stream + /// + /// Reads in little-endian format + public static UInt48 ReadUInt48LittleEndian(this BinaryReader reader) + { + byte[] buffer = reader.ReadBytes(6); + return buffer.ToUInt48LittleEndian(); + } + + /// + /// Reads in big-endian format + public static long ReadInt64BigEndian(this BinaryReader reader) + { + byte[] buffer = reader.ReadBytes(8); + return buffer.ToInt64BigEndian(); + } + + /// + /// Reads in little-endian format + public static long ReadInt64LittleEndian(this BinaryReader reader) + { + byte[] buffer = reader.ReadBytes(8); + return buffer.ToInt64LittleEndian(); + } + + /// + /// Reads in both-endian format + public static BothInt64 ReadInt64BothEndian(this BinaryReader reader) + { + long le = reader.ReadInt64LittleEndian(); + long be = reader.ReadInt64BigEndian(); + return new BothInt64(le, be); + } + + /// + /// Reads in big-endian format + public static ulong ReadUInt64BigEndian(this BinaryReader reader) + { + byte[] buffer = reader.ReadBytes(8); + return buffer.ToUInt64BigEndian(); + } + + /// + /// Reads in little-endian format + public static ulong ReadUInt64LittleEndian(this BinaryReader reader) + { + byte[] buffer = reader.ReadBytes(8); + return buffer.ToUInt64LittleEndian(); + } + + /// + /// Reads in both-endian format + public static BothUInt64 ReadUInt64BothEndian(this BinaryReader reader) + { + ulong le = reader.ReadUInt64LittleEndian(); + ulong be = reader.ReadUInt64BigEndian(); + return new BothUInt64(le, be); + } + + /// + /// Read a QWORD (8-byte) from the base stream + /// + /// Reads in machine native format + public static ulong ReadQWORD(this BinaryReader reader) + => reader.ReadUInt64(); + + /// + /// Read a QWORD (8-byte) from the base stream + /// + /// Reads in big-endian format + public static ulong ReadQWORDBigEndian(this BinaryReader reader) + => reader.ReadUInt64BigEndian(); + + /// + /// Read a QWORD (8-byte) from the base stream + /// + /// Reads in little-endian format + public static ulong ReadQWORDLittleEndian(this BinaryReader reader) + => reader.ReadUInt64LittleEndian(); + + /// + /// Read a QWORD (8-byte) from the base stream + /// + /// Reads in both-endian format + public static BothUInt64 ReadQWORDBothEndian(this BinaryReader reader) + => reader.ReadUInt64BothEndian(); + + /// + /// Reads in big-endian format + public static double ReadDoubleBigEndian(this BinaryReader reader) + { + byte[] buffer = reader.ReadBytes(8); + return buffer.ToDoubleBigEndian(); + } + + /// + /// Reads in little-endian format + public static double ReadDoubleLittleEndian(this BinaryReader reader) + { + byte[] buffer = reader.ReadBytes(8); + return buffer.ToDoubleLittleEndian(); + } + + /// + /// Read a Guid from the underlying stream + /// + /// Reads in machine native format + public static Guid ReadGuid(this BinaryReader reader) + { + if (BitConverter.IsLittleEndian) + return reader.ReadGuidLittleEndian(); + else + return reader.ReadGuidBigEndian(); + } + + /// + /// Read a Guid from the underlying stream + /// + /// Reads in big-endian format + public static Guid ReadGuidBigEndian(this BinaryReader reader) + { + byte[] buffer = reader.ReadBytes(16); + return buffer.ToGuidBigEndian(); + } + + /// + /// Read a Guid from the underlying stream + /// + /// Reads in little-endian format + public static Guid ReadGuidLittleEndian(this BinaryReader reader) + { + byte[] buffer = reader.ReadBytes(16); + return buffer.ToGuidLittleEndian(); + } + +#if NET7_0_OR_GREATER + /// + /// Read an Int128 from the underlying stream + /// + /// Reads in machine native format + public static Int128 ReadInt128(this BinaryReader reader) + { + if (BitConverter.IsLittleEndian) + return reader.ReadInt128LittleEndian(); + else + return reader.ReadInt128BigEndian(); + } + + /// + /// Read an Int128 from the underlying stream + /// + /// Reads in big-endian format + public static Int128 ReadInt128BigEndian(this BinaryReader reader) + { + byte[] buffer = reader.ReadBytes(16); + return buffer.ToInt128BigEndian(); + } + + /// + /// Read an Int128 from the underlying stream + /// + /// Reads in little-endian format + public static Int128 ReadInt128LittleEndian(this BinaryReader reader) + { + byte[] buffer = reader.ReadBytes(16); + return buffer.ToInt128LittleEndian(); + } + + /// + /// Read a UInt128 from the underlying stream + /// + /// Reads in machine native format + public static UInt128 ReadUInt128(this BinaryReader reader) + { + if (BitConverter.IsLittleEndian) + return reader.ReadUInt128LittleEndian(); + else + return reader.ReadUInt128BigEndian(); + } + + /// + /// Read a UInt128 from the underlying stream + /// + /// Reads in big-endian format + public static UInt128 ReadUInt128BigEndian(this BinaryReader reader) + { + byte[] buffer = reader.ReadBytes(16); + return buffer.ToUInt128BigEndian(); + } + + /// + /// Read a UInt128 from the underlying stream + /// + /// Reads in little-endian format + public static UInt128 ReadUInt128LittleEndian(this BinaryReader reader) + { + byte[] buffer = reader.ReadBytes(16); + return buffer.ToUInt128LittleEndian(); + } +#endif + + /// + /// Reads in big-endian format + public static decimal ReadDecimalBigEndian(this BinaryReader reader) + { + byte[] buffer = reader.ReadBytes(16); + return buffer.ToDecimalBigEndian(); + } + + /// + /// Reads in little-endian format + public static decimal ReadDecimalLittleEndian(this BinaryReader reader) + { + byte[] buffer = reader.ReadBytes(16); + return buffer.ToDecimalLittleEndian(); + } + + #endregion + + #region Peek Read + + /// + /// Peek a UInt8 from the base stream + /// + /// Only works properly on seekable streams + public static byte PeekByte(this BinaryReader reader) + { + byte value = reader.ReadByte(); + reader.BaseStream.Seek(-1, SeekOrigin.Current); + return value; + } + + /// + /// Peek a UInt8 from the base stream + /// + /// Only works properly on seekable streams + public static byte PeekByteValue(this BinaryReader reader) + => reader.PeekByte(); + + /// + /// Peek a UInt8 from the base stream + /// + /// Reads in both-endian format + /// Only works properly on seekable streams + public static BothUInt8 PeekByteBothEndian(this BinaryReader reader) + { + BothUInt8 value = reader.ReadByteBothEndian(); + reader.BaseStream.Seek(-2, SeekOrigin.Current); + return value; + } + + /// + /// Peek a UInt8[] from the base stream + /// + /// Only works properly on seekable streams + public static byte[] PeekBytes(this BinaryReader reader, int count) + { + byte[] value = reader.ReadBytes(count); + reader.BaseStream.Seek(-count, SeekOrigin.Current); + return value; + } + + /// + /// Peek an Int8 from the base stream + /// + /// Only works properly on seekable streams + public static sbyte PeekSByte(this BinaryReader reader) + { + sbyte value = reader.ReadSByte(); + reader.BaseStream.Seek(-1, SeekOrigin.Current); + return value; + } + + /// + /// Peek a Int8 from the base stream + /// + /// Reads in both-endian format + /// Only works properly on seekable streams + public static BothInt8 PeekSByteBothEndian(this BinaryReader reader) + { + BothInt8 value = reader.ReadSByteBothEndian(); + reader.BaseStream.Seek(-2, SeekOrigin.Current); + return value; + } + + /// + /// Peek an Int16 from the base stream + /// + /// Reads in machine native format + /// Only works properly on seekable streams + public static short PeekInt16(this BinaryReader reader) + { + if (BitConverter.IsLittleEndian) + return reader.PeekInt16LittleEndian(); + else + return reader.PeekInt16BigEndian(); + } + + /// + /// Peek an Int16 from the base stream + /// + /// Reads in big-endian format + /// Only works properly on seekable streams + public static short PeekInt16BigEndian(this BinaryReader reader) + { + short value = reader.ReadInt16BigEndian(); + reader.BaseStream.Seek(-2, SeekOrigin.Current); + return value; + } + + /// + /// Peek an Int16 from the base stream + /// + /// Reads in little-endian format + /// Only works properly on seekable streams + public static short PeekInt16LittleEndian(this BinaryReader reader) + { + short value = reader.ReadInt16LittleEndian(); + reader.BaseStream.Seek(-2, SeekOrigin.Current); + return value; + } + + /// + /// Peek a Int16 from the base stream + /// + /// Reads in both-endian format + /// Only works properly on seekable streams + public static BothInt16 PeekInt16BothEndian(this BinaryReader reader) + { + BothInt16 value = reader.ReadInt16BothEndian(); + reader.BaseStream.Seek(-4, SeekOrigin.Current); + return value; + } + + /// + /// Peek a UInt16 from the base stream + /// + /// Reads in machine native format + /// Only works properly on seekable streams + public static ushort PeekUInt16(this BinaryReader reader) + { + if (BitConverter.IsLittleEndian) + return reader.PeekUInt16LittleEndian(); + else + return reader.PeekUInt16BigEndian(); + } + + /// + /// Peek a UInt16 from the base stream + /// + /// Reads in big-endian format + /// Only works properly on seekable streams + public static ushort PeekUInt16BigEndian(this BinaryReader reader) + { + ushort value = reader.ReadUInt16BigEndian(); + reader.BaseStream.Seek(-2, SeekOrigin.Current); + return value; + } + + /// + /// Peek a UInt16 from the base stream + /// + /// Reads in little-endian format + /// Only works properly on seekable streams + public static ushort PeekUInt16LittleEndian(this BinaryReader reader) + { + ushort value = reader.ReadUInt16LittleEndian(); + reader.BaseStream.Seek(-2, SeekOrigin.Current); + return value; + } + + /// + /// Peek a UInt16 from the base stream + /// + /// Reads in both-endian format + /// Only works properly on seekable streams + public static BothUInt16 PeekUInt16BothEndian(this BinaryReader reader) + { + BothUInt16 value = reader.ReadUInt16BothEndian(); + reader.BaseStream.Seek(-4, SeekOrigin.Current); + return value; + } + + /// + /// Peek a WORD (2-byte) from the base stream + /// + /// Reads in machine native format + /// Only works properly on seekable streams + public static ushort PeekWORD(this BinaryReader reader) + => reader.PeekUInt16(); + + /// + /// Peek a WORD (2-byte) from the base stream + /// + /// Reads in big-endian format + /// Only works properly on seekable streams + public static ushort PeekWORDBigEndian(this BinaryReader reader) + => reader.PeekUInt16BigEndian(); + + /// + /// Peek a WORD (2-byte) from the base stream + /// + /// Reads in little-endian format + /// Only works properly on seekable streams + public static ushort PeekWORDLittleEndian(this BinaryReader reader) + => reader.PeekUInt16LittleEndian(); + + /// + /// Peek a WORD (2-byte) from the base stream + /// + /// Reads in both-endian format + /// Only works properly on seekable streams + public static BothUInt16 PeekWORDBothEndian(this BinaryReader reader) + => reader.PeekUInt16BothEndian(); + +#if NET5_0_OR_GREATER + /// + /// Peek a Half from the base stream + /// + /// Reads in machine native format + /// Only works properly on seekable streams + public static Half PeekHalf(this BinaryReader reader) + { + if (BitConverter.IsLittleEndian) + return reader.PeekHalfLittleEndian(); + else + return reader.PeekHalfBigEndian(); + } + + /// + /// Peek a Half from the base stream + /// + /// Reads in big-endian format + /// Only works properly on seekable streams + public static Half PeekHalfBigEndian(this BinaryReader reader) + { + Half value = reader.ReadHalfBigEndian(); + reader.BaseStream.Seek(-2, SeekOrigin.Current); + return value; + } + + /// + /// Peek a Half from the base stream + /// + /// Reads in little-endian format + /// Only works properly on seekable streams + public static Half PeekHalfLittleEndian(this BinaryReader reader) + { + Half value = reader.ReadHalfLittleEndian(); + reader.BaseStream.Seek(-2, SeekOrigin.Current); + return value; + } +#endif + + /// + /// Peek an Int24 from the base stream + /// + /// Reads in machine native format + /// Only works properly on seekable streams + public static Int24 PeekInt24(this BinaryReader reader) + { + if (BitConverter.IsLittleEndian) + return reader.PeekInt24LittleEndian(); + else + return reader.PeekInt24BigEndian(); + } + + /// + /// Peek an Int24 from the base stream + /// + /// Reads in big-endian format + /// Only works properly on seekable streams + public static Int24 PeekInt24BigEndian(this BinaryReader reader) + { + Int24 value = reader.ReadInt24BigEndian(); + reader.BaseStream.Seek(-3, SeekOrigin.Current); + return value; + } + + /// + /// Peek an Int24 from the base stream + /// + /// Reads in little-endian format + /// Only works properly on seekable streams + public static Int24 PeekInt24LittleEndian(this BinaryReader reader) + { + Int24 value = reader.ReadInt24LittleEndian(); + reader.BaseStream.Seek(-3, SeekOrigin.Current); + return value; + } + + /// + /// Peek a UInt24 from the base stream + /// + /// Reads in machine native format + /// Only works properly on seekable streams + public static UInt24 PeekUInt24(this BinaryReader reader) + { + if (BitConverter.IsLittleEndian) + return reader.PeekUInt24LittleEndian(); + else + return reader.PeekUInt24BigEndian(); + } + + /// + /// Peek a UInt24 from the base stream + /// + /// Reads in big-endian format + /// Only works properly on seekable streams + public static UInt24 PeekUInt24BigEndian(this BinaryReader reader) + { + UInt24 value = reader.ReadUInt24BigEndian(); + reader.BaseStream.Seek(-3, SeekOrigin.Current); + return value; + } + + /// + /// Peek a UInt24 from the base stream + /// + /// Reads in little-endian format + /// Only works properly on seekable streams + public static UInt24 PeekUInt24LittleEndian(this BinaryReader reader) + { + UInt24 value = reader.ReadUInt24LittleEndian(); + reader.BaseStream.Seek(-3, SeekOrigin.Current); + return value; + } + + /// + /// Peek an Int32 from the base stream + /// + /// Reads in machine native format + /// Only works properly on seekable streams + public static int PeekInt32(this BinaryReader reader) + { + if (BitConverter.IsLittleEndian) + return reader.PeekInt32LittleEndian(); + else + return reader.PeekInt32BigEndian(); + } + + /// + /// Peek an Int32 from the base stream + /// + /// Reads in big-endian format + /// Only works properly on seekable streams + public static int PeekInt32BigEndian(this BinaryReader reader) + { + int value = reader.ReadInt32BigEndian(); + reader.BaseStream.Seek(-4, SeekOrigin.Current); + return value; + } + + /// + /// Peek an Int32 from the base stream + /// + /// Reads in little-endian format + /// Only works properly on seekable streams + public static int PeekInt32LittleEndian(this BinaryReader reader) + { + int value = reader.ReadInt32LittleEndian(); + reader.BaseStream.Seek(-4, SeekOrigin.Current); + return value; + } + + /// + /// Peek a Int32 from the base stream + /// + /// Reads in both-endian format + /// Only works properly on seekable streams + public static BothInt32 PeekInt32BothEndian(this BinaryReader reader) + { + BothInt32 value = reader.ReadInt32BothEndian(); + reader.BaseStream.Seek(-8, SeekOrigin.Current); + return value; + } + + /// + /// Peek a UInt32 from the base stream + /// + /// Reads in machine native format + /// Only works properly on seekable streams + public static uint PeekUInt32(this BinaryReader reader) + { + if (BitConverter.IsLittleEndian) + return reader.PeekUInt32LittleEndian(); + else + return reader.PeekUInt32BigEndian(); + } + + /// + /// Peek a UInt32 from the base stream + /// + /// Reads in big-endian format + /// Only works properly on seekable streams + public static uint PeekUInt32BigEndian(this BinaryReader reader) + { + uint value = reader.ReadUInt32BigEndian(); + reader.BaseStream.Seek(-4, SeekOrigin.Current); + return value; + } + + /// + /// Peek a UInt32 from the base stream + /// + /// Reads in little-endian format + /// Only works properly on seekable streams + public static uint PeekUInt32LittleEndian(this BinaryReader reader) + { + uint value = reader.ReadUInt32LittleEndian(); + reader.BaseStream.Seek(-4, SeekOrigin.Current); + return value; + } + + /// + /// Peek a UInt32 from the base stream + /// + /// Reads in both-endian format + /// Only works properly on seekable streams + public static BothUInt32 PeekUInt32BothEndian(this BinaryReader reader) + { + BothUInt32 value = reader.ReadUInt32BothEndian(); + reader.BaseStream.Seek(-8, SeekOrigin.Current); + return value; + } + + /// + /// Peek a DWORD (4-byte) from the base stream + /// + /// Reads in machine native format + /// Only works properly on seekable streams + public static uint PeekDWORD(this BinaryReader reader) + => reader.PeekUInt32(); + + /// + /// Peek a DWORD (4-byte) from the base stream + /// + /// Reads in big-endian format + /// Only works properly on seekable streams + public static uint PeekDWORDBigEndian(this BinaryReader reader) + => reader.PeekUInt32BigEndian(); + + /// + /// Peek a DWORD (4-byte) from the base stream + /// + /// Reads in little-endian format + /// Only works properly on seekable streams + public static uint PeekDWORDLittleEndian(this BinaryReader reader) + => reader.PeekUInt32LittleEndian(); + + /// + /// Peek a DWORD (4-byte) from the base stream + /// + /// Reads in both-endian format + /// Only works properly on seekable streams + public static BothUInt32 PeekDWORDBothEndian(this BinaryReader reader) + => reader.PeekUInt32BothEndian(); + + /// + /// Peek a Single from the base stream + /// + /// Reads in machine native format + /// Only works properly on seekable streams + public static float PeekSingle(this BinaryReader reader) + { + if (BitConverter.IsLittleEndian) + return reader.PeekSingleLittleEndian(); + else + return reader.PeekSingleBigEndian(); + } + + /// + /// Peek a Single from the base stream + /// + /// Reads in big-endian format + /// Only works properly on seekable streams + public static float PeekSingleBigEndian(this BinaryReader reader) + { + float value = reader.ReadSingleBigEndian(); + reader.BaseStream.Seek(-4, SeekOrigin.Current); + return value; + } + + /// + /// Peek a Single from the base stream + /// + /// Reads in little-endian format + /// Only works properly on seekable streams + public static float PeekSingleLittleEndian(this BinaryReader reader) + { + float value = reader.ReadSingleLittleEndian(); + reader.BaseStream.Seek(-4, SeekOrigin.Current); + return value; + } + + /// + /// Peek an Int48 from the base stream + /// + /// Reads in machine native format + /// Only works properly on seekable streams + public static Int48 PeekInt48(this BinaryReader reader) + { + if (BitConverter.IsLittleEndian) + return reader.PeekInt48LittleEndian(); + else + return reader.PeekInt48BigEndian(); + } + + /// + /// Peek an Int48 from the base stream + /// + /// Reads in big-endian format + /// Only works properly on seekable streams + public static Int48 PeekInt48BigEndian(this BinaryReader reader) + { + Int48 value = reader.ReadInt48BigEndian(); + reader.BaseStream.Seek(-6, SeekOrigin.Current); + return value; + } + + /// + /// Peek an Int48 from the base stream + /// + /// Reads in little-endian format + /// Only works properly on seekable streams + public static Int48 PeekInt48LittleEndian(this BinaryReader reader) + { + Int48 value = reader.ReadInt48LittleEndian(); + reader.BaseStream.Seek(-6, SeekOrigin.Current); + return value; + } + + /// + /// Peek a UInt48 from the base stream + /// + /// Reads in machine native format + /// Only works properly on seekable streams + public static UInt48 PeekUInt48(this BinaryReader reader) + { + if (BitConverter.IsLittleEndian) + return reader.PeekUInt48LittleEndian(); + else + return reader.PeekUInt48BigEndian(); + } + + /// + /// Peek an UInt48 from the base stream + /// + /// Reads in big-endian format + /// Only works properly on seekable streams + public static UInt48 PeekUInt48BigEndian(this BinaryReader reader) + { + UInt48 value = reader.ReadUInt48BigEndian(); + reader.BaseStream.Seek(-6, SeekOrigin.Current); + return value; + } + + /// + /// Peek an UInt48 from the base stream + /// + /// Reads in little-endian format + /// Only works properly on seekable streams + public static UInt48 PeekUInt48LittleEndian(this BinaryReader reader) + { + UInt48 value = reader.ReadUInt48LittleEndian(); + reader.BaseStream.Seek(-6, SeekOrigin.Current); + return value; + } + + /// + /// Peek an Int64 from the base stream + /// + /// Reads in machine native format + /// Only works properly on seekable streams + public static long PeekInt64(this BinaryReader reader) + { + if (BitConverter.IsLittleEndian) + return reader.PeekInt64LittleEndian(); + else + return reader.PeekInt64BigEndian(); + } + + /// + /// Peek an Int64 from the base stream + /// + /// Reads in big-endian format + /// Only works properly on seekable streams + public static long PeekInt64BigEndian(this BinaryReader reader) + { + long value = reader.ReadInt64BigEndian(); + reader.BaseStream.Seek(-8, SeekOrigin.Current); + return value; + } + + /// + /// Peek an Int64 from the base stream + /// + /// Reads in big-endian format + /// Only works properly on seekable streams + public static long PeekInt64LittleEndian(this BinaryReader reader) + { + long value = reader.ReadInt64LittleEndian(); + reader.BaseStream.Seek(-8, SeekOrigin.Current); + return value; + } + + /// + /// Peek a Int64 from the base stream + /// + /// Reads in both-endian format + /// Only works properly on seekable streams + public static BothInt64 PeekInt64BothEndian(this BinaryReader reader) + { + BothInt64 value = reader.ReadInt64BothEndian(); + reader.BaseStream.Seek(-16, SeekOrigin.Current); + return value; + } + + /// + /// Peek a UInt64 from the base stream + /// + /// Reads in machine native format + /// Only works properly on seekable streams + public static ulong PeekUInt64(this BinaryReader reader) + { + if (BitConverter.IsLittleEndian) + return reader.PeekUInt64LittleEndian(); + else + return reader.PeekUInt64BigEndian(); + } + + /// + /// Peek a UInt64 from the base stream + /// + /// Reads in big-endian format + /// Only works properly on seekable streams + public static ulong PeekUInt64BigEndian(this BinaryReader reader) + { + ulong value = reader.ReadUInt64BigEndian(); + reader.BaseStream.Seek(-8, SeekOrigin.Current); + return value; + } + + /// + /// Peek a UInt64 from the base stream + /// + /// Reads in little-endian format + /// Only works properly on seekable streams + public static ulong PeekUInt64LittleEndian(this BinaryReader reader) + { + ulong value = reader.ReadUInt64LittleEndian(); + reader.BaseStream.Seek(-8, SeekOrigin.Current); + return value; + } + + /// + /// Peek a UInt64 from the base stream + /// + /// Reads in both-endian format + /// Only works properly on seekable streams + public static BothUInt64 PeekUInt64BothEndian(this BinaryReader reader) + { + BothUInt64 value = reader.ReadUInt64BothEndian(); + reader.BaseStream.Seek(-16, SeekOrigin.Current); + return value; + } + + /// + /// Peek a QWORD (8-byte) from the base stream + /// + /// Reads in machine native format + /// Only works properly on seekable streams + public static ulong PeekQWORD(this BinaryReader reader) + => reader.PeekUInt64(); + + /// + /// Peek a QWORD (8-byte) from the base stream + /// + /// Reads in big-endian format + /// Only works properly on seekable streams + public static ulong PeekQWORDBigEndian(this BinaryReader reader) + => reader.PeekUInt64BigEndian(); + + /// + /// Peek a QWORD (8-byte) from the base stream + /// + /// Reads in little-endian format + /// Only works properly on seekable streams + public static ulong PeekQWORDLittleEndian(this BinaryReader reader) + => reader.PeekUInt64LittleEndian(); + + /// + /// Peek a QWORD (8-byte) from the base stream + /// + /// Reads in both-endian format + /// Only works properly on seekable streams + public static BothUInt64 PeekQWORDBothEndian(this BinaryReader reader) + => reader.PeekUInt64BothEndian(); + + /// + /// Peek a Double from the base stream + /// + /// Reads in machine native format + /// Only works properly on seekable streams + public static double PeekDouble(this BinaryReader reader) + { + if (BitConverter.IsLittleEndian) + return reader.PeekDoubleLittleEndian(); + else + return reader.PeekDoubleBigEndian(); + } + + /// + /// Peek a Double from the base stream + /// + /// Reads in big-endian format + /// Only works properly on seekable streams + public static double PeekDoubleBigEndian(this BinaryReader reader) + { + double value = reader.ReadDoubleBigEndian(); + reader.BaseStream.Seek(-8, SeekOrigin.Current); + return value; + } + + /// + /// Peek a Double from the base stream + /// + /// Reads in little-endian format + /// Only works properly on seekable streams + public static double PeekDoubleLittleEndian(this BinaryReader reader) + { + double value = reader.ReadDoubleLittleEndian(); + reader.BaseStream.Seek(-8, SeekOrigin.Current); + return value; + } + + /// + /// Peek a Guid from the base stream + /// + /// Reads in machine native format + /// Only works properly on seekable streams + public static Guid PeekGuid(this BinaryReader reader) + { + if (BitConverter.IsLittleEndian) + return reader.PeekGuidLittleEndian(); + else + return reader.PeekGuidBigEndian(); + } + + /// + /// Peek a Guid from the base stream + /// + /// Reads in big-endian format + /// Only works properly on seekable streams + public static Guid PeekGuidBigEndian(this BinaryReader reader) + { + Guid value = reader.ReadGuidBigEndian(); + reader.BaseStream.Seek(-16, SeekOrigin.Current); + return value; + } + + /// + /// Peek a Guid from the base stream + /// + /// Reads in little-endian format + /// Only works properly on seekable streams + public static Guid PeekGuidLittleEndian(this BinaryReader reader) + { + Guid value = reader.ReadGuidLittleEndian(); + reader.BaseStream.Seek(-16, SeekOrigin.Current); + return value; + } + +#if NET7_0_OR_GREATER + /// + /// Peek an Int128 from the base stream + /// + /// Reads in machine native format + /// Only works properly on seekable streams + public static Int128 PeekInt128(this BinaryReader reader) + { + if (BitConverter.IsLittleEndian) + return reader.PeekInt128LittleEndian(); + else + return reader.PeekInt128BigEndian(); + } + + /// + /// Peek an Int128 from the base stream + /// + /// Reads in big-endian format + /// Only works properly on seekable streams + public static Int128 PeekInt128BigEndian(this BinaryReader reader) + { + Int128 value = reader.ReadInt128BigEndian(); + reader.BaseStream.Seek(-16, SeekOrigin.Current); + return value; + } + + /// + /// Peek an Int128 from the base stream + /// + /// Reads in little-endian format + /// Only works properly on seekable streams + public static Int128 PeekInt128LittleEndian(this BinaryReader reader) + { + Int128 value = reader.ReadInt128LittleEndian(); + reader.BaseStream.Seek(-16, SeekOrigin.Current); + return value; + } + + /// + /// Peek a UInt128 from the base stream + /// + /// Reads in machine native format + /// Only works properly on seekable streams + public static UInt128 PeekUInt128(this BinaryReader reader) + { + if (BitConverter.IsLittleEndian) + return reader.PeekUInt128LittleEndian(); + else + return reader.PeekUInt128BigEndian(); + } + + /// + /// Peek a UInt128 from the base stream + /// + /// Reads in big-endian format + /// Only works properly on seekable streams + public static UInt128 PeekUInt128BigEndian(this BinaryReader reader) + { + UInt128 value = reader.ReadUInt128BigEndian(); + reader.BaseStream.Seek(-16, SeekOrigin.Current); + return value; + } + + /// + /// Peek a UInt128 from the base stream + /// + /// Reads in little-endian format + /// Only works properly on seekable streams + public static UInt128 PeekUInt128LittleEndian(this BinaryReader reader) + { + UInt128 value = reader.ReadUInt128LittleEndian(); + reader.BaseStream.Seek(-16, SeekOrigin.Current); + return value; + } +#endif + + /// + /// Peek a Decimal from the base stream + /// + /// Reads in machine native format + /// Only works properly on seekable streams + public static decimal PeekDecimal(this BinaryReader reader) + { + if (BitConverter.IsLittleEndian) + return reader.PeekDecimalLittleEndian(); + else + return reader.PeekDecimalBigEndian(); + } + + /// + /// Peek a Decimal from the base stream + /// + /// Reads in big-endian format + /// Only works properly on seekable streams + public static decimal PeekDecimalBigEndian(this BinaryReader reader) + { + decimal value = reader.ReadDecimalBigEndian(); + reader.BaseStream.Seek(-16, SeekOrigin.Current); + return value; + } + + /// + /// Peek a Decimal from the base stream + /// + /// Reads in little-endian format + /// Only works properly on seekable streams + public static decimal PeekDecimalLittleEndian(this BinaryReader reader) + { + decimal value = reader.ReadDecimalLittleEndian(); + reader.BaseStream.Seek(-16, SeekOrigin.Current); + return value; + } + + #endregion + + #region Try Read + + /// + /// Read a UInt8 from the base stream + /// + public static bool TryReadByteValue(this BinaryReader reader, out byte value) + { + if (reader.BaseStream.Position > reader.BaseStream.Length - 1) + { + value = default; + return false; + } + + value = reader.ReadByte(); + return true; + } + + /// + /// Read a UInt8 from the base stream + /// + /// Reads in both-endian format + public static bool TryReadByteBothEndian(this BinaryReader reader, out BothUInt8 value) + { + if (reader.BaseStream.Position > reader.BaseStream.Length - 2) + { + value = default(byte); + return false; + } + + value = reader.ReadByteBothEndian(); + return true; + } + + /// + /// Read a UInt8[] from the base stream + /// + public static bool TryReadBytes(this BinaryReader reader, int count, out byte[] value) + { + if (reader.BaseStream.Position > reader.BaseStream.Length - count) + { + value = []; + return false; + } + + value = reader.ReadBytes(count); + return true; + } + + /// + /// Read an Int8 from the base stream + /// + public static bool TryReadSByte(this BinaryReader reader, out sbyte value) + { + if (reader.BaseStream.Position > reader.BaseStream.Length - 1) + { + value = default; + return false; + } + + value = reader.ReadSByte(); + return true; + } + + /// + /// Read a Int8 from the base stream + /// + /// Reads in both-endian format + public static bool TryReadSByteBothEndian(this BinaryReader reader, out BothInt8 value) + { + if (reader.BaseStream.Position > reader.BaseStream.Length - 2) + { + value = default(sbyte); + return false; + } + + value = reader.ReadSByteBothEndian(); + return true; + } + + /// + /// Read a Char from the base stream + /// + public static bool TryReadChar(this BinaryReader reader, out char value) + { + if (reader.BaseStream.Position > reader.BaseStream.Length - 1) + { + value = default; + return false; + } + + value = reader.ReadChar(); + return true; + } + + /// + /// Read an Int16 from the base stream + /// + /// Reads in machine native format + public static bool TryReadInt16(this BinaryReader reader, out short value) + { + if (BitConverter.IsLittleEndian) + return reader.TryReadInt16LittleEndian(out value); + else + return reader.TryReadInt16BigEndian(out value); + } + + /// + /// Read an Int16 from the base stream + /// + /// Reads in big-endian format + public static bool TryReadInt16BigEndian(this BinaryReader reader, out short value) + { + if (reader.BaseStream.Position > reader.BaseStream.Length - 2) + { + value = default; + return false; + } + + value = reader.ReadInt16BigEndian(); + return true; + } + + /// + /// Read an Int16 from the base stream + /// + /// Reads in little-endian format + public static bool TryReadInt16LittleEndian(this BinaryReader reader, out short value) + { + if (reader.BaseStream.Position > reader.BaseStream.Length - 2) + { + value = default; + return false; + } + + value = reader.ReadInt16LittleEndian(); + return true; + } + + /// + /// Read a Int16 from the base stream + /// + /// Reads in both-endian format + public static bool TryReadInt16BothEndian(this BinaryReader reader, out BothInt16 value) + { + if (reader.BaseStream.Position > reader.BaseStream.Length - 4) + { + value = default(short); + return false; + } + + value = reader.ReadInt16BothEndian(); + return true; + } + + /// + /// Read a UInt16 from the base stream + /// + /// Reads in machine native format + public static bool TryReadUInt16(this BinaryReader reader, out ushort value) + { + if (BitConverter.IsLittleEndian) + return reader.TryReadUInt16LittleEndian(out value); + else + return reader.TryReadUInt16BigEndian(out value); + } + + /// + /// Read a UInt16 from the base stream + /// + /// Reads in big-endian format + public static bool TryReadUInt16BigEndian(this BinaryReader reader, out ushort value) + { + if (reader.BaseStream.Position > reader.BaseStream.Length - 2) + { + value = default; + return false; + } + + value = reader.ReadUInt16BigEndian(); + return true; + } + + /// + /// Read a UInt16 from the base stream + /// + /// Reads in little-endian format + public static bool TryReadUInt16LittleEndian(this BinaryReader reader, out ushort value) + { + if (reader.BaseStream.Position > reader.BaseStream.Length - 2) + { + value = default; + return false; + } + + value = reader.ReadUInt16LittleEndian(); + return true; + } + + /// + /// Read a UInt16 from the base stream + /// + /// Reads in both-endian format + public static bool TryReadUInt16BothEndian(this BinaryReader reader, out BothUInt16 value) + { + if (reader.BaseStream.Position > reader.BaseStream.Length - 4) + { + value = default(ushort); + return false; + } + + value = reader.ReadUInt16BothEndian(); + return true; + } + + /// + /// Read a WORD (2-byte) from the base stream + /// + /// Reads in machine native format + public static bool TryReadWORD(this BinaryReader reader, out ushort value) + => reader.TryReadUInt16(out value); + + /// + /// Read a WORD (2-byte) from the base stream + /// + /// Reads in big-endian format + public static bool TryReadWORDBigEndian(this BinaryReader reader, out ushort value) + => reader.TryReadUInt16BigEndian(out value); + + /// + /// Read a WORD (2-byte) from the base stream + /// + /// Reads in little-endian format + public static bool TryReadWORDLittleEndian(this BinaryReader reader, out ushort value) + => reader.TryReadUInt16LittleEndian(out value); + + /// + /// Read a WORD (2-byte) from the base stream + /// + /// Reads in both-endian format + public static bool TryReadWORDBothEndian(this BinaryReader reader, out BothUInt16 value) + => reader.TryReadUInt16BothEndian(out value); + +#if NET5_0_OR_GREATER + /// + /// Read a Half from the base stream + /// + /// Reads in machine native format + public static bool TryReadHalf(this BinaryReader reader, out Half value) + { + if (BitConverter.IsLittleEndian) + return reader.TryReadHalfLittleEndian(out value); + else + return reader.TryReadHalfBigEndian(out value); + } + + /// + /// Read a Half from the base stream + /// + /// Reads in big-endian format + public static bool TryReadHalfBigEndian(this BinaryReader reader, out Half value) + { + if (reader.BaseStream.Position > reader.BaseStream.Length - 2) + { + value = default; + return false; + } + + value = reader.ReadHalfBigEndian(); + return true; + } + + /// + /// Read a Half from the base stream + /// + /// Reads in little-endian format + public static bool TryReadHalfLittleEndian(this BinaryReader reader, out Half value) + { + if (reader.BaseStream.Position > reader.BaseStream.Length - 2) + { + value = default; + return false; + } + + value = reader.ReadHalfLittleEndian(); + return true; + } +#endif + + /// + /// Read an Int24 from the base stream + /// + /// Reads in machine native format + public static bool TryReadInt24(this BinaryReader reader, out Int24 value) + { + if (BitConverter.IsLittleEndian) + return reader.TryReadInt24LittleEndian(out value); + else + return reader.TryReadInt24BigEndian(out value); + } + + /// + /// Read an Int24 from the base stream + /// + /// Reads in big-endian format + public static bool TryReadInt24BigEndian(this BinaryReader reader, out Int24 value) + { + if (reader.BaseStream.Position > reader.BaseStream.Length - 3) + { + value = new Int24(); + return false; + } + + value = reader.ReadInt24BigEndian(); + return true; + } + + /// + /// Read an Int24 from the base stream + /// + /// Reads in little-endian format + public static bool TryReadInt24LittleEndian(this BinaryReader reader, out Int24 value) + { + if (reader.BaseStream.Position > reader.BaseStream.Length - 3) + { + value = new Int24(); + return false; + } + + value = reader.ReadInt24LittleEndian(); + return true; + } + + /// + /// Read a UInt24 from the base stream + /// + /// Reads in machine native format + public static bool TryReadUInt24(this BinaryReader reader, out UInt24 value) + { + if (BitConverter.IsLittleEndian) + return reader.TryReadUInt24LittleEndian(out value); + else + return reader.TryReadUInt24BigEndian(out value); + } + + /// + /// Read a UInt24 from the base stream + /// + /// Reads in big-endian format + public static bool TryReadUInt24BigEndian(this BinaryReader reader, out UInt24 value) + { + if (reader.BaseStream.Position > reader.BaseStream.Length - 3) + { + value = new UInt24(); + return false; + } + + value = reader.ReadUInt24BigEndian(); + return true; + } + + /// + /// Read a UInt24 from the base stream + /// + /// Reads in little-endian format + public static bool TryReadUInt24LittleEndian(this BinaryReader reader, out UInt24 value) + { + if (reader.BaseStream.Position > reader.BaseStream.Length - 3) + { + value = new UInt24(); + return false; + } + + value = reader.ReadUInt24LittleEndian(); + return true; + } + + /// + /// Read an Int32 from the base stream + /// + /// Reads in machine native format + public static bool TryReadInt32(this BinaryReader reader, out int value) + { + if (BitConverter.IsLittleEndian) + return reader.TryReadInt32LittleEndian(out value); + else + return reader.TryReadInt32BigEndian(out value); + } + + /// + /// Read an Int32 from the base stream + /// + /// Reads in big-endian format + public static bool TryReadInt32BigEndian(this BinaryReader reader, out int value) + { + if (reader.BaseStream.Position > reader.BaseStream.Length - 4) + { + value = default; + return false; + } + + value = reader.ReadInt32BigEndian(); + return true; + } + + /// + /// Read an Int32 from the base stream + /// + /// Reads in little-endian format + public static bool TryReadInt32LittleEndian(this BinaryReader reader, out int value) + { + if (reader.BaseStream.Position > reader.BaseStream.Length - 4) + { + value = default; + return false; + } + + value = reader.ReadInt32LittleEndian(); + return true; + } + + /// + /// Read a Int32 from the base stream + /// + /// Reads in both-endian format + public static bool TryReadInt32BothEndian(this BinaryReader reader, out BothInt32 value) + { + if (reader.BaseStream.Position > reader.BaseStream.Length - 8) + { + value = default(int); + return false; + } + + value = reader.ReadInt32BothEndian(); + return true; + } + + /// + /// Read a UInt32 from the base stream + /// + /// Reads in machine native format + public static bool TryReadUInt32(this BinaryReader reader, out uint value) + { + if (BitConverter.IsLittleEndian) + return reader.TryReadUInt32LittleEndian(out value); + else + return reader.TryReadUInt32BigEndian(out value); + } + + /// + /// Read a UInt32 from the base stream + /// + /// Reads in big-endian format + public static bool TryReadUInt32BigEndian(this BinaryReader reader, out uint value) + { + if (reader.BaseStream.Position > reader.BaseStream.Length - 4) + { + value = default; + return false; + } + + value = reader.ReadUInt32BigEndian(); + return true; + } + + /// + /// Read a UInt32 from the base stream + /// + /// Reads in little-endian format + public static bool TryReadUInt32LittleEndian(this BinaryReader reader, out uint value) + { + if (reader.BaseStream.Position > reader.BaseStream.Length - 4) + { + value = default; + return false; + } + + value = reader.ReadUInt32LittleEndian(); + return true; + } + + /// + /// Read a UInt32 from the base stream + /// + /// Reads in both-endian format + public static bool TryReadUInt32BothEndian(this BinaryReader reader, out BothUInt32 value) + { + if (reader.BaseStream.Position > reader.BaseStream.Length - 8) + { + value = default(uint); + return false; + } + + value = reader.ReadUInt32BothEndian(); + return true; + } + + /// + /// Read a DWORD (4-byte) from the base stream + /// + /// Reads in machine native format + public static bool TryReadDWORD(this BinaryReader reader, out uint value) + => reader.TryReadUInt32(out value); + + /// + /// Read a DWORD (4-byte) from the base stream + /// + /// Reads in big-endian format + public static bool TryReadDWORDBigEndian(this BinaryReader reader, out uint value) + => reader.TryReadUInt32BigEndian(out value); + + /// + /// Read a DWORD (4-byte) from the base stream + /// + /// Reads in little-endian format + public static bool TryReadDWORDLittleEndian(this BinaryReader reader, out uint value) + => reader.TryReadUInt32LittleEndian(out value); + + /// + /// Read a DWORD (4-byte) from the base stream + /// + /// Reads in both-endian format + public static bool TryReadDWORDBothEndian(this BinaryReader reader, out BothUInt32 value) + => reader.TryReadUInt32BothEndian(out value); + + /// + /// Read a Single from the base stream + /// + /// Reads in machine native format + public static bool TryReadSingle(this BinaryReader reader, out float value) + { + if (BitConverter.IsLittleEndian) + return reader.TryReadSingleLittleEndian(out value); + else + return reader.TryReadSingleBigEndian(out value); + } + + /// + /// Read a Single from the base stream + /// + /// Reads in big-endian format + public static bool TryReadSingleBigEndian(this BinaryReader reader, out float value) + { + if (reader.BaseStream.Position > reader.BaseStream.Length - 4) + { + value = default; + return false; + } + + value = reader.ReadSingleBigEndian(); + return true; + } + + /// + /// Read a Single from the base stream + /// + /// Reads in little-endian format + public static bool TryReadSingleLittleEndian(this BinaryReader reader, out float value) + { + if (reader.BaseStream.Position > reader.BaseStream.Length - 4) + { + value = default; + return false; + } + + value = reader.ReadSingleLittleEndian(); + return true; + } + + /// + /// Read an Int48 from the base stream + /// + /// Reads in machine native format + public static bool TryReadInt48(this BinaryReader reader, out Int48 value) + { + if (BitConverter.IsLittleEndian) + return reader.TryReadInt48LittleEndian(out value); + else + return reader.TryReadInt48BigEndian(out value); + } + + /// + /// Read an Int48 from the base stream + /// + /// Reads in big-endian format + public static bool TryReadInt48BigEndian(this BinaryReader reader, out Int48 value) + { + if (reader.BaseStream.Position > reader.BaseStream.Length - 6) + { + value = new Int48(); + return false; + } + + value = reader.ReadInt48BigEndian(); + return true; + } + + /// + /// Read an Int48 from the base stream + /// + /// Reads in little-endian format + public static bool TryReadInt48LittleEndian(this BinaryReader reader, out Int48 value) + { + if (reader.BaseStream.Position > reader.BaseStream.Length - 6) + { + value = new Int48(); + return false; + } + + value = reader.ReadInt48LittleEndian(); + return true; + } + + /// + /// Read a UInt48 from the base stream + /// + /// Reads in machine native format + public static bool TryReadUInt48(this BinaryReader reader, out UInt48 value) + { + if (BitConverter.IsLittleEndian) + return reader.TryReadUInt48LittleEndian(out value); + else + return reader.TryReadUInt48BigEndian(out value); + } + + /// + /// Read a UInt48 from the base stream + /// + /// Reads in big-endian format + public static bool TryReadUInt48BigEndian(this BinaryReader reader, out UInt48 value) + { + if (reader.BaseStream.Position > reader.BaseStream.Length - 6) + { + value = new UInt48(); + return false; + } + + value = reader.ReadUInt48BigEndian(); + return true; + } + + /// + /// Read an UInt48 from the base stream + /// + /// Reads in little-endian format + public static bool TryReadUInt48LittleEndian(this BinaryReader reader, out UInt48 value) + { + if (reader.BaseStream.Position > reader.BaseStream.Length - 6) + { + value = new UInt48(); + return false; + } + + value = reader.ReadUInt48LittleEndian(); + return true; + } + + /// + /// Read an Int64 from the base stream + /// + /// Reads in machine native format + public static bool TryReadInt64(this BinaryReader reader, out long value) + { + if (BitConverter.IsLittleEndian) + return reader.TryReadInt64LittleEndian(out value); + else + return reader.TryReadInt64BigEndian(out value); + } + + /// + /// Read an Int64 from the base stream + /// + /// Reads in big-endian format + public static bool TryReadInt64BigEndian(this BinaryReader reader, out long value) + { + if (reader.BaseStream.Position > reader.BaseStream.Length - 8) + { + value = default; + return false; + } + + value = reader.ReadInt64BigEndian(); + return true; + } + + /// + /// Read an Int64 from the base stream + /// + /// Reads in little-endian format + public static bool TryReadInt64LittleEndian(this BinaryReader reader, out long value) + { + if (reader.BaseStream.Position > reader.BaseStream.Length - 8) + { + value = default; + return false; + } + + value = reader.ReadInt64BigEndian(); + return true; + } + + /// + /// Read a Int64 from the base stream + /// + /// Reads in both-endian format + public static bool TryReadInt64BothEndian(this BinaryReader reader, out BothInt64 value) + { + if (reader.BaseStream.Position > reader.BaseStream.Length - 16) + { + value = default(long); + return false; + } + + value = reader.ReadInt64BothEndian(); + return true; + } + + /// + /// Read a UInt64 from the base stream + /// + /// Reads in machine native format + public static bool TryReadUInt64(this BinaryReader reader, out ulong value) + { + if (BitConverter.IsLittleEndian) + return reader.TryReadUInt64LittleEndian(out value); + else + return reader.TryReadUInt64BigEndian(out value); + } + + /// + /// Read a UInt64 from the base stream + /// + /// Reads in big-endian format + public static bool TryReadUInt64BigEndian(this BinaryReader reader, out ulong value) + { + if (reader.BaseStream.Position > reader.BaseStream.Length - 8) + { + value = default; + return false; + } + + value = reader.ReadUInt64BigEndian(); + return true; + } + + /// + /// Read a UInt64 from the base stream + /// + /// Reads in little-endian format + public static bool TryReadUInt64LittleEndian(this BinaryReader reader, out ulong value) + { + if (reader.BaseStream.Position > reader.BaseStream.Length - 8) + { + value = default; + return false; + } + + value = reader.ReadUInt64LittleEndian(); + return true; + } + + /// + /// Read a UInt64 from the base stream + /// + /// Reads in both-endian format + public static bool TryReadUInt64BothEndian(this BinaryReader reader, out BothUInt64 value) + { + if (reader.BaseStream.Position > reader.BaseStream.Length - 16) + { + value = default(ulong); + return false; + } + + value = reader.ReadUInt64BothEndian(); + return true; + } + + /// + /// Read a QWORD (8-byte) from the base stream + /// + /// Reads in machine native format + public static bool TryReadQWORD(this BinaryReader reader, out ulong value) + => reader.TryReadUInt64(out value); + + /// + /// Read a QWORD (8-byte) from the base stream + /// + /// Reads in big-endian format + public static bool TryReadQWORDBigEndian(this BinaryReader reader, out ulong value) + => reader.TryReadUInt64BigEndian(out value); + + /// + /// Read a QWORD (8-byte) from the base stream + /// + /// Reads in little-endian format + public static bool TryReadQWORDLittleEndian(this BinaryReader reader, out ulong value) + => reader.TryReadUInt64LittleEndian(out value); + + /// + /// Read a QWORD (8-byte) from the base stream + /// + /// Reads in both-endian format + public static bool TryReadQWORDBothEndian(this BinaryReader reader, out BothUInt64 value) + => reader.TryReadUInt64BothEndian(out value); + + /// + /// Read a Double from the base stream + /// + /// Reads in machine native format + public static bool TryReadDouble(this BinaryReader reader, out double value) + { + if (BitConverter.IsLittleEndian) + return reader.TryReadDoubleLittleEndian(out value); + else + return reader.TryReadDoubleBigEndian(out value); + } + + /// + /// Read a Double from the base stream + /// + /// Reads in big-endian format + public static bool TryReadDoubleBigEndian(this BinaryReader reader, out double value) + { + if (reader.BaseStream.Position > reader.BaseStream.Length - 8) + { + value = default; + return false; + } + + value = reader.ReadDoubleBigEndian(); + return true; + } + + /// + /// Read a Double from the base stream + /// + /// Reads in little-endian format + public static bool TryReadDoubleLittleEndian(this BinaryReader reader, out double value) + { + if (reader.BaseStream.Position > reader.BaseStream.Length - 8) + { + value = default; + return false; + } + + value = reader.ReadDoubleLittleEndian(); + return true; + } + + /// + /// Read a Guid from the base stream + /// + /// Reads in machine native format + public static bool TryReadGuid(this BinaryReader reader, out Guid value) + { + if (BitConverter.IsLittleEndian) + return reader.TryReadGuidLittleEndian(out value); + else + return reader.TryReadGuidBigEndian(out value); + } + + /// + /// Read a Guid from the base stream + /// + /// Reads in big-endian format + public static bool TryReadGuidBigEndian(this BinaryReader reader, out Guid value) + { + if (reader.BaseStream.Position > reader.BaseStream.Length - 16) + { + value = default; + return false; + } + + value = reader.ReadGuidBigEndian(); + return true; + } + + /// + /// Read a Guid from the base stream + /// + /// Reads in big-endian format + public static bool TryReadGuidLittleEndian(this BinaryReader reader, out Guid value) + { + if (reader.BaseStream.Position > reader.BaseStream.Length - 16) + { + value = default; + return false; + } + + value = reader.ReadGuidLittleEndian(); + return true; + } + +#if NET7_0_OR_GREATER + /// + /// Read an Int128 from the base stream + /// + /// Reads in machine native format + public static bool TryReadInt128(this BinaryReader reader, out Int128 value) + { + if (BitConverter.IsLittleEndian) + return reader.TryReadInt128LittleEndian(out value); + else + return reader.TryReadInt128BigEndian(out value); + } + + /// + /// Read an Int128 from the base stream + /// + /// Reads in big-endian format + public static bool TryReadInt128BigEndian(this BinaryReader reader, out Int128 value) + { + if (reader.BaseStream.Position > reader.BaseStream.Length - 16) + { + value = default; + return false; + } + + value = reader.ReadInt128BigEndian(); + return true; + } + + /// + /// Read an Int128 from the base stream + /// + /// Reads in little-endian format + public static bool TryReadInt128LittleEndian(this BinaryReader reader, out Int128 value) + { + if (reader.BaseStream.Position > reader.BaseStream.Length - 16) + { + value = default; + return false; + } + + value = reader.ReadInt128LittleEndian(); + return true; + } + + /// + /// Read a UInt128 from the base stream + /// + /// Reads in machine native format + public static bool TryReadUInt128(this BinaryReader reader, out UInt128 value) + { + if (BitConverter.IsLittleEndian) + return reader.TryReadUInt128LittleEndian(out value); + else + return reader.TryReadUInt128BigEndian(out value); + } + + /// + /// Read a UInt128 from the base stream + /// + /// Reads in big-endian format + public static bool TryReadUInt128BigEndian(this BinaryReader reader, out UInt128 value) + { + if (reader.BaseStream.Position > reader.BaseStream.Length - 16) + { + value = default; + return false; + } + + value = reader.ReadUInt128BigEndian(); + return true; + } + + /// + /// Read a UInt128 from the base stream + /// + /// Reads in little-endian format + public static bool TryReadUInt128LittleEndian(this BinaryReader reader, out UInt128 value) + { + if (reader.BaseStream.Position > reader.BaseStream.Length - 16) + { + value = default; + return false; + } + + value = reader.ReadUInt128LittleEndian(); + return true; + } +#endif + + /// + /// Read a Decimal from the base stream + /// + /// Reads in machine native format + public static bool TryReadDecimal(this BinaryReader reader, out decimal value) + { + if (BitConverter.IsLittleEndian) + return reader.TryReadDecimalLittleEndian(out value); + else + return reader.TryReadDecimalBigEndian(out value); + } + + /// + /// Read a Decimal from the base stream + /// + /// Reads in big-endian format + public static bool TryReadDecimalBigEndian(this BinaryReader reader, out decimal value) + { + if (reader.BaseStream.Position > reader.BaseStream.Length - 16) + { + value = default; + return false; + } + + value = reader.ReadDecimalBigEndian(); + return true; + } + + /// + /// Read a Decimal from the base stream + /// + /// Reads in little-endian format + public static bool TryReadDecimalLittleEndian(this BinaryReader reader, out decimal value) + { + if (reader.BaseStream.Position > reader.BaseStream.Length - 16) + { + value = default; + return false; + } + + value = reader.ReadDecimalLittleEndian(); + return true; + } + + #endregion + } +} diff --git a/SabreTools.Numerics.Extensions/BinaryWriterExtensions.cs b/SabreTools.Numerics.Extensions/BinaryWriterExtensions.cs new file mode 100644 index 0000000..59bf848 --- /dev/null +++ b/SabreTools.Numerics.Extensions/BinaryWriterExtensions.cs @@ -0,0 +1,517 @@ +using System; +using System.IO; +using System.Text; + +namespace SabreTools.Numerics.Extensions +{ + /// + /// Extensions for BinaryWriter + /// + public static class BinaryWriterExtensions + { + /// + /// Writes in both-endian format + public static bool WriteBothEndian(this BinaryWriter writer, BothUInt8 value) + { + writer.Write(value.LittleEndian); + writer.Write(value.BigEndian); + return true; + } + + /// + /// Writes in both-endian format + public static bool WriteBothEndian(this BinaryWriter writer, BothInt8 value) + { + writer.Write(value.LittleEndian); + writer.Write(value.BigEndian); + return true; + } + + /// + /// Writes in big-endian format + public static bool WriteBigEndian(this BinaryWriter writer, byte[] value) + { + Array.Reverse(value); + return WriteFromBuffer(writer, value); + } + + /// + public static bool Write(this BinaryWriter writer, char value, Encoding encoding) + { + byte[] buffer = encoding.GetBytes($"{value}"); + return WriteFromBuffer(writer, buffer); + } + + /// + /// Writes in big-endian format + public static bool WriteBigEndian(this BinaryWriter writer, short value) + { + byte[] buffer = value.GetBytesBigEndian(); + return WriteFromBuffer(writer, buffer); + } + + /// + /// Writes in little-endian format + public static bool WriteLittleEndian(this BinaryWriter writer, short value) + { + byte[] buffer = value.GetBytesLittleEndian(); + return WriteFromBuffer(writer, buffer); + } + + /// + /// Writes in both-endian format + public static bool WriteBothEndian(this BinaryWriter writer, BothInt16 value) + { + writer.WriteLittleEndian(value.LittleEndian); + writer.WriteBigEndian(value.BigEndian); + return true; + } + + /// + /// Writes in big-endian format + public static bool WriteBigEndian(this BinaryWriter writer, ushort value) + { + byte[] buffer = value.GetBytesBigEndian(); + return WriteFromBuffer(writer, buffer); + } + + /// + /// Writes in little-endian format + public static bool WriteLittleEndian(this BinaryWriter writer, ushort value) + { + byte[] buffer = value.GetBytesLittleEndian(); + return WriteFromBuffer(writer, buffer); + } + + /// + /// Writes in both-endian format + public static bool WriteBothEndian(this BinaryWriter writer, BothUInt16 value) + { + writer.WriteLittleEndian(value.LittleEndian); + writer.WriteBigEndian(value.BigEndian); + return true; + } + +#if NET5_0_OR_GREATER + /// + /// Write a Half to the underlying stream + /// + /// Writes in machine native format + public static bool Write(this BinaryWriter writer, Half value) + { + if (BitConverter.IsLittleEndian) + return writer.WriteLittleEndian(value); + else + return writer.WriteBigEndian(value); + } + + /// + /// Writes in big-endian format + public static bool WriteBigEndian(this BinaryWriter writer, Half value) + { + byte[] buffer = value.GetBytesBigEndian(); + return WriteFromBuffer(writer, buffer); + } + + /// + /// Writes in little-endian format + public static bool WriteLittleEndian(this BinaryWriter writer, Half value) + { + byte[] buffer = value.GetBytesLittleEndian(); + return WriteFromBuffer(writer, buffer); + } +#endif + + /// + /// Write an Int24 to the underlying stream + /// + /// Writes in machine native format + public static bool Write(this BinaryWriter writer, Int24 value) + { + if (BitConverter.IsLittleEndian) + return writer.WriteLittleEndian(value); + else + return writer.WriteBigEndian(value); + } + + /// + /// Write an Int24 to the underlying stream + /// + /// Writes in big-endian format + public static bool WriteBigEndian(this BinaryWriter writer, Int24 value) + { + byte[] buffer = value.GetBytesBigEndian(); + return WriteFromBuffer(writer, buffer); + } + + /// + /// Write an Int24 to the underlying stream + /// + /// Writes in little-endian format + public static bool WriteLittleEndian(this BinaryWriter writer, Int24 value) + { + byte[] buffer = value.GetBytesLittleEndian(); + return WriteFromBuffer(writer, buffer); + } + + /// + /// Write a UInt24 to the underlying stream + /// + /// Writes in machine native format + public static bool Write(this BinaryWriter writer, UInt24 value) + { + if (BitConverter.IsLittleEndian) + return writer.WriteLittleEndian(value); + else + return writer.WriteBigEndian(value); + } + + /// + /// Write a UInt24 to the underlying stream + /// + /// Writes in big-endian format + public static bool WriteBigEndian(this BinaryWriter writer, UInt24 value) + { + byte[] buffer = value.GetBytesBigEndian(); + return WriteFromBuffer(writer, buffer); + } + + /// + /// Write a UInt24 to the underlying stream + /// + /// Writes in little-endian format + public static bool WriteLittleEndian(this BinaryWriter writer, UInt24 value) + { + byte[] buffer = value.GetBytesLittleEndian(); + return WriteFromBuffer(writer, buffer); + } + + /// + /// Writes in big-endian format + public static bool WriteBigEndian(this BinaryWriter writer, int value) + { + byte[] buffer = value.GetBytesBigEndian(); + return WriteFromBuffer(writer, buffer); + } + + /// + /// Writes in little-endian format + public static bool WriteLittleEndian(this BinaryWriter writer, int value) + { + byte[] buffer = value.GetBytesLittleEndian(); + return WriteFromBuffer(writer, buffer); + } + + /// + /// Writes in both-endian format + public static bool WriteBothEndian(this BinaryWriter writer, BothInt32 value) + { + writer.WriteLittleEndian(value.LittleEndian); + writer.WriteBigEndian(value.BigEndian); + return true; + } + + /// + /// Writes in big-endian format + public static bool WriteBigEndian(this BinaryWriter writer, uint value) + { + byte[] buffer = value.GetBytesBigEndian(); + return WriteFromBuffer(writer, buffer); + } + + /// + /// Writes in little-endian format + public static bool WriteLittleEndian(this BinaryWriter writer, uint value) + { + byte[] buffer = value.GetBytesLittleEndian(); + return WriteFromBuffer(writer, buffer); + } + + /// + /// Writes in both-endian format + public static bool WriteBothEndian(this BinaryWriter writer, BothUInt32 value) + { + writer.WriteLittleEndian(value.LittleEndian); + writer.WriteBigEndian(value.BigEndian); + return true; + } + + /// + /// Writes in big-endian format + public static bool WriteBigEndian(this BinaryWriter writer, float value) + { + byte[] buffer = value.GetBytesBigEndian(); + return WriteFromBuffer(writer, buffer); + } + + /// + /// Writes in little-endian format + public static bool WriteLittleEndian(this BinaryWriter writer, float value) + { + byte[] buffer = value.GetBytesLittleEndian(); + return WriteFromBuffer(writer, buffer); + } + + /// + /// Write an Int48 to the underlying stream + /// + /// Writes in machine native format + public static bool Write(this BinaryWriter writer, Int48 value) + { + if (BitConverter.IsLittleEndian) + return writer.WriteLittleEndian(value); + else + return writer.WriteBigEndian(value); + } + + /// + /// Write an Int48 to the underlying stream + /// + /// Writes in big-endian format + public static bool WriteBigEndian(this BinaryWriter writer, Int48 value) + { + byte[] buffer = value.GetBytesBigEndian(); + return WriteFromBuffer(writer, buffer); + } + + /// + /// Write an Int48 to the underlying stream + /// + /// Writes in little-endian format + public static bool WriteLittleEndian(this BinaryWriter writer, Int48 value) + { + byte[] buffer = value.GetBytesLittleEndian(); + return WriteFromBuffer(writer, buffer); + } + + /// + /// Write a UInt48 to the underlying stream + /// + /// Writes in machine native format + public static bool Write(this BinaryWriter writer, UInt48 value) + { + if (BitConverter.IsLittleEndian) + return writer.WriteLittleEndian(value); + else + return writer.WriteBigEndian(value); + } + + /// + /// Write a UInt48 to the underlying stream + /// + /// Writes in big-endian format + public static bool WriteBigEndian(this BinaryWriter writer, UInt48 value) + { + byte[] buffer = value.GetBytesBigEndian(); + return WriteFromBuffer(writer, buffer); + } + + /// + /// Write a UInt48 to the underlying stream + /// + /// Writes in little-endian format + public static bool WriteLittleEndian(this BinaryWriter writer, UInt48 value) + { + byte[] buffer = value.GetBytesLittleEndian(); + return WriteFromBuffer(writer, buffer); + } + + /// + /// Writes in big-endian format + public static bool WriteBigEndian(this BinaryWriter writer, long value) + { + byte[] buffer = value.GetBytesBigEndian(); + return WriteFromBuffer(writer, buffer); + } + + /// + /// Writes in little-endian format + public static bool WriteLittleEndian(this BinaryWriter writer, long value) + { + byte[] buffer = value.GetBytesLittleEndian(); + return WriteFromBuffer(writer, buffer); + } + + /// + /// Writes in both-endian format + public static bool WriteBothEndian(this BinaryWriter writer, BothInt64 value) + { + writer.WriteLittleEndian(value.LittleEndian); + writer.WriteBigEndian(value.BigEndian); + return true; + } + + /// + /// Writes in big-endian format + public static bool WriteBigEndian(this BinaryWriter writer, ulong value) + { + byte[] buffer = value.GetBytesBigEndian(); + return WriteFromBuffer(writer, buffer); + } + + /// + /// Writes in little-endian format + public static bool WriteLittleEndian(this BinaryWriter writer, ulong value) + { + byte[] buffer = value.GetBytesLittleEndian(); + return WriteFromBuffer(writer, buffer); + } + + /// + /// Writes in both-endian format + public static bool WriteBothEndian(this BinaryWriter writer, BothUInt64 value) + { + writer.WriteLittleEndian(value.LittleEndian); + writer.WriteBigEndian(value.BigEndian); + return true; + } + + /// + /// Writes in big-endian format + public static bool WriteBigEndian(this BinaryWriter writer, double value) + { + byte[] buffer = value.GetBytesBigEndian(); + return WriteFromBuffer(writer, buffer); + } + + /// + /// Writes in little-endian format + public static bool WriteLittleEndian(this BinaryWriter writer, double value) + { + byte[] buffer = value.GetBytesLittleEndian(); + return WriteFromBuffer(writer, buffer); + } + + /// + /// Write a Guid + /// + /// Writes in machine native format + public static bool Write(this BinaryWriter writer, Guid value) + { + if (BitConverter.IsLittleEndian) + return writer.WriteLittleEndian(value); + else + return writer.WriteBigEndian(value); + } + + /// + /// Write a Guid + /// + /// Writes in big-endian format + public static bool WriteBigEndian(this BinaryWriter writer, Guid value) + { + byte[] buffer = value.GetBytesBigEndian(); + return WriteFromBuffer(writer, buffer); + } + + /// + /// Write a Guid + /// + /// Writes in big-endian format + public static bool WriteLittleEndian(this BinaryWriter writer, Guid value) + { + byte[] buffer = value.GetBytesLittleEndian(); + return WriteFromBuffer(writer, buffer); + } + +#if NET7_0_OR_GREATER + /// + /// Write an Int128 to the underlying stream + /// + /// Writes in machine native format + public static bool Write(this BinaryWriter writer, Int128 value) + { + if (BitConverter.IsLittleEndian) + return writer.WriteLittleEndian(value); + else + return writer.WriteBigEndian(value); + } + + /// + /// Write an Int128 to the underlying stream + /// + /// Writes in big-endian format + public static bool WriteBigEndian(this BinaryWriter writer, Int128 value) + { + byte[] buffer = value.GetBytesBigEndian(); + return WriteFromBuffer(writer, buffer); + } + + /// + /// Write an Int128 to the underlying stream + /// + /// Writes in little-endian format + public static bool WriteLittleEndian(this BinaryWriter writer, Int128 value) + { + byte[] buffer = value.GetBytesLittleEndian(); + return WriteFromBuffer(writer, buffer); + } + + /// + /// Write a UInt128 to the underlying stream + /// + /// Writes in machine native format + public static bool Write(this BinaryWriter writer, UInt128 value) + { + if (BitConverter.IsLittleEndian) + return writer.WriteLittleEndian(value); + else + return writer.WriteBigEndian(value); + } + + /// + /// Write a UInt128 to the underlying stream + /// + /// Writes in big-endian format + public static bool WriteBigEndian(this BinaryWriter writer, UInt128 value) + { + byte[] buffer = value.GetBytesBigEndian(); + return WriteFromBuffer(writer, buffer); + } + + /// + /// Write a UInt128 to the underlying stream + /// + /// Writes in little-endian format + public static bool WriteLittleEndian(this BinaryWriter writer, UInt128 value) + { + byte[] buffer = value.GetBytesLittleEndian(); + return WriteFromBuffer(writer, buffer); + } +#endif + + /// + /// Writes in big-endian format + public static bool WriteBigEndian(this BinaryWriter writer, decimal value) + { + byte[] buffer = value.GetBytesBigEndian(); + return WriteFromBuffer(writer, buffer); + } + + /// + /// Writes in little-endian format + public static bool WriteLittleEndian(this BinaryWriter writer, decimal value) + { + byte[] buffer = value.GetBytesLittleEndian(); + return WriteFromBuffer(writer, buffer); + } + + /// + /// Write an array of bytes to the underlying stream + /// + private static bool WriteFromBuffer(BinaryWriter writer, byte[] value) + { + // If the stream is not writable + if (!writer.BaseStream.CanWrite) + return false; + + // Handle the 0-byte case + if (value.Length == 0) + return true; + + // Handle the general case, forcing a write of the correct length + writer.Write(value, 0, value.Length); + return true; + } + } +} diff --git a/SabreTools.Numerics.Extensions/ByteArrayReaderExtensions.cs b/SabreTools.Numerics.Extensions/ByteArrayReaderExtensions.cs new file mode 100644 index 0000000..b29ba8b --- /dev/null +++ b/SabreTools.Numerics.Extensions/ByteArrayReaderExtensions.cs @@ -0,0 +1,2658 @@ +using System; + +namespace SabreTools.Numerics.Extensions +{ + /// + /// Extensions for byte arrays + /// + public static class ByteArrayReaderExtensions + { + #region Exact Read + + /// + /// Read a UInt8 and increment the pointer to an array + /// + public static byte ReadByte(this byte[] content, ref int offset) + { + byte[] buffer = ReadExactlyToBuffer(content, ref offset, 1); + return buffer[0]; + } + + /// + /// Read a UInt8 and increment the pointer to an array + /// + public static byte ReadByteValue(this byte[] content, ref int offset) + => content.ReadByte(ref offset); + + /// + /// Read a UInt8 and increment the pointer to an array + /// + /// Reads in both-endian format + public static BothUInt8 ReadByteBothEndian(this byte[] content, ref int offset) + { + byte le = content.ReadByte(ref offset); + byte be = content.ReadByte(ref offset); + return new BothUInt8(le, be); + } + + /// + /// Read a UInt8[] and increment the pointer to an array + /// + public static byte[] ReadBytes(this byte[] content, ref int offset, int count) + => ReadExactlyToBuffer(content, ref offset, count); + + /// + /// Read an Int8 and increment the pointer to an array + /// + public static sbyte ReadSByte(this byte[] content, ref int offset) + { + byte[] buffer = ReadExactlyToBuffer(content, ref offset, 1); + return (sbyte)buffer[0]; + } + + /// + /// Read a Int8 and increment the pointer to an array + /// + /// Reads in both-endian format + public static BothInt8 ReadSByteBothEndian(this byte[] content, ref int offset) + { + sbyte le = content.ReadSByte(ref offset); + sbyte be = content.ReadSByte(ref offset); + return new BothInt8(le, be); + } + + /// + /// Read a Char and increment the pointer to an array + /// + public static char ReadChar(this byte[] content, ref int offset) + { + byte[] buffer = ReadExactlyToBuffer(content, ref offset, 1); + return (char)buffer[0]; + } + + /// + /// Read an Int16 and increment the pointer to an array + /// + /// Reads in machine native format + public static short ReadInt16(this byte[] content, ref int offset) + { + if (BitConverter.IsLittleEndian) + return content.ReadInt16LittleEndian(ref offset); + else + return content.ReadInt16BigEndian(ref offset); + } + + /// + /// Read an Int16 and increment the pointer to an array + /// + /// Reads in big-endian format + public static short ReadInt16BigEndian(this byte[] content, ref int offset) + { + byte[] buffer = ReadExactlyToBuffer(content, ref offset, 2); + return buffer.ToInt16BigEndian(); + } + + /// + /// Read an Int16 and increment the pointer to an array + /// + /// Reads in little-endian format + public static short ReadInt16LittleEndian(this byte[] content, ref int offset) + { + byte[] buffer = ReadExactlyToBuffer(content, ref offset, 2); + return buffer.ToInt16LittleEndian(); + } + + /// + /// Read a Int16 and increment the pointer to an array + /// + /// Reads in both-endian format + public static BothInt16 ReadInt16BothEndian(this byte[] content, ref int offset) + { + short le = content.ReadInt16LittleEndian(ref offset); + short be = content.ReadInt16BigEndian(ref offset); + return new BothInt16(le, be); + } + + /// + /// Read a UInt16 and increment the pointer to an array + /// + /// Reads in machine native format + public static ushort ReadUInt16(this byte[] content, ref int offset) + { + if (BitConverter.IsLittleEndian) + return content.ReadUInt16LittleEndian(ref offset); + else + return content.ReadUInt16BigEndian(ref offset); + } + + /// + /// Read a UInt16 and increment the pointer to an array + /// + /// Reads in big-endian format + public static ushort ReadUInt16BigEndian(this byte[] content, ref int offset) + { + byte[] buffer = ReadExactlyToBuffer(content, ref offset, 2); + return buffer.ToUInt16BigEndian(); + } + + /// + /// Read a UInt16 and increment the pointer to an array + /// + /// Reads in little-endian format + public static ushort ReadUInt16LittleEndian(this byte[] content, ref int offset) + { + byte[] buffer = ReadExactlyToBuffer(content, ref offset, 2); + return buffer.ToUInt16LittleEndian(); + } + + /// + /// Read a UInt16 and increment the pointer to an array + /// + /// Reads in both-endian format + public static BothUInt16 ReadUInt16BothEndian(this byte[] content, ref int offset) + { + ushort le = content.ReadUInt16LittleEndian(ref offset); + ushort be = content.ReadUInt16BigEndian(ref offset); + return new BothUInt16(le, be); + } + + /// + /// Read a WORD (2-byte) and increment the pointer to an array + /// + /// Reads in machine native format + public static ushort ReadWORD(this byte[] content, ref int offset) + => content.ReadUInt16(ref offset); + + /// + /// Read a WORD (2-byte) and increment the pointer to an array + /// + /// Reads in big-endian format + public static ushort ReadWORDBigEndian(this byte[] content, ref int offset) + => content.ReadUInt16BigEndian(ref offset); + + /// + /// Read a WORD (2-byte) and increment the pointer to an array + /// + /// Reads in little-endian format + public static ushort ReadWORDLittleEndian(this byte[] content, ref int offset) + => content.ReadUInt16LittleEndian(ref offset); + + /// + /// Read a WORD (2-byte) and increment the pointer to an array + /// + /// Reads in both-endian format + public static BothUInt16 ReadWORDBothEndian(this byte[] content, ref int offset) + => content.ReadUInt16BothEndian(ref offset); + +#if NET5_0_OR_GREATER + /// + /// Read a Half and increment the pointer to an array + /// + /// Reads in machine native format + public static Half ReadHalf(this byte[] content, ref int offset) + { + if (BitConverter.IsLittleEndian) + return content.ReadHalfLittleEndian(ref offset); + else + return content.ReadHalfBigEndian(ref offset); + } + + /// + /// Read a Half and increment the pointer to an array + /// + /// Reads in big-endian format + public static Half ReadHalfBigEndian(this byte[] content, ref int offset) + { + byte[] buffer = ReadExactlyToBuffer(content, ref offset, 2); + return buffer.ToHalfBigEndian(); + } + + /// + /// Read a Half and increment the pointer to an array + /// + /// Reads in little-endian format + public static Half ReadHalfLittleEndian(this byte[] content, ref int offset) + { + byte[] buffer = ReadExactlyToBuffer(content, ref offset, 2); + return buffer.ToHalfLittleEndian(); + } +#endif + + /// + /// Read an Int24 and increment the pointer to an array + /// + /// Reads in machine native format + public static Int24 ReadInt24(this byte[] content, ref int offset) + { + if (BitConverter.IsLittleEndian) + return content.ReadInt24LittleEndian(ref offset); + else + return content.ReadInt24BigEndian(ref offset); + } + + /// + /// Read an Int24 and increment the pointer to an array + /// + /// Reads in big-endian format + public static Int24 ReadInt24BigEndian(this byte[] content, ref int offset) + { + byte[] buffer = ReadExactlyToBuffer(content, ref offset, 3); + return buffer.ToInt24BigEndian(); + } + + /// + /// Read an Int24 and increment the pointer to an array + /// + /// Reads in little-endian format + public static Int24 ReadInt24LittleEndian(this byte[] content, ref int offset) + { + byte[] buffer = ReadExactlyToBuffer(content, ref offset, 3); + return buffer.ToInt24LittleEndian(); + } + + /// + /// Read a UInt24 and increment the pointer to an array + /// + /// Reads in machine native format + public static UInt24 ReadUInt24(this byte[] content, ref int offset) + { + if (BitConverter.IsLittleEndian) + return content.ReadUInt24LittleEndian(ref offset); + else + return content.ReadUInt24BigEndian(ref offset); + } + + /// + /// Read a UInt24 and increment the pointer to an array + /// + /// Reads in big-endian format + public static UInt24 ReadUInt24BigEndian(this byte[] content, ref int offset) + { + byte[] buffer = ReadExactlyToBuffer(content, ref offset, 3); + return buffer.ToUInt24BigEndian(); + } + + /// + /// Read a UInt24 and increment the pointer to an array + /// + /// Reads in little-endian format + public static UInt24 ReadUInt24LittleEndian(this byte[] content, ref int offset) + { + byte[] buffer = ReadExactlyToBuffer(content, ref offset, 3); + return buffer.ToUInt24LittleEndian(); + } + + /// + /// Read an Int32 and increment the pointer to an array + /// + /// Reads in machine native format + public static int ReadInt32(this byte[] content, ref int offset) + { + if (BitConverter.IsLittleEndian) + return content.ReadInt32LittleEndian(ref offset); + else + return content.ReadInt32BigEndian(ref offset); + } + + /// + /// Read an Int32 and increment the pointer to an array + /// + /// Reads in big-endian format + public static int ReadInt32BigEndian(this byte[] content, ref int offset) + { + byte[] buffer = ReadExactlyToBuffer(content, ref offset, 4); + return buffer.ToInt32BigEndian(); + } + + /// + /// Read an Int32 and increment the pointer to an array + /// + /// Reads in little-endian format + public static int ReadInt32LittleEndian(this byte[] content, ref int offset) + { + byte[] buffer = ReadExactlyToBuffer(content, ref offset, 4); + return buffer.ToInt32LittleEndian(); + } + + /// + /// Read a Int32 and increment the pointer to an array + /// + /// Reads in both-endian format + public static BothInt32 ReadInt32BothEndian(this byte[] content, ref int offset) + { + int le = content.ReadInt32LittleEndian(ref offset); + int be = content.ReadInt32BigEndian(ref offset); + return new BothInt32(le, be); + } + + /// + /// Read a UInt32 and increment the pointer to an array + /// + /// Reads in machine native format + public static uint ReadUInt32(this byte[] content, ref int offset) + { + if (BitConverter.IsLittleEndian) + return content.ReadUInt32LittleEndian(ref offset); + else + return content.ReadUInt32BigEndian(ref offset); + } + + /// + /// Read a UInt32 and increment the pointer to an array + /// + /// Reads in big-endian format + public static uint ReadUInt32BigEndian(this byte[] content, ref int offset) + { + byte[] buffer = ReadExactlyToBuffer(content, ref offset, 4); + return buffer.ToUInt32BigEndian(); + } + + /// + /// Read a UInt32 and increment the pointer to an array + /// + /// Reads in little-endian format + public static uint ReadUInt32LittleEndian(this byte[] content, ref int offset) + { + byte[] buffer = ReadExactlyToBuffer(content, ref offset, 4); + return buffer.ToUInt32LittleEndian(); + } + + /// + /// Read a UInt32 and increment the pointer to an array + /// + /// Reads in both-endian format + public static BothUInt32 ReadUInt32BothEndian(this byte[] content, ref int offset) + { + uint le = content.ReadUInt32LittleEndian(ref offset); + uint be = content.ReadUInt32BigEndian(ref offset); + return new BothUInt32(le, be); + } + + /// + /// Read a DWORD (4-byte) and increment the pointer to an array + /// + /// Reads in machine native format + public static uint ReadDWORD(this byte[] content, ref int offset) + => content.ReadUInt32(ref offset); + + /// + /// Read a DWORD (4-byte) and increment the pointer to an array + /// + /// Reads in big-endian format + public static uint ReadDWORDBigEndian(this byte[] content, ref int offset) + => content.ReadUInt32BigEndian(ref offset); + + /// + /// Read a DWORD (4-byte) and increment the pointer to an array + /// + /// Reads in little-endian format + public static uint ReadDWORDLittleEndian(this byte[] content, ref int offset) + => content.ReadUInt32LittleEndian(ref offset); + + /// + /// Read a DWORD (4-byte) and increment the pointer to an array + /// + /// Reads in both-endian format + public static BothUInt32 ReadDWORDBothEndian(this byte[] content, ref int offset) + => content.ReadUInt32BothEndian(ref offset); + + /// + /// Read a Single and increment the pointer to an array + /// + /// Reads in machine native format + public static float ReadSingle(this byte[] content, ref int offset) + { + if (BitConverter.IsLittleEndian) + return content.ReadSingleLittleEndian(ref offset); + else + return content.ReadSingleBigEndian(ref offset); + } + + /// + /// Read a Single and increment the pointer to an array + /// + /// Reads in big-endian format + public static float ReadSingleBigEndian(this byte[] content, ref int offset) + { + byte[] buffer = ReadExactlyToBuffer(content, ref offset, 4); + return buffer.ToSingleBigEndian(); + } + + /// + /// Read a Single and increment the pointer to an array + /// + /// Reads in little-endian format + public static float ReadSingleLittleEndian(this byte[] content, ref int offset) + { + byte[] buffer = ReadExactlyToBuffer(content, ref offset, 4); + return buffer.ToSingleLittleEndian(); + } + + /// + /// Read an Int48 and increment the pointer to an array + /// + /// Reads in machine native format + public static Int48 ReadInt48(this byte[] content, ref int offset) + { + if (BitConverter.IsLittleEndian) + return content.ReadInt48LittleEndian(ref offset); + else + return content.ReadInt48BigEndian(ref offset); + } + + /// + /// Read an Int48 and increment the pointer to an array + /// + /// Reads in big-endian format + public static Int48 ReadInt48BigEndian(this byte[] content, ref int offset) + { + byte[] buffer = ReadExactlyToBuffer(content, ref offset, 6); + return buffer.ToInt48BigEndian(); + } + + /// + /// Read an Int48 and increment the pointer to an array + /// + /// Reads in little-endian format + public static Int48 ReadInt48LittleEndian(this byte[] content, ref int offset) + { + byte[] buffer = ReadExactlyToBuffer(content, ref offset, 6); + return buffer.ToInt48LittleEndian(); + } + + /// + /// Read a UInt48 and increment the pointer to an array + /// + /// Reads in machine native format + public static UInt48 ReadUInt48(this byte[] content, ref int offset) + { + if (BitConverter.IsLittleEndian) + return content.ReadUInt48LittleEndian(ref offset); + else + return content.ReadUInt48BigEndian(ref offset); + } + + /// + /// Read an UInt48 and increment the pointer to an array + /// + /// Reads in big-endian format + public static UInt48 ReadUInt48BigEndian(this byte[] content, ref int offset) + { + byte[] buffer = ReadExactlyToBuffer(content, ref offset, 6); + return buffer.ToUInt48BigEndian(); + } + + /// + /// Read an UInt48 and increment the pointer to an array + /// + /// Reads in little-endian format + public static UInt48 ReadUInt48LittleEndian(this byte[] content, ref int offset) + { + byte[] buffer = ReadExactlyToBuffer(content, ref offset, 6); + return buffer.ToUInt48LittleEndian(); + } + + /// + /// Read an Int64 and increment the pointer to an array + /// + /// Reads in machine native format + public static long ReadInt64(this byte[] content, ref int offset) + { + if (BitConverter.IsLittleEndian) + return content.ReadInt64LittleEndian(ref offset); + else + return content.ReadInt64BigEndian(ref offset); + } + + /// + /// Read an Int64 and increment the pointer to an array + /// + /// Reads in big-endian format + public static long ReadInt64BigEndian(this byte[] content, ref int offset) + { + byte[] buffer = ReadExactlyToBuffer(content, ref offset, 8); + return buffer.ToInt64BigEndian(); + } + + /// + /// Read an Int64 and increment the pointer to an array + /// + /// Reads in big-endian format + public static long ReadInt64LittleEndian(this byte[] content, ref int offset) + { + byte[] buffer = ReadExactlyToBuffer(content, ref offset, 8); + return buffer.ToInt64LittleEndian(); + } + + /// + /// Read a Int64 and increment the pointer to an array + /// + /// Reads in both-endian format + public static BothInt64 ReadInt64BothEndian(this byte[] content, ref int offset) + { + long le = content.ReadInt64LittleEndian(ref offset); + long be = content.ReadInt64BigEndian(ref offset); + return new BothInt64(le, be); + } + + /// + /// Read a UInt64 and increment the pointer to an array + /// + /// Reads in machine native format + public static ulong ReadUInt64(this byte[] content, ref int offset) + { + if (BitConverter.IsLittleEndian) + return content.ReadUInt64LittleEndian(ref offset); + else + return content.ReadUInt64BigEndian(ref offset); + } + + /// + /// Read a UInt64 and increment the pointer to an array + /// + /// Reads in big-endian format + public static ulong ReadUInt64BigEndian(this byte[] content, ref int offset) + { + byte[] buffer = ReadExactlyToBuffer(content, ref offset, 8); + return buffer.ToUInt64BigEndian(); + } + + /// + /// Read a UInt64 and increment the pointer to an array + /// + /// Reads in little-endian format + public static ulong ReadUInt64LittleEndian(this byte[] content, ref int offset) + { + byte[] buffer = ReadExactlyToBuffer(content, ref offset, 8); + return buffer.ToUInt64LittleEndian(); + } + + /// + /// Read a UInt64 and increment the pointer to an array + /// + /// Reads in both-endian format + public static BothUInt64 ReadUInt64BothEndian(this byte[] content, ref int offset) + { + ulong le = content.ReadUInt64LittleEndian(ref offset); + ulong be = content.ReadUInt64BigEndian(ref offset); + return new BothUInt64(le, be); + } + + /// + /// Read a QWORD (8-byte) and increment the pointer to an array + /// + /// Reads in machine native format + public static ulong ReadQWORD(this byte[] content, ref int offset) + => content.ReadUInt64(ref offset); + + /// + /// Read a QWORD (8-byte) and increment the pointer to an array + /// + /// Reads in big-endian format + public static ulong ReadQWORDBigEndian(this byte[] content, ref int offset) + => content.ReadUInt64BigEndian(ref offset); + + /// + /// Read a QWORD (8-byte) and increment the pointer to an array + /// + /// Reads in little-endian format + public static ulong ReadQWORDLittleEndian(this byte[] content, ref int offset) + => content.ReadUInt64LittleEndian(ref offset); + + /// + /// Read a QWORD (8-byte) and increment the pointer to an array + /// + /// Reads in both-endian format + public static BothUInt64 ReadQWORDBothEndian(this byte[] content, ref int offset) + => content.ReadUInt64BothEndian(ref offset); + + /// + /// Read a Double and increment the pointer to an array + /// + /// Reads in machine native format + public static double ReadDouble(this byte[] content, ref int offset) + { + if (BitConverter.IsLittleEndian) + return content.ReadDoubleLittleEndian(ref offset); + else + return content.ReadDoubleBigEndian(ref offset); + } + + /// + /// Read a Double and increment the pointer to an array + /// + /// Reads in big-endian format + public static double ReadDoubleBigEndian(this byte[] content, ref int offset) + { + byte[] buffer = ReadExactlyToBuffer(content, ref offset, 8); + return buffer.ToDoubleBigEndian(); + } + + /// + /// Read a Double and increment the pointer to an array + /// + /// Reads in little-endian format + public static double ReadDoubleLittleEndian(this byte[] content, ref int offset) + { + byte[] buffer = ReadExactlyToBuffer(content, ref offset, 8); + return buffer.ToDoubleLittleEndian(); + } + + /// + /// Read a Guid and increment the pointer to an array + /// + /// Reads in machine native format + public static Guid ReadGuid(this byte[] content, ref int offset) + { + if (BitConverter.IsLittleEndian) + return content.ReadGuidLittleEndian(ref offset); + else + return content.ReadGuidBigEndian(ref offset); + } + + /// + /// Read a Guid and increment the pointer to an array + /// + /// Reads in big-endian format + public static Guid ReadGuidBigEndian(this byte[] content, ref int offset) + { + byte[] buffer = ReadExactlyToBuffer(content, ref offset, 16); + return buffer.ToGuidBigEndian(); + } + + /// + /// Read a Guid and increment the pointer to an array + /// + /// Reads in little-endian format + public static Guid ReadGuidLittleEndian(this byte[] content, ref int offset) + { + byte[] buffer = ReadExactlyToBuffer(content, ref offset, 16); + return buffer.ToGuidLittleEndian(); + } + +#if NET7_0_OR_GREATER + /// + /// Read an Int128 and increment the pointer to an array + /// + /// Reads in machine native format + public static Int128 ReadInt128(this byte[] content, ref int offset) + { + if (BitConverter.IsLittleEndian) + return content.ReadInt128LittleEndian(ref offset); + else + return content.ReadInt128BigEndian(ref offset); + } + + /// + /// Read an Int128 and increment the pointer to an array + /// + /// Reads in big-endian format + public static Int128 ReadInt128BigEndian(this byte[] content, ref int offset) + { + byte[] buffer = ReadExactlyToBuffer(content, ref offset, 16); + return buffer.ToInt128BigEndian(); + } + + /// + /// Read an Int128 and increment the pointer to an array + /// + /// Reads in little-endian format + public static Int128 ReadInt128LittleEndian(this byte[] content, ref int offset) + { + byte[] buffer = ReadExactlyToBuffer(content, ref offset, 16); + return buffer.ToInt128LittleEndian(); + } + + /// + /// Read a UInt128 and increment the pointer to an array + /// + /// Reads in machine native format + public static UInt128 ReadUInt128(this byte[] content, ref int offset) + { + if (BitConverter.IsLittleEndian) + return content.ReadUInt128LittleEndian(ref offset); + else + return content.ReadUInt128BigEndian(ref offset); + } + + /// + /// Read a UInt128 and increment the pointer to an array + /// + /// Reads in big-endian format + public static UInt128 ReadUInt128BigEndian(this byte[] content, ref int offset) + { + byte[] buffer = ReadExactlyToBuffer(content, ref offset, 16); + return buffer.ToUInt128BigEndian(); + } + + /// + /// Read a UInt128 and increment the pointer to an array + /// + /// Reads in little-endian format + public static UInt128 ReadUInt128LittleEndian(this byte[] content, ref int offset) + { + byte[] buffer = ReadExactlyToBuffer(content, ref offset, 16); + return buffer.ToUInt128LittleEndian(); + } +#endif + + /// + /// Read a Decimal and increment the pointer to an array + /// + /// Reads in machine native format + public static decimal ReadDecimal(this byte[] content, ref int offset) + { + if (BitConverter.IsLittleEndian) + return content.ReadDecimalLittleEndian(ref offset); + else + return content.ReadDecimalBigEndian(ref offset); + } + + /// + /// Read a Decimal and increment the pointer to an array + /// + /// Reads in big-endian format + public static decimal ReadDecimalBigEndian(this byte[] content, ref int offset) + { + byte[] buffer = ReadExactlyToBuffer(content, ref offset, 16); + return buffer.ToDecimalBigEndian(); + } + + /// + /// Read a Decimal and increment the pointer to an array + /// + /// Reads in little-endian format + public static decimal ReadDecimalLittleEndian(this byte[] content, ref int offset) + { + byte[] buffer = ReadExactlyToBuffer(content, ref offset, 16); + return buffer.ToDecimalLittleEndian(); + } + + /// + /// Read a number of bytes from the byte array to a buffer + /// + /// + /// Thrown if or + /// is an invalid value. + /// + /// + /// Thrown if the requested and + /// is greater than + /// length. + /// + private static byte[] ReadExactlyToBuffer(byte[] content, ref int offset, int length) + { + // If we have an invalid offset + if (offset < 0 || offset >= content.Length) + throw new ArgumentOutOfRangeException($"{nameof(offset)} must be between 0 and {content.Length}, {offset} provided"); + + // If we have an invalid length + if (length < 0) + throw new ArgumentOutOfRangeException($"{nameof(length)} must be 0 or a positive value, {length} requested"); + + // Handle the 0-byte case + if (length == 0) + return []; + + // If there are not enough bytes + if (offset + length > content.Length) + throw new System.IO.EndOfStreamException($"Requested to read {length} bytes from {nameof(content)}, {content.Length - offset} returned"); + + // Handle the general case, forcing a read of the correct length + byte[] buffer = new byte[length]; + Array.Copy(content, offset, buffer, 0, length); + offset += length; + + return buffer; + } + + #endregion + + #region Peek Read + + /// + /// Peek a UInt8 without incrementing the pointer to an array + /// + public static byte PeekByte(this byte[] content, ref int offset) + { + byte value = content.ReadByte(ref offset); + offset -= 1; + return value; + } + + /// + /// Peek a UInt8 without incrementing the pointer to an array + /// + public static byte PeekByteValue(this byte[] content, ref int offset) + => content.PeekByte(ref offset); + + /// + /// Peek a UInt8 without incrementing the pointer to an array + /// + /// Reads in both-endian format + public static BothUInt8 PeekByteBothEndian(this byte[] content, ref int offset) + { + BothUInt8 value = content.ReadByteBothEndian(ref offset); + offset -= 2; + return value; + } + + /// + /// Peek a UInt8[] without incrementing the pointer to an array + /// + public static byte[] PeekBytes(this byte[] content, ref int offset, int count) + { + byte[] value = content.ReadBytes(ref offset, count); + offset -= count; + return value; + } + + /// + /// Peek an Int8 without incrementing the pointer to an array + /// + public static sbyte PeekSByte(this byte[] content, ref int offset) + { + sbyte value = content.ReadSByte(ref offset); + offset -= 1; + return value; + } + + /// + /// Peek a Int8 without incrementing the pointer to an array + /// + /// Reads in both-endian format + public static BothInt8 PeekSByteBothEndian(this byte[] content, ref int offset) + { + BothInt8 value = content.ReadSByteBothEndian(ref offset); + offset -= 2; + return value; + } + + /// + /// Peek a Char without incrementing the pointer to an array + /// + public static char PeekChar(this byte[] content, ref int offset) + { + char value = content.ReadChar(ref offset); + offset -= 1; + return value; + } + + /// + /// Peek an Int16 without incrementing the pointer to an array + /// + /// Reads in machine native format + public static short PeekInt16(this byte[] content, ref int offset) + { + if (BitConverter.IsLittleEndian) + return content.PeekInt16LittleEndian(ref offset); + else + return content.PeekInt16BigEndian(ref offset); + } + + /// + /// Peek an Int16 without incrementing the pointer to an array + /// + /// Reads in big-endian format + public static short PeekInt16BigEndian(this byte[] content, ref int offset) + { + short value = content.ReadInt16BigEndian(ref offset); + offset -= 2; + return value; + } + + /// + /// Peek an Int16 without incrementing the pointer to an array + /// + /// Reads in little-endian format + public static short PeekInt16LittleEndian(this byte[] content, ref int offset) + { + short value = content.ReadInt16LittleEndian(ref offset); + offset -= 2; + return value; + } + + /// + /// Peek a Int16 without incrementing the pointer to an array + /// + /// Reads in both-endian format + public static BothInt16 PeekInt16BothEndian(this byte[] content, ref int offset) + { + BothInt16 value = content.ReadInt16BothEndian(ref offset); + offset -= 4; + return value; + } + + /// + /// Peek a UInt16 without incrementing the pointer to an array + /// + /// Reads in machine native format + public static ushort PeekUInt16(this byte[] content, ref int offset) + { + if (BitConverter.IsLittleEndian) + return content.PeekUInt16LittleEndian(ref offset); + else + return content.PeekUInt16BigEndian(ref offset); + } + + /// + /// Peek a UInt16 without incrementing the pointer to an array + /// + /// Reads in big-endian format + public static ushort PeekUInt16BigEndian(this byte[] content, ref int offset) + { + ushort value = content.ReadUInt16BigEndian(ref offset); + offset -= 2; + return value; + } + + /// + /// Peek a UInt16 without incrementing the pointer to an array + /// + /// Reads in little-endian format + public static ushort PeekUInt16LittleEndian(this byte[] content, ref int offset) + { + ushort value = content.ReadUInt16LittleEndian(ref offset); + offset -= 2; + return value; + } + + /// + /// Peek a UInt16 without incrementing the pointer to an array + /// + /// Reads in both-endian format + public static BothUInt16 PeekUInt16BothEndian(this byte[] content, ref int offset) + { + BothUInt16 value = content.ReadUInt16BothEndian(ref offset); + offset -= 4; + return value; + } + + /// + /// Peek a WORD (2-byte) without incrementing the pointer to an array + /// + /// Reads in machine native format + public static ushort PeekWORD(this byte[] content, ref int offset) + => content.PeekUInt16(ref offset); + + /// + /// Peek a WORD (2-byte) without incrementing the pointer to an array + /// + /// Reads in big-endian format + public static ushort PeekWORDBigEndian(this byte[] content, ref int offset) + => content.PeekUInt16BigEndian(ref offset); + + /// + /// Peek a WORD (2-byte) without incrementing the pointer to an array + /// + /// Reads in little-endian format + public static ushort PeekWORDLittleEndian(this byte[] content, ref int offset) + => content.PeekUInt16LittleEndian(ref offset); + + /// + /// Peek a WORD (2-byte) without incrementing the pointer to an array + /// + /// Reads in both-endian format + public static BothUInt16 PeekWORDBothEndian(this byte[] content, ref int offset) + => content.PeekUInt16BothEndian(ref offset); + +#if NET5_0_OR_GREATER + /// + /// Peek a Half without incrementing the pointer to an array + /// + /// Reads in machine native format + public static Half PeekHalf(this byte[] content, ref int offset) + { + if (BitConverter.IsLittleEndian) + return content.PeekHalfLittleEndian(ref offset); + else + return content.PeekHalfBigEndian(ref offset); + } + + /// + /// Peek a Half without incrementing the pointer to an array + /// + /// Reads in big-endian format + public static Half PeekHalfBigEndian(this byte[] content, ref int offset) + { + Half value = content.ReadHalfBigEndian(ref offset); + offset -= 2; + return value; + } + + /// + /// Peek a Half without incrementing the pointer to an array + /// + /// Reads in little-endian format + public static Half PeekHalfLittleEndian(this byte[] content, ref int offset) + { + Half value = content.ReadHalfLittleEndian(ref offset); + offset -= 2; + return value; + } +#endif + + /// + /// Peek an Int24 without incrementing the pointer to an array + /// + /// Reads in machine native format + public static Int24 PeekInt24(this byte[] content, ref int offset) + { + if (BitConverter.IsLittleEndian) + return content.PeekInt24LittleEndian(ref offset); + else + return content.PeekInt24BigEndian(ref offset); + } + + /// + /// Peek an Int24 without incrementing the pointer to an array + /// + /// Reads in big-endian format + public static Int24 PeekInt24BigEndian(this byte[] content, ref int offset) + { + Int24 value = content.ReadInt24BigEndian(ref offset); + offset -= 3; + return value; + } + + /// + /// Peek an Int24 without incrementing the pointer to an array + /// + /// Reads in little-endian format + public static Int24 PeekInt24LittleEndian(this byte[] content, ref int offset) + { + Int24 value = content.ReadInt24LittleEndian(ref offset); + offset -= 3; + return value; + } + + /// + /// Peek a UInt24 without incrementing the pointer to an array + /// + /// Reads in machine native format + public static UInt24 PeekUInt24(this byte[] content, ref int offset) + { + if (BitConverter.IsLittleEndian) + return content.PeekUInt24LittleEndian(ref offset); + else + return content.PeekUInt24BigEndian(ref offset); + } + + /// + /// Peek a UInt24 without incrementing the pointer to an array + /// + /// Reads in big-endian format + public static UInt24 PeekUInt24BigEndian(this byte[] content, ref int offset) + { + UInt24 value = content.ReadUInt24BigEndian(ref offset); + offset -= 3; + return value; + } + + /// + /// Peek a UInt24 without incrementing the pointer to an array + /// + /// Reads in little-endian format + public static UInt24 PeekUInt24LittleEndian(this byte[] content, ref int offset) + { + UInt24 value = content.ReadUInt24LittleEndian(ref offset); + offset -= 3; + return value; + } + + /// + /// Peek an Int32 without incrementing the pointer to an array + /// + /// Reads in machine native format + public static int PeekInt32(this byte[] content, ref int offset) + { + if (BitConverter.IsLittleEndian) + return content.PeekInt32LittleEndian(ref offset); + else + return content.PeekInt32BigEndian(ref offset); + } + + /// + /// Peek an Int32 without incrementing the pointer to an array + /// + /// Reads in big-endian format + public static int PeekInt32BigEndian(this byte[] content, ref int offset) + { + int value = content.ReadInt32BigEndian(ref offset); + offset -= 4; + return value; + } + + /// + /// Peek an Int32 without incrementing the pointer to an array + /// + /// Reads in little-endian format + public static int PeekInt32LittleEndian(this byte[] content, ref int offset) + { + int value = content.ReadInt32LittleEndian(ref offset); + offset -= 4; + return value; + } + + /// + /// Peek a Int32 without incrementing the pointer to an array + /// + /// Reads in both-endian format + public static BothInt32 PeekInt32BothEndian(this byte[] content, ref int offset) + { + BothInt32 value = content.ReadInt32BothEndian(ref offset); + offset -= 8; + return value; + } + + /// + /// Peek a UInt32 without incrementing the pointer to an array + /// + /// Reads in machine native format + public static uint PeekUInt32(this byte[] content, ref int offset) + { + if (BitConverter.IsLittleEndian) + return content.PeekUInt32LittleEndian(ref offset); + else + return content.PeekUInt32BigEndian(ref offset); + } + + /// + /// Peek a UInt32 without incrementing the pointer to an array + /// + /// Reads in big-endian format + public static uint PeekUInt32BigEndian(this byte[] content, ref int offset) + { + uint value = content.ReadUInt32BigEndian(ref offset); + offset -= 4; + return value; + } + + /// + /// Peek a UInt32 without incrementing the pointer to an array + /// + /// Reads in little-endian format + public static uint PeekUInt32LittleEndian(this byte[] content, ref int offset) + { + uint value = content.ReadUInt32LittleEndian(ref offset); + offset -= 4; + return value; + } + + /// + /// Peek a UInt32 without incrementing the pointer to an array + /// + /// Reads in both-endian format + public static BothUInt32 PeekUInt32BothEndian(this byte[] content, ref int offset) + { + BothUInt32 value = content.ReadUInt32BothEndian(ref offset); + offset -= 8; + return value; + } + + /// + /// Peek a DWORD (4-byte) without incrementing the pointer to an array + /// + /// Reads in machine native format + public static uint PeekDWORD(this byte[] content, ref int offset) + => content.PeekUInt32(ref offset); + + /// + /// Peek a DWORD (4-byte) without incrementing the pointer to an array + /// + /// Reads in big-endian format + public static uint PeekDWORDBigEndian(this byte[] content, ref int offset) + => content.PeekUInt32BigEndian(ref offset); + + /// + /// Peek a DWORD (4-byte) without incrementing the pointer to an array + /// + /// Reads in little-endian format + public static uint PeekDWORDLittleEndian(this byte[] content, ref int offset) + => content.PeekUInt32LittleEndian(ref offset); + + /// + /// Peek a DWORD (4-byte) without incrementing the pointer to an array + /// + /// Reads in both-endian format + public static BothUInt32 PeekDWORDBothEndian(this byte[] content, ref int offset) + => content.PeekUInt32BothEndian(ref offset); + + /// + /// Peek a Single without incrementing the pointer to an array + /// + /// Reads in machine native format + public static float PeekSingle(this byte[] content, ref int offset) + { + if (BitConverter.IsLittleEndian) + return content.PeekSingleLittleEndian(ref offset); + else + return content.PeekSingleBigEndian(ref offset); + } + + /// + /// Peek a Single without incrementing the pointer to an array + /// + /// Reads in big-endian format + public static float PeekSingleBigEndian(this byte[] content, ref int offset) + { + float value = content.ReadSingleBigEndian(ref offset); + offset -= 4; + return value; + } + + /// + /// Peek a Single without incrementing the pointer to an array + /// + /// Reads in little-endian format + public static float PeekSingleLittleEndian(this byte[] content, ref int offset) + { + float value = content.ReadSingleLittleEndian(ref offset); + offset -= 4; + return value; + } + + /// + /// Peek an Int48 without incrementing the pointer to an array + /// + /// Reads in machine native format + public static Int48 PeekInt48(this byte[] content, ref int offset) + { + if (BitConverter.IsLittleEndian) + return content.PeekInt48LittleEndian(ref offset); + else + return content.PeekInt48BigEndian(ref offset); + } + + /// + /// Peek an Int48 without incrementing the pointer to an array + /// + /// Reads in big-endian format + public static Int48 PeekInt48BigEndian(this byte[] content, ref int offset) + { + Int48 value = content.ReadInt48BigEndian(ref offset); + offset -= 6; + return value; + } + + /// + /// Peek an Int48 without incrementing the pointer to an array + /// + /// Reads in little-endian format + public static Int48 PeekInt48LittleEndian(this byte[] content, ref int offset) + { + Int48 value = content.ReadInt48LittleEndian(ref offset); + offset -= 6; + return value; + } + + /// + /// Peek a UInt48 without incrementing the pointer to an array + /// + /// Reads in machine native format + public static UInt48 PeekUInt48(this byte[] content, ref int offset) + { + if (BitConverter.IsLittleEndian) + return content.PeekUInt48LittleEndian(ref offset); + else + return content.PeekUInt48BigEndian(ref offset); + } + + /// + /// Peek an UInt48 without incrementing the pointer to an array + /// + /// Reads in big-endian format + public static UInt48 PeekUInt48BigEndian(this byte[] content, ref int offset) + { + UInt48 value = content.ReadUInt48BigEndian(ref offset); + offset -= 6; + return value; + } + + /// + /// Peek an UInt48 without incrementing the pointer to an array + /// + /// Reads in little-endian format + public static UInt48 PeekUInt48LittleEndian(this byte[] content, ref int offset) + { + UInt48 value = content.ReadUInt48LittleEndian(ref offset); + offset -= 6; + return value; + } + + /// + /// Peek an Int64 without incrementing the pointer to an array + /// + /// Reads in machine native format + public static long PeekInt64(this byte[] content, ref int offset) + { + if (BitConverter.IsLittleEndian) + return content.PeekInt64LittleEndian(ref offset); + else + return content.PeekInt64BigEndian(ref offset); + } + + /// + /// Peek an Int64 without incrementing the pointer to an array + /// + /// Reads in big-endian format + public static long PeekInt64BigEndian(this byte[] content, ref int offset) + { + long value = content.ReadInt64BigEndian(ref offset); + offset -= 8; + return value; + } + + /// + /// Peek an Int64 without incrementing the pointer to an array + /// + /// Reads in big-endian format + public static long PeekInt64LittleEndian(this byte[] content, ref int offset) + { + long value = content.ReadInt64LittleEndian(ref offset); + offset -= 8; + return value; + } + + /// + /// Peek a Int64 without incrementing the pointer to an array + /// + /// Reads in both-endian format + public static BothInt64 PeekInt64BothEndian(this byte[] content, ref int offset) + { + BothInt64 value = content.ReadInt64BothEndian(ref offset); + offset -= 16; + return value; + } + + /// + /// Peek a UInt64 without incrementing the pointer to an array + /// + /// Reads in machine native format + public static ulong PeekUInt64(this byte[] content, ref int offset) + { + if (BitConverter.IsLittleEndian) + return content.PeekUInt64LittleEndian(ref offset); + else + return content.PeekUInt64BigEndian(ref offset); + } + + /// + /// Peek a UInt64 without incrementing the pointer to an array + /// + /// Reads in big-endian format + public static ulong PeekUInt64BigEndian(this byte[] content, ref int offset) + { + ulong value = content.ReadUInt64BigEndian(ref offset); + offset -= 8; + return value; + } + + /// + /// Peek a UInt64 without incrementing the pointer to an array + /// + /// Reads in little-endian format + public static ulong PeekUInt64LittleEndian(this byte[] content, ref int offset) + { + ulong value = content.ReadUInt64LittleEndian(ref offset); + offset -= 8; + return value; + } + + /// + /// Peek a UInt64 without incrementing the pointer to an array + /// + /// Reads in both-endian format + public static BothUInt64 PeekUInt64BothEndian(this byte[] content, ref int offset) + { + BothUInt64 value = content.ReadUInt64BothEndian(ref offset); + offset -= 16; + return value; + } + + /// + /// Peek a QWORD (8-byte) without incrementing the pointer to an array + /// + /// Reads in machine native format + public static ulong PeekQWORD(this byte[] content, ref int offset) + => content.PeekUInt64(ref offset); + + /// + /// Peek a QWORD (8-byte) without incrementing the pointer to an array + /// + /// Reads in big-endian format + public static ulong PeekQWORDBigEndian(this byte[] content, ref int offset) + => content.PeekUInt64BigEndian(ref offset); + + /// + /// Peek a QWORD (8-byte) without incrementing the pointer to an array + /// + /// Reads in little-endian format + public static ulong PeekQWORDLittleEndian(this byte[] content, ref int offset) + => content.PeekUInt64LittleEndian(ref offset); + + /// + /// Peek a QWORD (8-byte) without incrementing the pointer to an array + /// + /// Reads in both-endian format + public static BothUInt64 PeekQWORDBothEndian(this byte[] content, ref int offset) + => content.PeekUInt64BothEndian(ref offset); + + /// + /// Peek a Double without incrementing the pointer to an array + /// + /// Reads in machine native format + public static double PeekDouble(this byte[] content, ref int offset) + { + if (BitConverter.IsLittleEndian) + return content.PeekDoubleLittleEndian(ref offset); + else + return content.PeekDoubleBigEndian(ref offset); + } + + /// + /// Peek a Double without incrementing the pointer to an array + /// + /// Reads in big-endian format + public static double PeekDoubleBigEndian(this byte[] content, ref int offset) + { + double value = content.ReadDoubleBigEndian(ref offset); + offset -= 8; + return value; + } + + /// + /// Peek a Double without incrementing the pointer to an array + /// + /// Reads in little-endian format + public static double PeekDoubleLittleEndian(this byte[] content, ref int offset) + { + double value = content.ReadDoubleLittleEndian(ref offset); + offset -= 8; + return value; + } + + /// + /// Peek a Guid without incrementing the pointer to an array + /// + /// Reads in machine native format + public static Guid PeekGuid(this byte[] content, ref int offset) + { + if (BitConverter.IsLittleEndian) + return content.PeekGuidLittleEndian(ref offset); + else + return content.PeekGuidBigEndian(ref offset); + } + + /// + /// Peek a Guid without incrementing the pointer to an array + /// + /// Reads in big-endian format + public static Guid PeekGuidBigEndian(this byte[] content, ref int offset) + { + Guid value = content.ReadGuidBigEndian(ref offset); + offset -= 16; + return value; + } + + /// + /// Peek a Guid without incrementing the pointer to an array + /// + /// Reads in big-endian format + public static Guid PeekGuidLittleEndian(this byte[] content, ref int offset) + { + Guid value = content.ReadGuidLittleEndian(ref offset); + offset -= 16; + return value; + } + +#if NET7_0_OR_GREATER + /// + /// Peek an Int128 without incrementing the pointer to an array + /// + /// Reads in machine native format + public static Int128 PeekInt128(this byte[] content, ref int offset) + { + if (BitConverter.IsLittleEndian) + return content.PeekInt128LittleEndian(ref offset); + else + return content.PeekInt128BigEndian(ref offset); + } + + /// + /// Peek an Int128 without incrementing the pointer to an array + /// + /// Reads in big-endian format + public static Int128 PeekInt128BigEndian(this byte[] content, ref int offset) + { + Int128 value = content.ReadInt128BigEndian(ref offset); + offset -= 16; + return value; + } + + /// + /// Peek an Int128 without incrementing the pointer to an array + /// + /// Reads in little-endian format + public static Int128 PeekInt128LittleEndian(this byte[] content, ref int offset) + { + Int128 value = content.ReadInt128LittleEndian(ref offset); + offset -= 16; + return value; + } + + /// + /// Peek a UInt128 without incrementing the pointer to an array + /// + /// Reads in machine native format + public static UInt128 PeekUInt128(this byte[] content, ref int offset) + { + if (BitConverter.IsLittleEndian) + return content.PeekUInt128LittleEndian(ref offset); + else + return content.PeekUInt128BigEndian(ref offset); + } + + /// + /// Peek a UInt128 without incrementing the pointer to an array + /// + /// Reads in big-endian format + public static UInt128 PeekUInt128BigEndian(this byte[] content, ref int offset) + { + UInt128 value = content.ReadUInt128BigEndian(ref offset); + offset -= 16; + return value; + } + + /// + /// Peek a UInt128 without incrementing the pointer to an array + /// + /// Reads in little-endian format + public static UInt128 PeekUInt128LittleEndian(this byte[] content, ref int offset) + { + UInt128 value = content.ReadUInt128LittleEndian(ref offset); + offset -= 16; + return value; + } +#endif + + /// + /// Peek a Decimal without incrementing the pointer to an array + /// + /// Reads in machine native format + public static decimal PeekDecimal(this byte[] content, ref int offset) + { + if (BitConverter.IsLittleEndian) + return content.PeekDecimalLittleEndian(ref offset); + else + return content.PeekDecimalBigEndian(ref offset); + } + + /// + /// Peek a Decimal without incrementing the pointer to an array + /// + /// Reads in big-endian format + public static decimal PeekDecimalBigEndian(this byte[] content, ref int offset) + { + decimal value = content.ReadDecimalBigEndian(ref offset); + offset -= 16; + return value; + } + + /// + /// Peek a Decimal without incrementing the pointer to an array + /// + /// Reads in little-endian format + public static decimal PeekDecimalLittleEndian(this byte[] content, ref int offset) + { + decimal value = content.ReadDecimalLittleEndian(ref offset); + offset -= 16; + return value; + } + + #endregion + + #region Try Read + + /// + /// Read a UInt8 and increment the pointer to an array + /// + public static bool TryReadByte(this byte[] content, ref int offset, out byte value) + { + if (offset > content.Length - 1) + { + value = default; + return false; + } + + value = content.ReadByte(ref offset); + return true; + } + + /// + /// Read a UInt8 and increment the pointer to an array + /// + public static bool TryReadByteValue(this byte[] content, ref int offset, out byte value) + { + if (offset > content.Length - 1) + { + value = default; + return false; + } + + value = content.ReadByteValue(ref offset); + return true; + } + + /// + /// Read a UInt8 and increment the pointer to an array + /// + /// Reads in both-endian format + public static bool TryReadByteBothEndian(this byte[] content, ref int offset, out BothUInt8 value) + { + if (offset > content.Length - 2) + { + value = default(byte); + return false; + } + + value = content.ReadByteBothEndian(ref offset); + return true; + } + + /// + /// Read a UInt8[] and increment the pointer to an array + /// + public static bool TryReadBytes(this byte[] content, ref int offset, int count, out byte[] value) + { + if (offset > content.Length - count) + { + value = []; + return false; + } + + value = content.ReadBytes(ref offset, count); + return true; + } + + /// + /// Read an Int8 and increment the pointer to an array + /// + public static bool TryReadSByte(this byte[] content, ref int offset, out sbyte value) + { + if (offset > content.Length - 1) + { + value = default; + return false; + } + + value = content.ReadSByte(ref offset); + return true; + } + + /// + /// Read a Int8 and increment the pointer to an array + /// + /// Reads in both-endian format + public static bool TryReadSByteBothEndian(this byte[] content, ref int offset, out BothInt8 value) + { + if (offset > content.Length - 2) + { + value = default(sbyte); + return false; + } + + value = content.ReadSByteBothEndian(ref offset); + return true; + } + + /// + /// Read a Char and increment the pointer to an array + /// + public static bool TryReadChar(this byte[] content, ref int offset, out char value) + { + if (offset > content.Length - 1) + { + value = default; + return false; + } + + value = content.ReadChar(ref offset); + return true; + } + + /// + /// Read an Int16 and increment the pointer to an array + /// + /// Reads in machine native format + public static bool TryReadInt16(this byte[] content, ref int offset, out short value) + { + if (BitConverter.IsLittleEndian) + return content.TryReadInt16LittleEndian(ref offset, out value); + else + return content.TryReadInt16BigEndian(ref offset, out value); + } + + /// + /// Read an Int16 and increment the pointer to an array + /// + /// Reads in big-endian format + public static bool TryReadInt16BigEndian(this byte[] content, ref int offset, out short value) + { + if (offset > content.Length - 2) + { + value = default; + return false; + } + + value = content.ReadInt16BigEndian(ref offset); + return true; + } + + /// + /// Read an Int16 and increment the pointer to an array + /// + /// Reads in little-endian format + public static bool TryReadInt16LittleEndian(this byte[] content, ref int offset, out short value) + { + if (offset > content.Length - 2) + { + value = default; + return false; + } + + value = content.ReadInt16LittleEndian(ref offset); + return true; + } + + /// + /// Read a Int16 and increment the pointer to an array + /// + /// Reads in both-endian format + public static bool TryReadInt16BothEndian(this byte[] content, ref int offset, out BothInt16 value) + { + if (offset > content.Length - 4) + { + value = default(short); + return false; + } + + value = content.ReadInt16BothEndian(ref offset); + return true; + } + + /// + /// Read a UInt16 and increment the pointer to an array + /// + /// Reads in machine native format + public static bool TryReadUInt16(this byte[] content, ref int offset, out ushort value) + { + if (BitConverter.IsLittleEndian) + return content.TryReadUInt16LittleEndian(ref offset, out value); + else + return content.TryReadUInt16BigEndian(ref offset, out value); + } + + /// + /// Read a UInt16 and increment the pointer to an array + /// + /// Reads in big-endian format + public static bool TryReadUInt16BigEndian(this byte[] content, ref int offset, out ushort value) + { + if (offset > content.Length - 2) + { + value = default; + return false; + } + + value = content.ReadUInt16BigEndian(ref offset); + return true; + } + + /// + /// Read a UInt16 and increment the pointer to an array + /// + /// Reads in little-endian format + public static bool TryReadUInt16LittleEndian(this byte[] content, ref int offset, out ushort value) + { + if (offset > content.Length - 2) + { + value = default; + return false; + } + + value = content.ReadUInt16LittleEndian(ref offset); + return true; + } + + /// + /// Read a UInt16 and increment the pointer to an array + /// + /// Reads in both-endian format + public static bool TryReadUInt16BothEndian(this byte[] content, ref int offset, out BothUInt16 value) + { + if (offset > content.Length - 4) + { + value = default(ushort); + return false; + } + + value = content.ReadUInt16BothEndian(ref offset); + return true; + } + + /// + /// Read a WORD (2-byte) and increment the pointer to an array + /// + /// Reads in machine native format + public static bool TryReadWORD(this byte[] content, ref int offset, out ushort value) + => content.TryReadUInt16(ref offset, out value); + + /// + /// Read a WORD (2-byte) and increment the pointer to an array + /// + /// Reads in big-endian format + public static bool TryReadWORDBigEndian(this byte[] content, ref int offset, out ushort value) + => content.TryReadUInt16BigEndian(ref offset, out value); + + /// + /// Read a WORD (2-byte) and increment the pointer to an array + /// + /// Reads in little-endian format + public static bool TryReadWORDLittleEndian(this byte[] content, ref int offset, out ushort value) + => content.TryReadUInt16LittleEndian(ref offset, out value); + + /// + /// Read a WORD (2-byte) and increment the pointer to an array + /// + /// Reads in both-endian format + public static bool TryReadWORDBothEndian(this byte[] content, ref int offset, out BothUInt16 value) + => content.TryReadUInt16BothEndian(ref offset, out value); + +#if NET5_0_OR_GREATER + /// + /// Read a Half and increment the pointer to an array + /// + /// Reads in machine native format + public static bool TryReadHalf(this byte[] content, ref int offset, out Half value) + { + if (BitConverter.IsLittleEndian) + return content.TryReadHalfLittleEndian(ref offset, out value); + else + return content.TryReadHalfBigEndian(ref offset, out value); + } + + /// + /// Read a Half and increment the pointer to an array + /// + /// Reads in big-endian format + public static bool TryReadHalfBigEndian(this byte[] content, ref int offset, out Half value) + { + if (offset > content.Length - 2) + { + value = default; + return false; + } + + value = content.ReadHalfBigEndian(ref offset); + return true; + } + + /// + /// Read a Half and increment the pointer to an array + /// + /// Reads in little-endian format + public static bool TryReadHalfLittleEndian(this byte[] content, ref int offset, out Half value) + { + if (offset > content.Length - 2) + { + value = default; + return false; + } + + value = content.ReadHalfLittleEndian(ref offset); + return true; + } +#endif + + /// + /// Read an Int24 and increment the pointer to an array + /// + /// Reads in machine native format + public static bool TryReadInt24(this byte[] content, ref int offset, out Int24 value) + { + if (BitConverter.IsLittleEndian) + return content.TryReadInt24LittleEndian(ref offset, out value); + else + return content.TryReadInt24BigEndian(ref offset, out value); + } + + /// + /// Read an Int24 and increment the pointer to an array + /// + /// Reads in big-endian format + public static bool TryReadInt24BigEndian(this byte[] content, ref int offset, out Int24 value) + { + if (offset > content.Length - 3) + { + value = new Int24(); + return false; + } + + value = content.ReadInt24BigEndian(ref offset); + return true; + } + + /// + /// Read an Int24 and increment the pointer to an array + /// + /// Reads in little-endian format + public static bool TryReadInt24LittleEndian(this byte[] content, ref int offset, out Int24 value) + { + if (offset > content.Length - 3) + { + value = new Int24(); + return false; + } + + value = content.ReadInt24LittleEndian(ref offset); + return true; + } + + /// + /// Read a UInt24 and increment the pointer to an array + /// + /// Reads in machine native format + public static bool TryReadUInt24(this byte[] content, ref int offset, out UInt24 value) + { + if (BitConverter.IsLittleEndian) + return content.TryReadUInt24LittleEndian(ref offset, out value); + else + return content.TryReadUInt24BigEndian(ref offset, out value); + } + + /// + /// Read a UInt24 and increment the pointer to an array + /// + /// Reads in big-endian format + public static bool TryReadUInt24BigEndian(this byte[] content, ref int offset, out UInt24 value) + { + if (offset > content.Length - 3) + { + value = new UInt24(); + return false; + } + + value = content.ReadUInt24BigEndian(ref offset); + return true; + } + + /// + /// Read a UInt24 and increment the pointer to an array + /// + /// Reads in little-endian format + public static bool TryReadUInt24LittleEndian(this byte[] content, ref int offset, out UInt24 value) + { + if (offset > content.Length - 3) + { + value = new UInt24(); + return false; + } + + value = content.ReadUInt24LittleEndian(ref offset); + return true; + } + + /// + /// Read an Int32 and increment the pointer to an array + /// + /// Reads in machine native format + public static bool TryReadInt32(this byte[] content, ref int offset, out int value) + { + if (BitConverter.IsLittleEndian) + return content.TryReadInt32LittleEndian(ref offset, out value); + else + return content.TryReadInt32BigEndian(ref offset, out value); + } + + /// + /// Read an Int32 and increment the pointer to an array + /// + /// Reads in big-endian format + public static bool TryReadInt32BigEndian(this byte[] content, ref int offset, out int value) + { + if (offset > content.Length - 4) + { + value = default; + return false; + } + + value = content.ReadInt32BigEndian(ref offset); + return true; + } + + /// + /// Read an Int32 and increment the pointer to an array + /// + /// Reads in little-endian format + public static bool TryReadInt32LittleEndian(this byte[] content, ref int offset, out int value) + { + if (offset > content.Length - 4) + { + value = default; + return false; + } + + value = content.ReadInt32LittleEndian(ref offset); + return true; + } + + /// + /// Read a Int32 and increment the pointer to an array + /// + /// Reads in both-endian format + public static bool TryReadInt32BothEndian(this byte[] content, ref int offset, out BothInt32 value) + { + if (offset > content.Length - 8) + { + value = default(int); + return false; + } + + value = content.ReadInt32BothEndian(ref offset); + return true; + } + + /// + /// Read a UInt32 and increment the pointer to an array + /// + /// Reads in machine native format + public static bool TryReadUInt32(this byte[] content, ref int offset, out uint value) + { + if (BitConverter.IsLittleEndian) + return content.TryReadUInt32LittleEndian(ref offset, out value); + else + return content.TryReadUInt32BigEndian(ref offset, out value); + } + + /// + /// Read a UInt32 and increment the pointer to an array + /// + /// Reads in big-endian format + public static bool TryReadUInt32BigEndian(this byte[] content, ref int offset, out uint value) + { + if (offset > content.Length - 4) + { + value = default; + return false; + } + + value = content.ReadUInt32BigEndian(ref offset); + return true; + } + + /// + /// Read a UInt32 and increment the pointer to an array + /// + /// Reads in little-endian format + public static bool TryReadUInt32LittleEndian(this byte[] content, ref int offset, out uint value) + { + if (offset > content.Length - 4) + { + value = default; + return false; + } + + value = content.ReadUInt32LittleEndian(ref offset); + return true; + } + + /// + /// Read a UInt32 and increment the pointer to an array + /// + /// Reads in both-endian format + public static bool TryReadUInt32BothEndian(this byte[] content, ref int offset, out BothUInt32 value) + { + if (offset > content.Length - 8) + { + value = default(uint); + return false; + } + + value = content.ReadUInt32BothEndian(ref offset); + return true; + } + + /// + /// Read a DWORD (4-byte) and increment the pointer to an array + /// + /// Reads in machine native format + public static bool TryReadDWORD(this byte[] content, ref int offset, out uint value) + => content.TryReadUInt32(ref offset, out value); + + /// + /// Read a DWORD (4-byte) and increment the pointer to an array + /// + /// Reads in big-endian format + public static bool TryReadDWORDBigEndian(this byte[] content, ref int offset, out uint value) + => content.TryReadUInt32BigEndian(ref offset, out value); + + /// + /// Read a DWORD (4-byte) and increment the pointer to an array + /// + /// Reads in little-endian format + public static bool TryReadDWORDLittleEndian(this byte[] content, ref int offset, out uint value) + => content.TryReadUInt32LittleEndian(ref offset, out value); + + /// + /// Read a DWORD (4-byte) and increment the pointer to an array + /// + /// Reads in both-endian format + public static bool TryReadDWORDBothEndian(this byte[] content, ref int offset, out BothUInt32 value) + => content.TryReadUInt32BothEndian(ref offset, out value); + + /// + /// Read a Single and increment the pointer to an array + /// + /// Reads in machine native format + public static bool TryReadSingle(this byte[] content, ref int offset, out float value) + { + if (BitConverter.IsLittleEndian) + return content.TryReadSingleLittleEndian(ref offset, out value); + else + return content.TryReadSingleBigEndian(ref offset, out value); + } + + /// + /// Read a Single and increment the pointer to an array + /// + /// Reads in big-endian format + public static bool TryReadSingleBigEndian(this byte[] content, ref int offset, out float value) + { + if (offset > content.Length - 4) + { + value = default; + return false; + } + + value = content.ReadSingleBigEndian(ref offset); + return true; + } + + /// + /// Read a Single and increment the pointer to an array + /// + /// Reads in little-endian format + public static bool TryReadSingleLittleEndian(this byte[] content, ref int offset, out float value) + { + if (offset > content.Length - 4) + { + value = default; + return false; + } + + value = content.ReadSingleLittleEndian(ref offset); + return true; + } + + /// + /// Read an Int48 and increment the pointer to an array + /// + /// Reads in machine native format + public static bool TryReadInt48(this byte[] content, ref int offset, out Int48 value) + { + if (BitConverter.IsLittleEndian) + return content.TryReadInt48LittleEndian(ref offset, out value); + else + return content.TryReadInt48BigEndian(ref offset, out value); + } + + /// + /// Read an Int48 and increment the pointer to an array + /// + /// Reads in big-endian format + public static bool TryReadInt48BigEndian(this byte[] content, ref int offset, out Int48 value) + { + if (offset > content.Length - 6) + { + value = new Int48(); + return false; + } + + value = content.ReadInt48BigEndian(ref offset); + return true; + } + + /// + /// Read an Int48 and increment the pointer to an array + /// + /// Reads in little-endian format + public static bool TryReadInt48LittleEndian(this byte[] content, ref int offset, out Int48 value) + { + if (offset > content.Length - 6) + { + value = new Int48(); + return false; + } + + value = content.ReadInt48LittleEndian(ref offset); + return true; + } + + /// + /// Read a UInt48 and increment the pointer to an array + /// + /// Reads in machine native format + public static bool TryReadUInt48(this byte[] content, ref int offset, out UInt48 value) + { + if (BitConverter.IsLittleEndian) + return content.TryReadUInt48LittleEndian(ref offset, out value); + else + return content.TryReadUInt48BigEndian(ref offset, out value); + } + + /// + /// Read a UInt48 and increment the pointer to an array + /// + /// Reads in big-endian format + public static bool TryReadUInt48BigEndian(this byte[] content, ref int offset, out UInt48 value) + { + if (offset > content.Length - 6) + { + value = new UInt48(); + return false; + } + + value = content.ReadUInt48BigEndian(ref offset); + return true; + } + + /// + /// Read an UInt48 and increment the pointer to an array + /// + /// Reads in little-endian format + public static bool TryReadUInt48LittleEndian(this byte[] content, ref int offset, out UInt48 value) + { + if (offset > content.Length - 6) + { + value = new UInt48(); + return false; + } + + value = content.ReadUInt48LittleEndian(ref offset); + return true; + } + + /// + /// Read an Int64 and increment the pointer to an array + /// + /// Reads in machine native format + public static bool TryReadInt64(this byte[] content, ref int offset, out long value) + { + if (BitConverter.IsLittleEndian) + return content.TryReadInt64LittleEndian(ref offset, out value); + else + return content.TryReadInt64BigEndian(ref offset, out value); + } + + /// + /// Read an Int64 and increment the pointer to an array + /// + /// Reads in big-endian format + public static bool TryReadInt64BigEndian(this byte[] content, ref int offset, out long value) + { + if (offset > content.Length - 8) + { + value = default; + return false; + } + + value = content.ReadInt64BigEndian(ref offset); + return true; + } + + /// + /// Read an Int64 and increment the pointer to an array + /// + /// Reads in little-endian format + public static bool TryReadInt64LittleEndian(this byte[] content, ref int offset, out long value) + { + if (offset > content.Length - 8) + { + value = default; + return false; + } + + value = content.ReadInt64BigEndian(ref offset); + return true; + } + + /// + /// Read a Int64 and increment the pointer to an array + /// + /// Reads in both-endian format + public static bool TryReadInt64BothEndian(this byte[] content, ref int offset, out BothInt64 value) + { + if (offset > content.Length - 16) + { + value = default(long); + return false; + } + + value = content.ReadInt64BothEndian(ref offset); + return true; + } + + /// + /// Read a UInt64 and increment the pointer to an array + /// + /// Reads in machine native format + public static bool TryReadUInt64(this byte[] content, ref int offset, out ulong value) + { + if (BitConverter.IsLittleEndian) + return content.TryReadUInt64LittleEndian(ref offset, out value); + else + return content.TryReadUInt64BigEndian(ref offset, out value); + } + + /// + /// Read a UInt64 and increment the pointer to an array + /// + /// Reads in big-endian format + public static bool TryReadUInt64BigEndian(this byte[] content, ref int offset, out ulong value) + { + if (offset > content.Length - 8) + { + value = default; + return false; + } + + value = content.ReadUInt64BigEndian(ref offset); + return true; + } + + /// + /// Read a UInt64 and increment the pointer to an array + /// + /// Reads in little-endian format + public static bool TryReadUInt64LittleEndian(this byte[] content, ref int offset, out ulong value) + { + if (offset > content.Length - 8) + { + value = default; + return false; + } + + value = content.ReadUInt64LittleEndian(ref offset); + return true; + } + + /// + /// Read a UInt64 and increment the pointer to an array + /// + /// Reads in both-endian format + public static bool TryReadUInt64BothEndian(this byte[] content, ref int offset, out BothUInt64 value) + { + if (offset > content.Length - 16) + { + value = default(ulong); + return false; + } + + value = content.ReadUInt64BothEndian(ref offset); + return true; + } + + /// + /// Read a QWORD (8-byte) and increment the pointer to an array + /// + /// Reads in machine native format + public static bool TryReadQWORD(this byte[] content, ref int offset, out ulong value) + => content.TryReadUInt64(ref offset, out value); + + /// + /// Read a QWORD (8-byte) and increment the pointer to an array + /// + /// Reads in big-endian format + public static bool TryReadQWORDBigEndian(this byte[] content, ref int offset, out ulong value) + => content.TryReadUInt64BigEndian(ref offset, out value); + + /// + /// Read a QWORD (8-byte) and increment the pointer to an array + /// + /// Reads in little-endian format + public static bool TryReadQWORDLittleEndian(this byte[] content, ref int offset, out ulong value) + => content.TryReadUInt64LittleEndian(ref offset, out value); + + /// + /// Read a QWORD (8-byte) and increment the pointer to an array + /// + /// Reads in both-endian format + public static bool TryReadQWORDBothEndian(this byte[] content, ref int offset, out BothUInt64 value) + => content.TryReadUInt64BothEndian(ref offset, out value); + + /// + /// Read a Double and increment the pointer to an array + /// + /// Reads in machine native format + public static bool TryReadDouble(this byte[] content, ref int offset, out double value) + { + if (BitConverter.IsLittleEndian) + return content.TryReadDoubleLittleEndian(ref offset, out value); + else + return content.TryReadDoubleBigEndian(ref offset, out value); + } + + /// + /// Read a Double and increment the pointer to an array + /// + /// Reads in big-endian format + public static bool TryReadDoubleBigEndian(this byte[] content, ref int offset, out double value) + { + if (offset > content.Length - 8) + { + value = default; + return false; + } + + value = content.ReadDoubleBigEndian(ref offset); + return true; + } + + /// + /// Read a Double and increment the pointer to an array + /// + /// Reads in big-endian format + public static bool TryReadDoubleLittleEndian(this byte[] content, ref int offset, out double value) + { + if (offset > content.Length - 8) + { + value = default; + return false; + } + + value = content.ReadDoubleLittleEndian(ref offset); + return true; + } + + /// + /// Read a Guid and increment the pointer to an array + /// + /// Reads in machine native format + public static bool TryReadGuid(this byte[] content, ref int offset, out Guid value) + { + if (BitConverter.IsLittleEndian) + return content.TryReadGuidLittleEndian(ref offset, out value); + else + return content.TryReadGuidBigEndian(ref offset, out value); + } + + /// + /// Read a Guid and increment the pointer to an array + /// + /// Reads in big-endian format + public static bool TryReadGuidBigEndian(this byte[] content, ref int offset, out Guid value) + { + if (offset > content.Length - 16) + { + value = default; + return false; + } + + value = content.ReadGuidBigEndian(ref offset); + return true; + } + + /// + /// Read a Guid and increment the pointer to an array + /// + /// Reads in little-endian format + public static bool TryReadGuidLittleEndian(this byte[] content, ref int offset, out Guid value) + { + if (offset > content.Length - 16) + { + value = default; + return false; + } + + value = content.ReadGuidLittleEndian(ref offset); + return true; + } + +#if NET7_0_OR_GREATER + /// + /// Read an Int128 and increment the pointer to an array + /// + /// Reads in machine native format + public static bool TryReadInt128(this byte[] content, ref int offset, out Int128 value) + { + if (BitConverter.IsLittleEndian) + return content.TryReadInt128LittleEndian(ref offset, out value); + else + return content.TryReadInt128BigEndian(ref offset, out value); + } + + /// + /// Read an Int128 and increment the pointer to an array + /// + /// Reads in big-endian format + public static bool TryReadInt128BigEndian(this byte[] content, ref int offset, out Int128 value) + { + if (offset > content.Length - 16) + { + value = default; + return false; + } + + value = content.ReadInt128BigEndian(ref offset); + return true; + } + + /// + /// Read an Int128 and increment the pointer to an array + /// + /// Reads in little-endian format + public static bool TryReadInt128LittleEndian(this byte[] content, ref int offset, out Int128 value) + { + if (offset > content.Length - 16) + { + value = default; + return false; + } + + value = content.ReadInt128LittleEndian(ref offset); + return true; + } + + /// + /// Read a UInt128 and increment the pointer to an array + /// + /// Reads in machine native format + public static bool TryReadUInt128(this byte[] content, ref int offset, out UInt128 value) + { + if (BitConverter.IsLittleEndian) + return content.TryReadUInt128LittleEndian(ref offset, out value); + else + return content.TryReadUInt128BigEndian(ref offset, out value); + } + + /// + /// Read a UInt128 and increment the pointer to an array + /// + /// Reads in big-endian format + public static bool TryReadUInt128BigEndian(this byte[] content, ref int offset, out UInt128 value) + { + if (offset > content.Length - 16) + { + value = default; + return false; + } + + value = content.ReadUInt128BigEndian(ref offset); + return true; + } + + /// + /// Read a UInt128 and increment the pointer to an array + /// + /// Reads in little-endian format + public static bool TryReadUInt128LittleEndian(this byte[] content, ref int offset, out UInt128 value) + { + if (offset > content.Length - 16) + { + value = default; + return false; + } + + value = content.ReadUInt128LittleEndian(ref offset); + return true; + } +#endif + + /// + /// Read a Decimal and increment the pointer to an array + /// + /// Reads in machine native format + public static bool TryReadDecimal(this byte[] content, ref int offset, out decimal value) + { + if (BitConverter.IsLittleEndian) + return content.TryReadDecimalLittleEndian(ref offset, out value); + else + return content.TryReadDecimalBigEndian(ref offset, out value); + } + + /// + /// Read a Decimal and increment the pointer to an array + /// + /// Reads in big-endian format + public static bool TryReadDecimalBigEndian(this byte[] content, ref int offset, out decimal value) + { + if (offset > content.Length - 16) + { + value = default; + return false; + } + + value = content.ReadDecimalBigEndian(ref offset); + return true; + } + + /// + /// Read a Decimal and increment the pointer to an array + /// + /// Reads in little-endian format + public static bool TryReadDecimalLittleEndian(this byte[] content, ref int offset, out decimal value) + { + if (offset > content.Length - 16) + { + value = default; + return false; + } + + value = content.ReadDecimalLittleEndian(ref offset); + return true; + } + + #endregion + } +} diff --git a/SabreTools.Numerics.Extensions/ByteArrayWriterExtensions.cs b/SabreTools.Numerics.Extensions/ByteArrayWriterExtensions.cs new file mode 100644 index 0000000..3b73beb --- /dev/null +++ b/SabreTools.Numerics.Extensions/ByteArrayWriterExtensions.cs @@ -0,0 +1,717 @@ +using System; +using System.Text; + +namespace SabreTools.Numerics.Extensions +{ + /// + /// Extensions for byte arrays + /// + public static class ByteArrayWriterExtensions + { + /// + /// Write a UInt8 and increment the pointer to an array + /// + public static bool Write(this byte[] content, ref int offset, byte value) + => WriteFromBuffer(content, ref offset, [value]); + + /// + /// Write a UInt8 and increment the pointer to an array + /// + /// Writes in both-endian format + public static bool WriteBothEndian(this byte[] content, ref int offset, BothUInt8 value) + { + bool actual = content.Write(ref offset, value.LittleEndian); + actual &= content.Write(ref offset, value.BigEndian); + return actual; + } + + /// + /// Write a UInt8[] and increment the pointer to an array + /// + public static bool Write(this byte[] content, ref int offset, byte[] value) + => WriteFromBuffer(content, ref offset, value); + + /// + /// Write a UInt8[] and increment the pointer to an array + /// + /// Writes in big-endian format + public static bool WriteBigEndian(this byte[] content, ref int offset, byte[] value) + { + Array.Reverse(value); + return WriteFromBuffer(content, ref offset, value); + } + + /// + /// Write an Int8 and increment the pointer to an array + /// + public static bool Write(this byte[] content, ref int offset, sbyte value) + => WriteFromBuffer(content, ref offset, [(byte)value]); + + /// + /// Write a Int8 and increment the pointer to an array + /// + /// Writes in both-endian format + public static bool WriteBothEndian(this byte[] content, ref int offset, BothInt8 value) + { + bool actual = content.Write(ref offset, value.LittleEndian); + actual &= content.Write(ref offset, value.BigEndian); + return actual; + } + + /// + /// Write a Char and increment the pointer to an array + /// + public static bool Write(this byte[] content, ref int offset, char value) + { + byte[] buffer = BitConverter.GetBytes(value); + return WriteFromBuffer(content, ref offset, buffer); + } + + /// + /// Write a Char with an Encoding and increment the pointer to an array + /// + public static bool Write(this byte[] content, ref int offset, char value, Encoding encoding) + { + byte[] buffer = encoding.GetBytes($"{value}"); + return WriteFromBuffer(content, ref offset, buffer); + } + + /// + /// Write an Int16 and increment the pointer to an array + /// + /// Writes in machine native format + public static bool Write(this byte[] content, ref int offset, short value) + { + if (BitConverter.IsLittleEndian) + return content.WriteLittleEndian(ref offset, value); + else + return content.WriteBigEndian(ref offset, value); + } + + /// + /// Write an Int16 and increment the pointer to an array + /// + /// Writes in big-endian format + public static bool WriteBigEndian(this byte[] content, ref int offset, short value) + { + byte[] buffer = value.GetBytesBigEndian(); + return WriteFromBuffer(content, ref offset, buffer); + } + + /// + /// Write an Int16 and increment the pointer to an array + /// + /// Writes in little-endian format + public static bool WriteLittleEndian(this byte[] content, ref int offset, short value) + { + byte[] buffer = value.GetBytesLittleEndian(); + return WriteFromBuffer(content, ref offset, buffer); + } + + /// + /// Write a Int16 and increment the pointer to an array + /// + /// Writes in both-endian format + public static bool WriteBothEndian(this byte[] content, ref int offset, BothInt16 value) + { + bool actual = content.WriteLittleEndian(ref offset, value.LittleEndian); + actual &= content.WriteBigEndian(ref offset, value.BigEndian); + return actual; + } + + /// + /// Write a UInt16 and increment the pointer to an array + /// + /// Writes in machine native format + public static bool Write(this byte[] content, ref int offset, ushort value) + { + if (BitConverter.IsLittleEndian) + return content.WriteLittleEndian(ref offset, value); + else + return content.WriteBigEndian(ref offset, value); + } + + /// + /// Write a UInt16 and increment the pointer to an array + /// + /// Writes in big-endian format + public static bool WriteBigEndian(this byte[] content, ref int offset, ushort value) + { + byte[] buffer = value.GetBytesBigEndian(); + return WriteFromBuffer(content, ref offset, buffer); + } + + /// + /// Write a UInt16 and increment the pointer to an array + /// + /// Writes in little-endian format + public static bool WriteLittleEndian(this byte[] content, ref int offset, ushort value) + { + byte[] buffer = value.GetBytesLittleEndian(); + return WriteFromBuffer(content, ref offset, buffer); + } + + /// + /// Write a UInt16 and increment the pointer to an array + /// + /// Writes in both-endian format + public static bool WriteBothEndian(this byte[] content, ref int offset, BothUInt16 value) + { + bool actual = content.WriteLittleEndian(ref offset, value.LittleEndian); + actual &= content.WriteBigEndian(ref offset, value.BigEndian); + return actual; + } + +#if NET5_0_OR_GREATER + /// + /// Write a Half and increment the pointer to an array + /// + /// Writes in machine native format + public static bool Write(this byte[] content, ref int offset, Half value) + { + if (BitConverter.IsLittleEndian) + return content.WriteLittleEndian(ref offset, value); + else + return content.WriteBigEndian(ref offset, value); + } + + /// + /// Write a Half and increment the pointer to an array + /// + /// Writes in big-endian format + public static bool WriteBigEndian(this byte[] content, ref int offset, Half value) + { + byte[] buffer = value.GetBytesBigEndian(); + return WriteFromBuffer(content, ref offset, buffer); + } + + /// + /// Write a Half and increment the pointer to an array + /// + /// Writes in little-endian format + public static bool WriteLittleEndian(this byte[] content, ref int offset, Half value) + { + byte[] buffer = value.GetBytesLittleEndian(); + return WriteFromBuffer(content, ref offset, buffer); + } +#endif + + /// + /// Write an Int24 and increment the pointer to an array + /// + /// Writes in machine native format + public static bool Write(this byte[] content, ref int offset, Int24 value) + { + if (BitConverter.IsLittleEndian) + return content.WriteLittleEndian(ref offset, value); + else + return content.WriteBigEndian(ref offset, value); + } + + /// + /// Write an Int24 and increment the pointer to an array + /// + /// Writes in big-endian format + public static bool WriteBigEndian(this byte[] content, ref int offset, Int24 value) + { + byte[] buffer = value.GetBytesBigEndian(); + return WriteFromBuffer(content, ref offset, buffer); + } + + /// + /// Write an Int24 and increment the pointer to an array + /// + /// Writes in little-endian format + public static bool WriteLittleEndian(this byte[] content, ref int offset, Int24 value) + { + byte[] buffer = value.GetBytesLittleEndian(); + return WriteFromBuffer(content, ref offset, buffer); + } + + /// + /// Write a UInt24 and increment the pointer to an array + /// + /// Writes in machine native format + public static bool Write(this byte[] content, ref int offset, UInt24 value) + { + if (BitConverter.IsLittleEndian) + return content.WriteLittleEndian(ref offset, value); + else + return content.WriteBigEndian(ref offset, value); + } + + /// + /// Write a UInt24 and increment the pointer to an array + /// + /// Writes in big-endian format + public static bool WriteBigEndian(this byte[] content, ref int offset, UInt24 value) + { + byte[] buffer = value.GetBytesBigEndian(); + return WriteFromBuffer(content, ref offset, buffer); + } + + /// + /// Write a UInt24 and increment the pointer to an array + /// + /// Writes in little-endian format + public static bool WriteLittleEndian(this byte[] content, ref int offset, UInt24 value) + { + byte[] buffer = value.GetBytesLittleEndian(); + return WriteFromBuffer(content, ref offset, buffer); + } + + /// + /// Write an Int32 and increment the pointer to an array + /// + /// Writes in machine native format + public static bool Write(this byte[] content, ref int offset, int value) + { + if (BitConverter.IsLittleEndian) + return content.WriteLittleEndian(ref offset, value); + else + return content.WriteBigEndian(ref offset, value); + } + + /// + /// Write an Int32 and increment the pointer to an array + /// + /// Writes in big-endian format + public static bool WriteBigEndian(this byte[] content, ref int offset, int value) + { + byte[] buffer = value.GetBytesBigEndian(); + return WriteFromBuffer(content, ref offset, buffer); + } + + /// + /// Write an Int32 and increment the pointer to an array + /// + /// Writes in little-endian format + public static bool WriteLittleEndian(this byte[] content, ref int offset, int value) + { + byte[] buffer = value.GetBytesLittleEndian(); + return WriteFromBuffer(content, ref offset, buffer); + } + + /// + /// Write a Int32 and increment the pointer to an array + /// + /// Writes in both-endian format + public static bool WriteBothEndian(this byte[] content, ref int offset, BothInt32 value) + { + bool actual = content.WriteLittleEndian(ref offset, value.LittleEndian); + actual &= content.WriteBigEndian(ref offset, value.BigEndian); + return actual; + } + + /// + /// Write a UInt32 and increment the pointer to an array + /// + /// Writes in machine native format + public static bool Write(this byte[] content, ref int offset, uint value) + { + if (BitConverter.IsLittleEndian) + return content.WriteLittleEndian(ref offset, value); + else + return content.WriteBigEndian(ref offset, value); + } + + /// + /// Write a UInt32 and increment the pointer to an array + /// + /// Writes in big-endian format + public static bool WriteBigEndian(this byte[] content, ref int offset, uint value) + { + byte[] buffer = value.GetBytesBigEndian(); + return WriteFromBuffer(content, ref offset, buffer); + } + + /// + /// Write a UInt32 and increment the pointer to an array + /// + /// Writes in little-endian format + public static bool WriteLittleEndian(this byte[] content, ref int offset, uint value) + { + byte[] buffer = value.GetBytesLittleEndian(); + return WriteFromBuffer(content, ref offset, buffer); + } + + /// + /// Write a UInt32 and increment the pointer to an array + /// + /// Writes in both-endian format + public static bool WriteBothEndian(this byte[] content, ref int offset, BothUInt32 value) + { + bool actual = content.WriteLittleEndian(ref offset, value.LittleEndian); + actual &= content.WriteBigEndian(ref offset, value.BigEndian); + return actual; + } + + /// + /// Write a Single and increment the pointer to an array + /// + /// Writes in machine native format + public static bool Write(this byte[] content, ref int offset, float value) + { + if (BitConverter.IsLittleEndian) + return content.WriteLittleEndian(ref offset, value); + else + return content.WriteBigEndian(ref offset, value); + } + + /// + /// Write a Single and increment the pointer to an array + /// + /// Writes in big-endian format + public static bool WriteBigEndian(this byte[] content, ref int offset, float value) + { + byte[] buffer = value.GetBytesBigEndian(); + return WriteFromBuffer(content, ref offset, buffer); + } + + /// + /// Write a Single and increment the pointer to an array + /// + /// Writes in little-endian format + public static bool WriteLittleEndian(this byte[] content, ref int offset, float value) + { + byte[] buffer = value.GetBytesLittleEndian(); + return WriteFromBuffer(content, ref offset, buffer); + } + + /// + /// Write an Int48 and increment the pointer to an array + /// + /// Writes in machine native format + public static bool Write(this byte[] content, ref int offset, Int48 value) + { + if (BitConverter.IsLittleEndian) + return content.WriteLittleEndian(ref offset, value); + else + return content.WriteBigEndian(ref offset, value); + } + + /// + /// Write an Int48 and increment the pointer to an array + /// + /// Writes in big-endian format + public static bool WriteBigEndian(this byte[] content, ref int offset, Int48 value) + { + byte[] buffer = value.GetBytesBigEndian(); + return WriteFromBuffer(content, ref offset, buffer); + } + + /// + /// Write an Int48 and increment the pointer to an array + /// + /// Writes in little-endian format + public static bool WriteLittleEndian(this byte[] content, ref int offset, Int48 value) + { + byte[] buffer = value.GetBytesLittleEndian(); + return WriteFromBuffer(content, ref offset, buffer); + } + + /// + /// Write a UInt48 and increment the pointer to an array + /// + /// Writes in machine native format + public static bool Write(this byte[] content, ref int offset, UInt48 value) + { + if (BitConverter.IsLittleEndian) + return content.WriteLittleEndian(ref offset, value); + else + return content.WriteBigEndian(ref offset, value); + } + + /// + /// Write a UInt48 and increment the pointer to an array + /// + /// Writes in big-endian format + public static bool WriteBigEndian(this byte[] content, ref int offset, UInt48 value) + { + byte[] buffer = value.GetBytesBigEndian(); + return WriteFromBuffer(content, ref offset, buffer); + } + + /// + /// Write a UInt48 and increment the pointer to an array + /// + /// Writes in little-endian format + public static bool WriteLittleEndian(this byte[] content, ref int offset, UInt48 value) + { + byte[] buffer = value.GetBytesLittleEndian(); + return WriteFromBuffer(content, ref offset, buffer); + } + + /// + /// Write an Int64 and increment the pointer to an array + /// + /// Writes in machine native format + public static bool Write(this byte[] content, ref int offset, long value) + { + if (BitConverter.IsLittleEndian) + return content.WriteLittleEndian(ref offset, value); + else + return content.WriteBigEndian(ref offset, value); + } + + /// + /// Write an Int64 and increment the pointer to an array + /// + /// Writes in big-endian format + public static bool WriteBigEndian(this byte[] content, ref int offset, long value) + { + byte[] buffer = value.GetBytesBigEndian(); + return WriteFromBuffer(content, ref offset, buffer); + } + + /// + /// Write an Int64 and increment the pointer to an array + /// + /// Writes in little-endian format + public static bool WriteLittleEndian(this byte[] content, ref int offset, long value) + { + byte[] buffer = value.GetBytesLittleEndian(); + return WriteFromBuffer(content, ref offset, buffer); + } + + /// + /// Write a Int64 and increment the pointer to an array + /// + /// Writes in both-endian format + public static bool WriteBothEndian(this byte[] content, ref int offset, BothInt64 value) + { + bool actual = content.WriteLittleEndian(ref offset, value.LittleEndian); + actual &= content.WriteBigEndian(ref offset, value.BigEndian); + return actual; + } + + /// + /// Write a UInt64 and increment the pointer to an array + /// + /// Writes in machine native format + public static bool Write(this byte[] content, ref int offset, ulong value) + { + if (BitConverter.IsLittleEndian) + return content.WriteLittleEndian(ref offset, value); + else + return content.WriteBigEndian(ref offset, value); + } + + /// + /// Write a UInt64 and increment the pointer to an array + /// + /// Writes in big-endian format + public static bool WriteBigEndian(this byte[] content, ref int offset, ulong value) + { + byte[] buffer = value.GetBytesBigEndian(); + return WriteFromBuffer(content, ref offset, buffer); + } + + /// + /// Write a UInt64 and increment the pointer to an array + /// + /// Writes in little-endian format + public static bool WriteLittleEndian(this byte[] content, ref int offset, ulong value) + { + byte[] buffer = value.GetBytesLittleEndian(); + return WriteFromBuffer(content, ref offset, buffer); + } + + /// + /// Write a UInt64 and increment the pointer to an array + /// + /// Writes in both-endian format + public static bool WriteBothEndian(this byte[] content, ref int offset, BothUInt64 value) + { + bool actual = content.WriteLittleEndian(ref offset, value.LittleEndian); + actual &= content.WriteBigEndian(ref offset, value.BigEndian); + return actual; + } + + /// + /// Write a Double and increment the pointer to an array + /// + /// Writes in machine native format + public static bool Write(this byte[] content, ref int offset, double value) + { + if (BitConverter.IsLittleEndian) + return content.WriteLittleEndian(ref offset, value); + else + return content.WriteBigEndian(ref offset, value); + } + + /// + /// Write a Double and increment the pointer to an array + /// + /// Writes in big-endian format + public static bool WriteBigEndian(this byte[] content, ref int offset, double value) + { + byte[] buffer = value.GetBytesBigEndian(); + return WriteFromBuffer(content, ref offset, buffer); + } + + /// + /// Write a Double and increment the pointer to an array + /// + /// Writes in little-endian format + public static bool WriteLittleEndian(this byte[] content, ref int offset, double value) + { + byte[] buffer = value.GetBytesLittleEndian(); + return WriteFromBuffer(content, ref offset, buffer); + } + + /// + /// Write a Guid and increment the pointer to an array + /// + /// Writes in machine native format + public static bool Write(this byte[] content, ref int offset, Guid value) + { + if (BitConverter.IsLittleEndian) + return content.WriteLittleEndian(ref offset, value); + else + return content.WriteBigEndian(ref offset, value); + } + + /// + /// Write a Guid and increment the pointer to an array + /// + /// Writes in big-endian format + public static bool WriteBigEndian(this byte[] content, ref int offset, Guid value) + { + byte[] buffer = value.GetBytesBigEndian(); + return WriteFromBuffer(content, ref offset, buffer); + } + + /// + /// Write a Guid and increment the pointer to an array + /// + /// Writes in little-endian format + public static bool WriteLittleEndian(this byte[] content, ref int offset, Guid value) + { + byte[] buffer = value.GetBytesLittleEndian(); + return WriteFromBuffer(content, ref offset, buffer); + } + +#if NET7_0_OR_GREATER + /// + /// Write an Int128 and increment the pointer to an array + /// + /// Writes in machine native format + public static bool Write(this byte[] content, ref int offset, Int128 value) + { + if (BitConverter.IsLittleEndian) + return content.WriteLittleEndian(ref offset, value); + else + return content.WriteBigEndian(ref offset, value); + } + + /// + /// Write an Int128 and increment the pointer to an array + /// + /// Writes in big-endian format + public static bool WriteBigEndian(this byte[] content, ref int offset, Int128 value) + { + byte[] buffer = value.GetBytesBigEndian(); + return WriteFromBuffer(content, ref offset, buffer); + } + + /// + /// Write an Int128 and increment the pointer to an array + /// + /// Writes in little-endian format + public static bool WriteLittleEndian(this byte[] content, ref int offset, Int128 value) + { + byte[] buffer = value.GetBytesLittleEndian(); + return WriteFromBuffer(content, ref offset, buffer); + } + + /// + /// Write a UInt128 and increment the pointer to an array + /// + /// Writes in machine native format + public static bool Write(this byte[] content, ref int offset, UInt128 value) + { + if (BitConverter.IsLittleEndian) + return content.WriteLittleEndian(ref offset, value); + else + return content.WriteBigEndian(ref offset, value); + } + + /// + /// Write a UInt128 and increment the pointer to an array + /// + /// Writes in big-endian format + public static bool WriteBigEndian(this byte[] content, ref int offset, UInt128 value) + { + byte[] buffer = value.GetBytesBigEndian(); + return WriteFromBuffer(content, ref offset, buffer); + } + + /// + /// Write a UInt128 and increment the pointer to an array + /// + /// Writes in little-endian format + public static bool WriteLittleEndian(this byte[] content, ref int offset, UInt128 value) + { + byte[] buffer = value.GetBytesLittleEndian(); + return WriteFromBuffer(content, ref offset, buffer); + } +#endif + + /// + /// Write a Decimal and increment the pointer to an array + /// + /// Writes in machine native format + public static bool Write(this byte[] content, ref int offset, decimal value) + { + if (BitConverter.IsLittleEndian) + return content.WriteLittleEndian(ref offset, value); + else + return content.WriteBigEndian(ref offset, value); + } + + /// + /// Write a Decimal and increment the pointer to an array + /// + /// Writes in big-endian format + public static bool WriteBigEndian(this byte[] content, ref int offset, decimal value) + { + byte[] buffer = value.GetBytesBigEndian(); + return WriteFromBuffer(content, ref offset, buffer); + } + + /// + /// Write a Decimal and increment the pointer to an array + /// + /// Writes in little-endian format + public static bool WriteLittleEndian(this byte[] content, ref int offset, decimal value) + { + byte[] buffer = value.GetBytesLittleEndian(); + return WriteFromBuffer(content, ref offset, buffer); + } + + /// + /// Write an array of bytes to the byte array + /// + /// + /// Thrown if into + /// would not accomodate . + /// + private static bool WriteFromBuffer(byte[] content, ref int offset, byte[] value) + { + // Handle the 0-byte case + if (value.Length == 0) + return true; + + // If there are not enough bytes + if (offset + value.Length > content.Length) + throw new System.IO.EndOfStreamException(nameof(content)); + + // Handle the general case, forcing a write of the correct length + Array.Copy(value, 0, content, offset, value.Length); + offset += value.Length; + + return true; + } + } +} diff --git a/SabreTools.Numerics.Extensions/ExtensionAttribute.cs b/SabreTools.Numerics.Extensions/ExtensionAttribute.cs new file mode 100644 index 0000000..478ed8b --- /dev/null +++ b/SabreTools.Numerics.Extensions/ExtensionAttribute.cs @@ -0,0 +1,9 @@ +#if NET20 + +namespace System.Runtime.CompilerServices +{ + [AttributeUsage(AttributeTargets.Assembly | AttributeTargets.Class | AttributeTargets.Method)] + internal sealed class ExtensionAttribute : Attribute { } +} + +#endif diff --git a/SabreTools.IO.Extensions/NumericExtensions.cs b/SabreTools.Numerics.Extensions/NumericExtensions.cs similarity index 99% rename from SabreTools.IO.Extensions/NumericExtensions.cs rename to SabreTools.Numerics.Extensions/NumericExtensions.cs index ea48ab0..271b099 100644 --- a/SabreTools.IO.Extensions/NumericExtensions.cs +++ b/SabreTools.Numerics.Extensions/NumericExtensions.cs @@ -1,8 +1,7 @@ using System; using System.Collections.Generic; -using SabreTools.Numerics; -namespace SabreTools.IO.Extensions +namespace SabreTools.Numerics.Extensions { /// /// Extensions for numeric conversion diff --git a/SabreTools.Numerics.Extensions/README.MD b/SabreTools.Numerics.Extensions/README.MD new file mode 100644 index 0000000..c9b8c59 --- /dev/null +++ b/SabreTools.Numerics.Extensions/README.MD @@ -0,0 +1,15 @@ +# SabreTools.Numerics.Extensions + +This library contains numeric-related extensions with some functionality locked behind .NET version support. + +Relies on functionality found in `SabreTools.Numerics`. + +| Class | Description | +| --- | --- | +| `BinaryReaderExtensions` | Extensions to `BinaryReader` allowing for endian-specific reads, peeking functionality, and try-read functionality. | +| `BinaryWriterExtensions` | Extensions to `BinaryWriter` allowing for endian-specific writes. | +| `ByteArrayReaderExtensions` | Extensions to `byte[]` allowing for endian-specific reads, peeking functionality, and try-read functionality, similar to a stream. | +| `ByteArrayWriterExtensions` | Extensions to `byte[]` allowing for endian-specific writes, similar to a stream. | +| `NumericExtensions` | Common numeric type extensions allowing for endian-specific processing. | +| `StreamReaderExtensions` | Extensions to `Stream` allowing for endian-specific reads, peeking functionality, and try-read functionality. | +| `StreamWriterExtensions` | Extensions to `Stream` allowing for endian-specific writes. | diff --git a/SabreTools.Numerics.Extensions/SabreTools.Numerics.Extensions.csproj b/SabreTools.Numerics.Extensions/SabreTools.Numerics.Extensions.csproj new file mode 100644 index 0000000..89cfa11 --- /dev/null +++ b/SabreTools.Numerics.Extensions/SabreTools.Numerics.Extensions.csproj @@ -0,0 +1,36 @@ + + + + + net20;net35;net40;net452;net462;net472;net48;netcoreapp3.1;net5.0;net6.0;net7.0;net8.0;net9.0;net10.0;netstandard2.0;netstandard2.1 + true + true + latest + CS0618 + enable + true + snupkg + true + 1.9.1 + + + Matt Nadareski + Numeric-related extensions with some functionality locked behind .NET version support + Copyright (c) Matt Nadareski 2019-2026 + https://github.com/SabreTools/ + README.md + https://github.com/SabreTools/SabreTools.IO + git + io reading writing ini csv ssv tsv + MIT + + + + + + + + + + + \ No newline at end of file diff --git a/SabreTools.Numerics.Extensions/StreamReaderExtensions.cs b/SabreTools.Numerics.Extensions/StreamReaderExtensions.cs new file mode 100644 index 0000000..6b39934 --- /dev/null +++ b/SabreTools.Numerics.Extensions/StreamReaderExtensions.cs @@ -0,0 +1,2706 @@ +using System; +using System.IO; + +namespace SabreTools.Numerics.Extensions +{ + /// + /// Extensions for Streams + /// + public static class StreamReaderExtensions + { + #region Exact Read + + /// + /// Read a UInt8 from the stream + /// + public static byte ReadByteValue(this Stream stream) + { + byte[] buffer = ReadExactlyToBuffer(stream, 1); + return buffer[0]; + } + + /// + /// Read a UInt8 from the stream + /// + /// Reads in both-endian format + public static BothUInt8 ReadByteBothEndian(this Stream stream) + { + byte le = stream.ReadByteValue(); + byte be = stream.ReadByteValue(); + return new BothUInt8(le, be); + } + + /// + /// Read a UInt8[] from the stream + /// + public static byte[] ReadBytes(this Stream stream, int count) + => ReadExactlyToBuffer(stream, count); + + /// + /// Read an Int8 from the stream + /// + public static sbyte ReadSByte(this Stream stream) + { + byte[] buffer = ReadExactlyToBuffer(stream, 1); + return (sbyte)buffer[0]; + } + + /// + /// Read a UInt8 from the stream + /// + /// Reads in both-endian format + public static BothInt8 ReadSByteBothEndian(this Stream stream) + { + sbyte le = stream.ReadSByte(); + sbyte be = stream.ReadSByte(); + return new BothInt8(le, be); + } + + /// + /// Read a Char from the stream + /// + public static char ReadChar(this Stream stream) + { + byte[] buffer = ReadExactlyToBuffer(stream, 1); + return (char)buffer[0]; + } + + /// + /// Read an Int16 from the stream + /// + /// Reads in machine native format + public static short ReadInt16(this Stream stream) + { + if (BitConverter.IsLittleEndian) + return stream.ReadInt16LittleEndian(); + else + return stream.ReadInt16BigEndian(); + } + + /// + /// Read an Int16 from the stream + /// + /// Reads in big-endian format + public static short ReadInt16BigEndian(this Stream stream) + { + byte[] buffer = ReadExactlyToBuffer(stream, 2); + return buffer.ToInt16BigEndian(); + } + + /// + /// Read an Int16 from the stream + /// + /// Reads in little-endian format + public static short ReadInt16LittleEndian(this Stream stream) + { + byte[] buffer = ReadExactlyToBuffer(stream, 2); + return buffer.ToInt16LittleEndian(); + } + + /// + /// Read a Int16 from the stream + /// + /// Reads in both-endian format + public static BothInt16 ReadInt16BothEndian(this Stream stream) + { + short le = stream.ReadInt16LittleEndian(); + short be = stream.ReadInt16BigEndian(); + return new BothInt16(le, be); + } + + /// + /// Read a UInt16 from the stream + /// + /// Reads in machine native format + public static ushort ReadUInt16(this Stream stream) + { + if (BitConverter.IsLittleEndian) + return stream.ReadUInt16LittleEndian(); + else + return stream.ReadUInt16BigEndian(); + } + + /// + /// Read a UInt16 from the stream + /// + /// Reads in big-endian format + public static ushort ReadUInt16BigEndian(this Stream stream) + { + byte[] buffer = ReadExactlyToBuffer(stream, 2); + return buffer.ToUInt16BigEndian(); + } + + /// + /// Read a UInt16 from the stream + /// + /// Reads in little-endian format + public static ushort ReadUInt16LittleEndian(this Stream stream) + { + byte[] buffer = ReadExactlyToBuffer(stream, 2); + return buffer.ToUInt16LittleEndian(); + } + + /// + /// Read a UInt16 from the stream + /// + /// Reads in both-endian format + public static BothUInt16 ReadUInt16BothEndian(this Stream stream) + { + ushort le = stream.ReadUInt16LittleEndian(); + ushort be = stream.ReadUInt16BigEndian(); + return new BothUInt16(le, be); + } + + /// + /// Read a WORD (2-byte) from the stream + /// + /// Reads in machine native format + public static ushort ReadWORD(this Stream stream) + => stream.ReadUInt16(); + + /// + /// Read a WORD (2-byte) from the stream + /// + /// Reads in big-endian format + public static ushort ReadWORDBigEndian(this Stream stream) + => stream.ReadUInt16BigEndian(); + + /// + /// Read a WORD (2-byte) from the stream + /// + /// Reads in little-endian format + public static ushort ReadWORDLittleEndian(this Stream stream) + => stream.ReadUInt16LittleEndian(); + + /// + /// Read a WORD (2-byte) from the stream + /// + /// Reads in both-endian format + public static BothUInt16 ReadWORDBothEndian(this Stream stream) + => stream.ReadUInt16BothEndian(); + +#if NET5_0_OR_GREATER + /// + /// Read a Half from the stream + /// + /// Reads in machine native format + public static Half ReadHalf(this Stream stream) + { + if (BitConverter.IsLittleEndian) + return stream.ReadHalfLittleEndian(); + else + return stream.ReadHalfBigEndian(); + } + + /// + /// Read a Half from the stream + /// + /// Reads in big-endian format + public static Half ReadHalfBigEndian(this Stream stream) + { + byte[] buffer = ReadExactlyToBuffer(stream, 2); + return buffer.ToHalfBigEndian(); + } + + /// + /// Read a Half from the stream + /// + /// Reads in little-endian format + public static Half ReadHalfLittleEndian(this Stream stream) + { + byte[] buffer = ReadExactlyToBuffer(stream, 2); + return buffer.ToHalfLittleEndian(); + } +#endif + + /// + /// Read an Int24 from the stream + /// + /// Reads in machine native format + public static Int24 ReadInt24(this Stream stream) + { + if (BitConverter.IsLittleEndian) + return stream.ReadInt24LittleEndian(); + else + return stream.ReadInt24BigEndian(); + } + + /// + /// Read an Int24 from the stream + /// + /// Reads in big-endian format + public static Int24 ReadInt24BigEndian(this Stream stream) + { + byte[] buffer = ReadExactlyToBuffer(stream, 3); + return buffer.ToInt24BigEndian(); + } + + /// + /// Read an Int24 from the stream + /// + /// Reads in little-endian format + public static Int24 ReadInt24LittleEndian(this Stream stream) + { + byte[] buffer = ReadExactlyToBuffer(stream, 3); + return buffer.ToInt24LittleEndian(); + } + + /// + /// Read a UInt24 from the stream + /// + /// Reads in machine native format + public static UInt24 ReadUInt24(this Stream stream) + { + if (BitConverter.IsLittleEndian) + return stream.ReadUInt24LittleEndian(); + else + return stream.ReadUInt24BigEndian(); + } + + /// + /// Read a UInt24 from the stream + /// + /// Reads in big-endian format + public static UInt24 ReadUInt24BigEndian(this Stream stream) + { + byte[] buffer = ReadExactlyToBuffer(stream, 3); + return buffer.ToUInt24BigEndian(); + } + + /// + /// Read a UInt24 from the stream + /// + /// Reads in little-endian format + public static UInt24 ReadUInt24LittleEndian(this Stream stream) + { + byte[] buffer = ReadExactlyToBuffer(stream, 3); + return buffer.ToUInt24LittleEndian(); + } + + /// + /// Read an Int32 from the stream + /// + /// Reads in machine native format + public static int ReadInt32(this Stream stream) + { + if (BitConverter.IsLittleEndian) + return stream.ReadInt32LittleEndian(); + else + return stream.ReadInt32BigEndian(); + } + + /// + /// Read an Int32 from the stream + /// + /// Reads in big-endian format + public static int ReadInt32BigEndian(this Stream stream) + { + byte[] buffer = ReadExactlyToBuffer(stream, 4); + return buffer.ToInt32BigEndian(); + } + + /// + /// Read an Int32 from the stream + /// + /// Reads in little-endian format + public static int ReadInt32LittleEndian(this Stream stream) + { + byte[] buffer = ReadExactlyToBuffer(stream, 4); + return buffer.ToInt32LittleEndian(); + } + + /// + /// Read a Int32 from the stream + /// + /// Reads in both-endian format + public static BothInt32 ReadInt32BothEndian(this Stream stream) + { + int le = stream.ReadInt32LittleEndian(); + int be = stream.ReadInt32BigEndian(); + return new BothInt32(le, be); + } + + /// + /// Read a UInt32 from the stream + /// + /// Reads in machine native format + public static uint ReadUInt32(this Stream stream) + { + if (BitConverter.IsLittleEndian) + return stream.ReadUInt32LittleEndian(); + else + return stream.ReadUInt32BigEndian(); + } + + /// + /// Read a UInt32 from the stream + /// + /// Reads in big-endian format + public static uint ReadUInt32BigEndian(this Stream stream) + { + byte[] buffer = ReadExactlyToBuffer(stream, 4); + return buffer.ToUInt32BigEndian(); + } + + /// + /// Read a UInt32 from the stream + /// + /// Reads in little-endian format + public static uint ReadUInt32LittleEndian(this Stream stream) + { + byte[] buffer = ReadExactlyToBuffer(stream, 4); + return buffer.ToUInt32LittleEndian(); + } + + /// + /// Read a UInt32 from the stream + /// + /// Reads in both-endian format + public static BothUInt32 ReadUInt32BothEndian(this Stream stream) + { + uint le = stream.ReadUInt32LittleEndian(); + uint be = stream.ReadUInt32BigEndian(); + return new BothUInt32(le, be); + } + + /// + /// Read a DWORD (4-byte) from the stream + /// + /// Reads in machine native format + public static uint ReadDWORD(this Stream stream) + => stream.ReadUInt32(); + + /// + /// Read a DWORD (4-byte) from the stream + /// + /// Reads in big-endian format + public static uint ReadDWORDBigEndian(this Stream stream) + => stream.ReadUInt32BigEndian(); + + /// + /// Read a DWORD (4-byte) from the stream + /// + /// Reads in little-endian format + public static uint ReadDWORDLittleEndian(this Stream stream) + => stream.ReadUInt32LittleEndian(); + + /// + /// Read a DWORD (4-byte) from the stream + /// + /// Reads in both-endian format + public static BothUInt32 ReadDWORDBothEndian(this Stream stream) + => stream.ReadUInt32BothEndian(); + + /// + /// Read a Single from the stream + /// + /// Reads in machine native format + public static float ReadSingle(this Stream stream) + { + if (BitConverter.IsLittleEndian) + return stream.ReadSingleLittleEndian(); + else + return stream.ReadSingleBigEndian(); + } + + /// + /// Read a Single from the stream + /// + /// Reads in big-endian format + public static float ReadSingleBigEndian(this Stream stream) + { + byte[] buffer = ReadExactlyToBuffer(stream, 4); + return buffer.ToSingleBigEndian(); + } + + /// + /// Read a Single from the stream + /// + /// Reads in little-endian format + public static float ReadSingleLittleEndian(this Stream stream) + { + byte[] buffer = ReadExactlyToBuffer(stream, 4); + return buffer.ToSingleLittleEndian(); + } + + /// + /// Read an Int48 from the stream + /// + /// Reads in machine native format + public static Int48 ReadInt48(this Stream stream) + { + if (BitConverter.IsLittleEndian) + return stream.ReadInt48LittleEndian(); + else + return stream.ReadInt48BigEndian(); + } + + /// + /// Read an Int48 from the stream + /// + /// Reads in big-endian format + public static Int48 ReadInt48BigEndian(this Stream stream) + { + byte[] buffer = ReadExactlyToBuffer(stream, 6); + return buffer.ToInt48BigEndian(); + } + + /// + /// Read an Int48 from the stream + /// + /// Reads in little-endian format + public static Int48 ReadInt48LittleEndian(this Stream stream) + { + byte[] buffer = ReadExactlyToBuffer(stream, 6); + return buffer.ToInt48LittleEndian(); + } + + /// + /// Read a UInt48 from the stream + /// + /// Reads in machine native format + public static UInt48 ReadUInt48(this Stream stream) + { + if (BitConverter.IsLittleEndian) + return stream.ReadUInt48LittleEndian(); + else + return stream.ReadUInt48BigEndian(); + } + + /// + /// Read a UInt48 from the stream + /// + /// Reads in big-endian format + public static UInt48 ReadUInt48BigEndian(this Stream stream) + { + byte[] buffer = ReadExactlyToBuffer(stream, 6); + return buffer.ToUInt48BigEndian(); + } + + /// + /// Read an UInt48 from the stream + /// + /// Reads in little-endian format + public static UInt48 ReadUInt48LittleEndian(this Stream stream) + { + byte[] buffer = ReadExactlyToBuffer(stream, 6); + return buffer.ToUInt48LittleEndian(); + } + + /// + /// Read an Int64 from the stream + /// + /// Reads in machine native format + public static long ReadInt64(this Stream stream) + { + if (BitConverter.IsLittleEndian) + return stream.ReadInt64LittleEndian(); + else + return stream.ReadInt64BigEndian(); + } + + /// + /// Read an Int64 from the stream + /// + /// Reads in big-endian format + public static long ReadInt64BigEndian(this Stream stream) + { + byte[] buffer = ReadExactlyToBuffer(stream, 8); + return buffer.ToInt64BigEndian(); + } + + /// + /// Read an Int64 from the stream + /// + /// Reads in little-endian format + public static long ReadInt64LittleEndian(this Stream stream) + { + byte[] buffer = ReadExactlyToBuffer(stream, 8); + return buffer.ToInt64LittleEndian(); + } + + /// + /// Read a Int64 from the stream + /// + /// Reads in both-endian format + public static BothInt64 ReadInt64BothEndian(this Stream stream) + { + long le = stream.ReadInt64LittleEndian(); + long be = stream.ReadInt64BigEndian(); + return new BothInt64(le, be); + } + + /// + /// Read a UInt64 from the stream + /// + /// Reads in machine native format + public static ulong ReadUInt64(this Stream stream) + { + if (BitConverter.IsLittleEndian) + return stream.ReadUInt64LittleEndian(); + else + return stream.ReadUInt64BigEndian(); + } + + /// + /// Read a UInt64 from the stream + /// + /// Reads in big-endian format + public static ulong ReadUInt64BigEndian(this Stream stream) + { + byte[] buffer = ReadExactlyToBuffer(stream, 8); + return buffer.ToUInt64BigEndian(); + } + + /// + /// Read a UInt64 from the stream + /// + /// Reads in little-endian format + public static ulong ReadUInt64LittleEndian(this Stream stream) + { + byte[] buffer = ReadExactlyToBuffer(stream, 8); + return buffer.ToUInt64LittleEndian(); + } + + /// + /// Read a UInt64 from the stream + /// + /// Reads in both-endian format + public static BothUInt64 ReadUInt64BothEndian(this Stream stream) + { + ulong le = stream.ReadUInt64LittleEndian(); + ulong be = stream.ReadUInt64BigEndian(); + return new BothUInt64(le, be); + } + + /// + /// Read a QWORD (8-byte) from the stream + /// + /// Reads in machine native format + public static ulong ReadQWORD(this Stream stream) + => stream.ReadUInt64(); + + /// + /// Read a QWORD (8-byte) from the stream + /// + /// Reads in big-endian format + public static ulong ReadQWORDBigEndian(this Stream stream) + => stream.ReadUInt64BigEndian(); + + /// + /// Read a QWORD (8-byte) from the stream + /// + /// Reads in little-endian format + public static ulong ReadQWORDLittleEndian(this Stream stream) + => stream.ReadUInt64LittleEndian(); + + /// + /// Read a QWORD (8-byte) from the stream + /// + /// Reads in both-endian format + public static BothUInt64 ReadQWORDBothEndian(this Stream stream) + => stream.ReadUInt64BothEndian(); + + /// + /// Read a Double from the stream + /// + /// Reads in machine native format + public static double ReadDouble(this Stream stream) + { + if (BitConverter.IsLittleEndian) + return stream.ReadDoubleLittleEndian(); + else + return stream.ReadDoubleBigEndian(); + } + + /// + /// Read a Double from the stream + /// + /// Reads in big-endian format + public static double ReadDoubleBigEndian(this Stream stream) + { + byte[] buffer = ReadExactlyToBuffer(stream, 8); + return buffer.ToDoubleBigEndian(); + } + + /// + /// Read a Double from the stream + /// + /// Reads in little-endian format + public static double ReadDoubleLittleEndian(this Stream stream) + { + byte[] buffer = ReadExactlyToBuffer(stream, 8); + return buffer.ToDoubleLittleEndian(); + } + + /// + /// Read a Guid from the stream + /// + /// Reads in machine native format + public static Guid ReadGuid(this Stream stream) + { + if (BitConverter.IsLittleEndian) + return stream.ReadGuidLittleEndian(); + else + return stream.ReadGuidBigEndian(); + } + + /// + /// Read a Guid from the stream + /// + /// Reads in big-endian format + public static Guid ReadGuidBigEndian(this Stream stream) + { + byte[] buffer = ReadExactlyToBuffer(stream, 16); + return buffer.ToGuidBigEndian(); + } + + /// + /// Read a Guid from the stream + /// + /// Reads in little-endian format + public static Guid ReadGuidLittleEndian(this Stream stream) + { + byte[] buffer = ReadExactlyToBuffer(stream, 16); + return buffer.ToGuidLittleEndian(); + } + +#if NET7_0_OR_GREATER + /// + /// Read an Int128 from the stream + /// + /// Reads in machine native format + public static Int128 ReadInt128(this Stream stream) + { + if (BitConverter.IsLittleEndian) + return stream.ReadInt128LittleEndian(); + else + return stream.ReadInt128BigEndian(); + } + + /// + /// Read an Int128 from the stream + /// + /// Reads in big-endian format + public static Int128 ReadInt128BigEndian(this Stream stream) + { + byte[] buffer = ReadExactlyToBuffer(stream, 16); + return buffer.ToInt128BigEndian(); + } + + /// + /// Read an Int128 from the stream + /// + /// Reads in little-endian format + public static Int128 ReadInt128LittleEndian(this Stream stream) + { + byte[] buffer = ReadExactlyToBuffer(stream, 16); + return buffer.ToInt128LittleEndian(); + } + + /// + /// Read a UInt128 from the stream + /// + /// Reads in machine native format + public static UInt128 ReadUInt128(this Stream stream) + { + if (BitConverter.IsLittleEndian) + return stream.ReadUInt128LittleEndian(); + else + return stream.ReadUInt128BigEndian(); + } + + /// + /// Read a UInt128 from the stream + /// + /// Reads in big-endian format + public static UInt128 ReadUInt128BigEndian(this Stream stream) + { + byte[] buffer = ReadExactlyToBuffer(stream, 16); + return buffer.ToUInt128BigEndian(); + } + + /// + /// Read a UInt128 from the stream + /// + /// Reads in little-endian format + public static UInt128 ReadUInt128LittleEndian(this Stream stream) + { + byte[] buffer = ReadExactlyToBuffer(stream, 16); + return buffer.ToUInt128LittleEndian(); + } +#endif + + /// + /// Read a Decimal from the stream + /// + /// Reads in machine native format + public static decimal ReadDecimal(this Stream stream) + { + if (BitConverter.IsLittleEndian) + return stream.ReadDecimalLittleEndian(); + else + return stream.ReadDecimalBigEndian(); + } + + /// + /// Read a Decimal from the stream + /// + /// Reads in big-endian format + public static decimal ReadDecimalBigEndian(this Stream stream) + { + byte[] buffer = ReadExactlyToBuffer(stream, 16); + return buffer.ToDecimalBigEndian(); + } + + /// + /// Read a Decimal from the stream + /// + /// Reads in little-endian format + public static decimal ReadDecimalLittleEndian(this Stream stream) + { + byte[] buffer = ReadExactlyToBuffer(stream, 16); + return buffer.ToDecimalLittleEndian(); + } + + /// + /// Read a number of bytes from the stream to a buffer + /// + /// + /// Thrown if is an invalid value. + /// + /// + /// Thrown if the requested is greater + /// than the read bytes from . + /// length. + /// + private static byte[] ReadExactlyToBuffer(Stream stream, int length) + { + // If we have an invalid length + if (length < 0) + throw new ArgumentOutOfRangeException($"{nameof(length)} must be 0 or a positive value, {length} requested"); + + // Handle the 0-byte case + if (length == 0) + return []; + + // Handle the general case, forcing a read of the correct length + byte[] buffer = new byte[length]; + int read = stream.Read(buffer, 0, length); + if (read < length) + throw new EndOfStreamException($"Requested to read {length} bytes from {nameof(stream)}, {read} returned"); + + return buffer; + } + + #endregion + + #region Peek Read + + /// + /// Peek a UInt8 from the stream + /// + /// Only works properly on seekable streams + public static byte PeekByte(this Stream stream) + { + byte value = stream.ReadByteValue(); + stream.Seek(-1, SeekOrigin.Current); + return value; + } + + /// + /// Peek a UInt8 from the stream + /// + /// Only works properly on seekable streams + public static byte PeekByteValue(this Stream stream) + => stream.PeekByte(); + + /// + /// Peek a UInt8 from the stream + /// + /// Reads in both-endian format + /// Only works properly on seekable streams + public static BothUInt8 PeekByteBothEndian(this Stream stream) + { + BothUInt8 value = stream.ReadByteBothEndian(); + stream.Seek(-2, SeekOrigin.Current); + return value; + } + + /// + /// Peek a UInt8[] from the stream + /// + /// Only works properly on seekable streams + public static byte[] PeekBytes(this Stream stream, int count) + { + byte[] value = stream.ReadBytes(count); + stream.Seek(-count, SeekOrigin.Current); + return value; + } + + /// + /// Peek an Int8 from the stream + /// + /// Only works properly on seekable streams + public static sbyte PeekSByte(this Stream stream) + { + sbyte value = stream.ReadSByte(); + stream.Seek(-1, SeekOrigin.Current); + return value; + } + + /// + /// Peek a Int8 from the stream + /// + /// Reads in both-endian format + /// Only works properly on seekable streams + public static BothInt8 PeekSByteBothEndian(this Stream stream) + { + BothInt8 value = stream.ReadSByteBothEndian(); + stream.Seek(-2, SeekOrigin.Current); + return value; + } + + /// + /// Peek a Char from the stream + /// + /// Only works properly on seekable streams + public static char PeekChar(this Stream stream) + { + char value = stream.ReadChar(); + stream.Seek(-1, SeekOrigin.Current); + return value; + } + + /// + /// Peek an Int16 from the stream + /// + /// Reads in machine native format + /// Only works properly on seekable streams + public static short PeekInt16(this Stream stream) + { + if (BitConverter.IsLittleEndian) + return stream.PeekInt16LittleEndian(); + else + return stream.PeekInt16BigEndian(); + } + + /// + /// Peek an Int16 from the stream + /// + /// Reads in big-endian format + /// Only works properly on seekable streams + public static short PeekInt16BigEndian(this Stream stream) + { + short value = stream.ReadInt16BigEndian(); + stream.Seek(-2, SeekOrigin.Current); + return value; + } + + /// + /// Peek an Int16 from the stream + /// + /// Reads in little-endian format + /// Only works properly on seekable streams + public static short PeekInt16LittleEndian(this Stream stream) + { + short value = stream.ReadInt16LittleEndian(); + stream.Seek(-2, SeekOrigin.Current); + return value; + } + + /// + /// Peek a Int16 from the stream + /// + /// Reads in both-endian format + /// Only works properly on seekable streams + public static BothInt16 PeekInt16BothEndian(this Stream stream) + { + BothInt16 value = stream.ReadInt16BothEndian(); + stream.Seek(-4, SeekOrigin.Current); + return value; + } + + /// + /// Peek a UInt16 from the stream + /// + /// Reads in machine native format + /// Only works properly on seekable streams + public static ushort PeekUInt16(this Stream stream) + { + if (BitConverter.IsLittleEndian) + return stream.PeekUInt16LittleEndian(); + else + return stream.PeekUInt16BigEndian(); + } + + /// + /// Peek a UInt16 from the stream + /// + /// Reads in big-endian format + /// Only works properly on seekable streams + public static ushort PeekUInt16BigEndian(this Stream stream) + { + ushort value = stream.ReadUInt16BigEndian(); + stream.Seek(-2, SeekOrigin.Current); + return value; + } + + /// + /// Peek a UInt16 from the stream + /// + /// Reads in little-endian format + /// Only works properly on seekable streams + public static ushort PeekUInt16LittleEndian(this Stream stream) + { + ushort value = stream.ReadUInt16LittleEndian(); + stream.Seek(-2, SeekOrigin.Current); + return value; + } + + /// + /// Peek a UInt16 from the stream + /// + /// Reads in both-endian format + /// Only works properly on seekable streams + public static BothUInt16 PeekUInt16BothEndian(this Stream stream) + { + BothUInt16 value = stream.ReadUInt16BothEndian(); + stream.Seek(-4, SeekOrigin.Current); + return value; + } + + /// + /// Peek a WORD (2-byte) from the stream + /// + /// Reads in machine native format + /// Only works properly on seekable streams + public static ushort PeekWORD(this Stream stream) + => stream.PeekUInt16(); + + /// + /// Peek a WORD (2-byte) from the stream + /// + /// Reads in big-endian format + /// Only works properly on seekable streams + public static ushort PeekWORDBigEndian(this Stream stream) + => stream.PeekUInt16BigEndian(); + + /// + /// Peek a WORD (2-byte) from the stream + /// + /// Reads in little-endian format + /// Only works properly on seekable streams + public static ushort PeekWORDLittleEndian(this Stream stream) + => stream.PeekUInt16LittleEndian(); + + /// + /// Peek a WORD (2-byte) from the stream + /// + /// Reads in little-endian format + /// Only works properly on seekable streams + public static BothUInt16 PeekWORDBothEndian(this Stream stream) + => stream.PeekUInt16BothEndian(); + +#if NET5_0_OR_GREATER + /// + /// Peek a Half from the stream + /// + /// Reads in machine native format + /// Only works properly on seekable streams + public static Half PeekHalf(this Stream stream) + { + if (BitConverter.IsLittleEndian) + return stream.PeekHalfLittleEndian(); + else + return stream.PeekHalfBigEndian(); + } + + /// + /// Peek a Half from the stream + /// + /// Reads in big-endian format + /// Only works properly on seekable streams + public static Half PeekHalfBigEndian(this Stream stream) + { + Half value = stream.ReadHalfBigEndian(); + stream.Seek(-2, SeekOrigin.Current); + return value; + } + + /// + /// Peek a Half from the stream + /// + /// Reads in little-endian format + /// Only works properly on seekable streams + public static Half PeekHalfLittleEndian(this Stream stream) + { + Half value = stream.ReadHalfLittleEndian(); + stream.Seek(-2, SeekOrigin.Current); + return value; + } +#endif + + /// + /// Peek an Int24 from the stream + /// + /// Reads in machine native format + /// Only works properly on seekable streams + public static Int24 PeekInt24(this Stream stream) + { + if (BitConverter.IsLittleEndian) + return stream.PeekInt24LittleEndian(); + else + return stream.PeekInt24BigEndian(); + } + + /// + /// Peek an Int24 from the stream + /// + /// Reads in big-endian format + /// Only works properly on seekable streams + public static Int24 PeekInt24BigEndian(this Stream stream) + { + Int24 value = stream.ReadInt24BigEndian(); + stream.Seek(-3, SeekOrigin.Current); + return value; + } + + /// + /// Peek an Int24 from the stream + /// + /// Reads in little-endian format + /// Only works properly on seekable streams + public static Int24 PeekInt24LittleEndian(this Stream stream) + { + Int24 value = stream.ReadInt24LittleEndian(); + stream.Seek(-3, SeekOrigin.Current); + return value; + } + + /// + /// Peek a UInt24 from the stream + /// + /// Reads in machine native format + /// Only works properly on seekable streams + public static UInt24 PeekUInt24(this Stream stream) + { + if (BitConverter.IsLittleEndian) + return stream.PeekUInt24LittleEndian(); + else + return stream.PeekUInt24BigEndian(); + } + + /// + /// Peek a UInt24 from the stream + /// + /// Reads in big-endian format + /// Only works properly on seekable streams + public static UInt24 PeekUInt24BigEndian(this Stream stream) + { + UInt24 value = stream.ReadUInt24BigEndian(); + stream.Seek(-3, SeekOrigin.Current); + return value; + } + + /// + /// Peek a UInt24 from the stream + /// + /// Reads in little-endian format + /// Only works properly on seekable streams + public static UInt24 PeekUInt24LittleEndian(this Stream stream) + { + UInt24 value = stream.ReadUInt24LittleEndian(); + stream.Seek(-3, SeekOrigin.Current); + return value; + } + + /// + /// Peek an Int32 from the stream + /// + /// Reads in machine native format + /// Only works properly on seekable streams + public static int PeekInt32(this Stream stream) + { + if (BitConverter.IsLittleEndian) + return stream.PeekInt32LittleEndian(); + else + return stream.PeekInt32BigEndian(); + } + + /// + /// Peek an Int32 from the stream + /// + /// Reads in big-endian format + /// Only works properly on seekable streams + public static int PeekInt32BigEndian(this Stream stream) + { + int value = stream.ReadInt32BigEndian(); + stream.Seek(-4, SeekOrigin.Current); + return value; + } + + /// + /// Peek an Int32 from the stream + /// + /// Reads in little-endian format + /// Only works properly on seekable streams + public static int PeekInt32LittleEndian(this Stream stream) + { + int value = stream.ReadInt32LittleEndian(); + stream.Seek(-4, SeekOrigin.Current); + return value; + } + + /// + /// Peek a Int32 from the stream + /// + /// Reads in both-endian format + /// Only works properly on seekable streams + public static BothInt32 PeekInt32BothEndian(this Stream stream) + { + BothInt32 value = stream.ReadInt32BothEndian(); + stream.Seek(-8, SeekOrigin.Current); + return value; + } + + /// + /// Peek a UInt32 from the stream + /// + /// Reads in machine native format + /// Only works properly on seekable streams + public static uint PeekUInt32(this Stream stream) + { + if (BitConverter.IsLittleEndian) + return stream.PeekUInt32LittleEndian(); + else + return stream.PeekUInt32BigEndian(); + } + + /// + /// Peek a UInt32 from the stream + /// + /// Reads in big-endian format + /// Only works properly on seekable streams + public static uint PeekUInt32BigEndian(this Stream stream) + { + uint value = stream.ReadUInt32BigEndian(); + stream.Seek(-4, SeekOrigin.Current); + return value; + } + + /// + /// Peek a UInt32 from the stream + /// + /// Reads in little-endian format + /// Only works properly on seekable streams + public static uint PeekUInt32LittleEndian(this Stream stream) + { + uint value = stream.ReadUInt32LittleEndian(); + stream.Seek(-4, SeekOrigin.Current); + return value; + } + + /// + /// Peek a UInt32 from the stream + /// + /// Reads in both-endian format + /// Only works properly on seekable streams + public static BothUInt32 PeekUInt32BothEndian(this Stream stream) + { + BothUInt32 value = stream.ReadUInt32BothEndian(); + stream.Seek(-8, SeekOrigin.Current); + return value; + } + + /// + /// Peek a DWORD (4-byte) from the stream + /// + /// Reads in machine native format + /// Only works properly on seekable streams + public static uint PeekDWORD(this Stream stream) + => stream.PeekUInt32(); + + /// + /// Peek a DWORD (4-byte) from the stream + /// + /// Reads in big-endian format + /// Only works properly on seekable streams + public static uint PeekDWORDBigEndian(this Stream stream) + => stream.PeekUInt32BigEndian(); + + /// + /// Peek a DWORD (4-byte) from the stream + /// + /// Reads in little-endian format + /// Only works properly on seekable streams + public static uint PeekDWORDLittleEndian(this Stream stream) + => stream.PeekUInt32LittleEndian(); + + /// + /// Peek a DWORD (4-byte) from the stream + /// + /// Reads in little-endian format + /// Only works properly on seekable streams + public static BothUInt32 PeekDWORDBothEndian(this Stream stream) + => stream.PeekUInt32BothEndian(); + + /// + /// Peek a Single from the stream + /// + /// Reads in machine native format + /// Only works properly on seekable streams + public static float PeekSingle(this Stream stream) + { + if (BitConverter.IsLittleEndian) + return stream.PeekSingleLittleEndian(); + else + return stream.PeekSingleBigEndian(); + } + + /// + /// Peek a Single from the stream + /// + /// Reads in big-endian format + /// Only works properly on seekable streams + public static float PeekSingleBigEndian(this Stream stream) + { + float value = stream.ReadSingleBigEndian(); + stream.Seek(-4, SeekOrigin.Current); + return value; + } + + /// + /// Peek a Single from the stream + /// + /// Reads in little-endian format + /// Only works properly on seekable streams + public static float PeekSingleLittleEndian(this Stream stream) + { + float value = stream.ReadSingleLittleEndian(); + stream.Seek(-4, SeekOrigin.Current); + return value; + } + + /// + /// Peek an Int48 from the stream + /// + /// Reads in machine native format + /// Only works properly on seekable streams + public static Int48 PeekInt48(this Stream stream) + { + if (BitConverter.IsLittleEndian) + return stream.PeekInt48LittleEndian(); + else + return stream.PeekInt48BigEndian(); + } + + /// + /// Peek an Int48 from the stream + /// + /// Reads in big-endian format + /// Only works properly on seekable streams + public static Int48 PeekInt48BigEndian(this Stream stream) + { + Int48 value = stream.ReadInt48BigEndian(); + stream.Seek(-6, SeekOrigin.Current); + return value; + } + + /// + /// Peek an Int48 from the stream + /// + /// Reads in little-endian format + /// Only works properly on seekable streams + public static Int48 PeekInt48LittleEndian(this Stream stream) + { + Int48 value = stream.ReadInt48LittleEndian(); + stream.Seek(-6, SeekOrigin.Current); + return value; + } + + /// + /// Peek a UInt48 from the stream + /// + /// Reads in machine native format + /// Only works properly on seekable streams + public static UInt48 PeekUInt48(this Stream stream) + { + if (BitConverter.IsLittleEndian) + return stream.PeekUInt48LittleEndian(); + else + return stream.PeekUInt48BigEndian(); + } + + /// + /// Peek an UInt48 from the stream + /// + /// Reads in big-endian format + /// Only works properly on seekable streams + public static UInt48 PeekUInt48BigEndian(this Stream stream) + { + UInt48 value = stream.ReadUInt48BigEndian(); + stream.Seek(-6, SeekOrigin.Current); + return value; + } + + /// + /// Peek an UInt48 from the stream + /// + /// Reads in little-endian format + /// Only works properly on seekable streams + public static UInt48 PeekUInt48LittleEndian(this Stream stream) + { + UInt48 value = stream.ReadUInt48LittleEndian(); + stream.Seek(-6, SeekOrigin.Current); + return value; + } + + /// + /// Peek an Int64 from the stream + /// + /// Reads in machine native format + /// Only works properly on seekable streams + public static long PeekInt64(this Stream stream) + { + if (BitConverter.IsLittleEndian) + return stream.PeekInt64LittleEndian(); + else + return stream.PeekInt64BigEndian(); + } + + /// + /// Peek an Int64 from the stream + /// + /// Reads in big-endian format + /// Only works properly on seekable streams + public static long PeekInt64BigEndian(this Stream stream) + { + long value = stream.ReadInt64BigEndian(); + stream.Seek(-8, SeekOrigin.Current); + return value; + } + + /// + /// Peek an Int64 from the stream + /// + /// Reads in big-endian format + /// Only works properly on seekable streams + public static long PeekInt64LittleEndian(this Stream stream) + { + long value = stream.ReadInt64LittleEndian(); + stream.Seek(-8, SeekOrigin.Current); + return value; + } + + /// + /// Peek a Int64 from the stream + /// + /// Reads in both-endian format + /// Only works properly on seekable streams + public static BothInt64 PeekInt64BothEndian(this Stream stream) + { + BothInt64 value = stream.ReadInt64BothEndian(); + stream.Seek(-16, SeekOrigin.Current); + return value; + } + + /// + /// Peek a UInt64 from the stream + /// + /// Reads in machine native format + /// Only works properly on seekable streams + public static ulong PeekUInt64(this Stream stream) + { + if (BitConverter.IsLittleEndian) + return stream.PeekUInt64LittleEndian(); + else + return stream.PeekUInt64BigEndian(); + } + + /// + /// Peek a UInt64 from the stream + /// + /// Reads in big-endian format + /// Only works properly on seekable streams + public static ulong PeekUInt64BigEndian(this Stream stream) + { + ulong value = stream.ReadUInt64BigEndian(); + stream.Seek(-8, SeekOrigin.Current); + return value; + } + + /// + /// Peek a UInt64 from the stream + /// + /// Reads in little-endian format + /// Only works properly on seekable streams + public static ulong PeekUInt64LittleEndian(this Stream stream) + { + ulong value = stream.ReadUInt64LittleEndian(); + stream.Seek(-8, SeekOrigin.Current); + return value; + } + + /// + /// Peek a UInt64 from the stream + /// + /// Reads in both-endian format + /// Only works properly on seekable streams + public static BothUInt64 PeekUInt64BothEndian(this Stream stream) + { + BothUInt64 value = stream.ReadUInt64BothEndian(); + stream.Seek(-16, SeekOrigin.Current); + return value; + } + + /// + /// Peek a QWORD (8-byte) from the stream + /// + /// Reads in machine native format + /// Only works properly on seekable streams + public static ulong PeekQWORD(this Stream stream) + => stream.PeekUInt64(); + + /// + /// Peek a QWORD (8-byte) from the stream + /// + /// Reads in big-endian format + /// Only works properly on seekable streams + public static ulong PeekQWORDBigEndian(this Stream stream) + => stream.PeekUInt64BigEndian(); + + /// + /// Peek a QWORD (8-byte) from the stream + /// + /// Reads in little-endian format + /// Only works properly on seekable streams + public static ulong PeekQWORDLittleEndian(this Stream stream) + => stream.PeekUInt64LittleEndian(); + + /// + /// Peek a QWORD (8-byte) from the stream + /// + /// Reads in little-endian format + /// Only works properly on seekable streams + public static BothUInt64 PeekQWORDBothEndian(this Stream stream) + => stream.PeekUInt64BothEndian(); + + /// + /// Peek a Double from the stream + /// + /// Reads in machine native format + /// Only works properly on seekable streams + public static double PeekDouble(this Stream stream) + { + if (BitConverter.IsLittleEndian) + return stream.PeekDoubleLittleEndian(); + else + return stream.PeekDoubleBigEndian(); + } + + /// + /// Peek a Double from the stream + /// + /// Reads in big-endian format + /// Only works properly on seekable streams + public static double PeekDoubleBigEndian(this Stream stream) + { + double value = stream.ReadDoubleBigEndian(); + stream.Seek(-8, SeekOrigin.Current); + return value; + } + + /// + /// Peek a Double from the stream + /// + /// Reads in little-endian format + /// Only works properly on seekable streams + public static double PeekDoubleLittleEndian(this Stream stream) + { + double value = stream.ReadDoubleLittleEndian(); + stream.Seek(-8, SeekOrigin.Current); + return value; + } + + /// + /// Peek a Guid from the stream + /// + /// Reads in machine native format + /// Only works properly on seekable streams + public static Guid PeekGuid(this Stream stream) + { + if (BitConverter.IsLittleEndian) + return stream.PeekGuidLittleEndian(); + else + return stream.PeekGuidBigEndian(); + } + + /// + /// Peek a Guid from the stream + /// + /// Reads in big-endian format + /// Only works properly on seekable streams + public static Guid PeekGuidBigEndian(this Stream stream) + { + Guid value = stream.ReadGuidBigEndian(); + stream.Seek(-16, SeekOrigin.Current); + return value; + } + + /// + /// Peek a Guid from the stream + /// + /// Reads in little-endian format + /// Only works properly on seekable streams + public static Guid PeekGuidLittleEndian(this Stream stream) + { + Guid value = stream.ReadGuidLittleEndian(); + stream.Seek(-16, SeekOrigin.Current); + return value; + } + +#if NET7_0_OR_GREATER + /// + /// Peek an Int128 from the stream + /// + /// Reads in machine native format + /// Only works properly on seekable streams + public static Int128 PeekInt128(this Stream stream) + { + if (BitConverter.IsLittleEndian) + return stream.PeekInt128LittleEndian(); + else + return stream.PeekInt128BigEndian(); + } + + /// + /// Peek an Int128 from the stream + /// + /// Reads in big-endian format + /// Only works properly on seekable streams + public static Int128 PeekInt128BigEndian(this Stream stream) + { + Int128 value = stream.ReadInt128BigEndian(); + stream.Seek(-16, SeekOrigin.Current); + return value; + } + + /// + /// Peek an Int128 from the stream + /// + /// Reads in little-endian format + /// Only works properly on seekable streams + public static Int128 PeekInt128LittleEndian(this Stream stream) + { + Int128 value = stream.ReadInt128LittleEndian(); + stream.Seek(-16, SeekOrigin.Current); + return value; + } + + /// + /// Peek a UInt128 from the stream + /// + /// Reads in machine native format + /// Only works properly on seekable streams + public static UInt128 PeekUInt128(this Stream stream) + { + if (BitConverter.IsLittleEndian) + return stream.PeekUInt128LittleEndian(); + else + return stream.PeekUInt128BigEndian(); + } + + /// + /// Peek a UInt128 from the stream + /// + /// Reads in big-endian format + /// Only works properly on seekable streams + public static UInt128 PeekUInt128BigEndian(this Stream stream) + { + UInt128 value = stream.ReadUInt128BigEndian(); + stream.Seek(-16, SeekOrigin.Current); + return value; + } + + /// + /// Peek a UInt128 from the stream + /// + /// Reads in little-endian format + /// Only works properly on seekable streams + public static UInt128 PeekUInt128LittleEndian(this Stream stream) + { + UInt128 value = stream.ReadUInt128LittleEndian(); + stream.Seek(-16, SeekOrigin.Current); + return value; + } +#endif + + /// + /// Peek a Decimal from the stream + /// + /// Reads in machine native format + /// Only works properly on seekable streams + public static decimal PeekDecimal(this Stream stream) + { + if (BitConverter.IsLittleEndian) + return stream.PeekDecimalLittleEndian(); + else + return stream.PeekDecimalBigEndian(); + } + + /// + /// Peek a Decimal from the stream + /// + /// Reads in big-endian format + /// Only works properly on seekable streams + public static decimal PeekDecimalBigEndian(this Stream stream) + { + decimal value = stream.ReadDecimalBigEndian(); + stream.Seek(-16, SeekOrigin.Current); + return value; + } + + /// + /// Peek a Decimal from the stream + /// + /// Reads in little-endian format + /// Only works properly on seekable streams + public static decimal PeekDecimalLittleEndian(this Stream stream) + { + decimal value = stream.ReadDecimalLittleEndian(); + stream.Seek(-16, SeekOrigin.Current); + return value; + } + + #endregion + + #region Try Read + + /// + /// Read a UInt8 from the stream + /// + public static bool TryReadByteValue(this Stream stream, out byte value) + { + if (stream.Position > stream.Length - 1) + { + value = default; + return false; + } + + value = stream.ReadByteValue(); + return true; + } + + /// + /// Read a UInt8 from the stream + /// + /// Reads in both-endian format + public static bool TryReadByteBothEndian(this Stream stream, out BothUInt8 value) + { + if (stream.Position > stream.Length - 2) + { + value = default(byte); + return false; + } + + value = stream.ReadByteBothEndian(); + return true; + } + + /// + /// Read a UInt8[] from the stream + /// + public static bool TryReadBytes(this Stream stream, int count, out byte[] value) + { + if (stream.Position > stream.Length - count) + { + value = []; + return false; + } + + value = stream.ReadBytes(count); + return true; + } + + /// + /// Read an Int8 from the stream + /// + public static bool TryReadSByte(this Stream stream, out sbyte value) + { + if (stream.Position > stream.Length - 1) + { + value = default; + return false; + } + + value = stream.ReadSByte(); + return true; + } + + /// + /// Read a Int8 from the stream + /// + /// Reads in both-endian format + public static bool TryReadSByteBothEndian(this Stream stream, out BothInt8 value) + { + if (stream.Position > stream.Length - 2) + { + value = default(sbyte); + return false; + } + + value = stream.ReadSByteBothEndian(); + return true; + } + + /// + /// Read a Char from the stream + /// + public static bool TryReadChar(this Stream stream, out char value) + { + if (stream.Position > stream.Length - 1) + { + value = default; + return false; + } + + value = stream.ReadChar(); + return true; + } + + /// + /// Read an Int16 from the stream + /// + /// Reads in machine native format + public static bool TryReadInt16(this Stream stream, out short value) + { + if (BitConverter.IsLittleEndian) + return stream.TryReadInt16LittleEndian(out value); + else + return stream.TryReadInt16BigEndian(out value); + } + + /// + /// Read an Int16 from the stream + /// + /// Reads in big-endian format + public static bool TryReadInt16BigEndian(this Stream stream, out short value) + { + if (stream.Position > stream.Length - 2) + { + value = default; + return false; + } + + value = stream.ReadInt16BigEndian(); + return true; + } + + /// + /// Read an Int16 from the stream + /// + /// Reads in little-endian format + public static bool TryReadInt16LittleEndian(this Stream stream, out short value) + { + if (stream.Position > stream.Length - 2) + { + value = default; + return false; + } + + value = stream.ReadInt16LittleEndian(); + return true; + } + + /// + /// Read a Int16 from the stream + /// + /// Reads in both-endian format + public static bool TryReadInt16BothEndian(this Stream stream, out BothInt16 value) + { + if (stream.Position > stream.Length - 4) + { + value = default(short); + return false; + } + + value = stream.ReadInt16BothEndian(); + return true; + } + + /// + /// Read a UInt16 from the stream + /// + /// Reads in machine native format + public static bool TryReadUInt16(this Stream stream, out ushort value) + { + if (BitConverter.IsLittleEndian) + return stream.TryReadUInt16LittleEndian(out value); + else + return stream.TryReadUInt16BigEndian(out value); + } + + /// + /// Read a UInt16 from the stream + /// + /// Reads in big-endian format + public static bool TryReadUInt16BigEndian(this Stream stream, out ushort value) + { + if (stream.Position > stream.Length - 2) + { + value = default; + return false; + } + + value = stream.ReadUInt16BigEndian(); + return true; + } + + /// + /// Read a UInt16 from the stream + /// + /// Reads in little-endian format + public static bool TryReadUInt16LittleEndian(this Stream stream, out ushort value) + { + if (stream.Position > stream.Length - 2) + { + value = default; + return false; + } + + value = stream.ReadUInt16LittleEndian(); + return true; + } + + /// + /// Read a UInt16 from the stream + /// + /// Reads in both-endian format + public static bool TryReadUInt16BothEndian(this Stream stream, out BothUInt16 value) + { + if (stream.Position > stream.Length - 4) + { + value = default(ushort); + return false; + } + + value = stream.ReadUInt16BothEndian(); + return true; + } + + /// + /// Read a WORD (2-byte) from the stream + /// + /// Reads in machine native format + public static bool TryReadWORD(this Stream stream, out ushort value) + => stream.TryReadUInt16(out value); + + /// + /// Read a WORD (2-byte) from the stream + /// + /// Reads in big-endian format + public static bool TryReadWORDBigEndian(this Stream stream, out ushort value) + => stream.TryReadUInt16BigEndian(out value); + + /// + /// Read a WORD (2-byte) from the stream + /// + /// Reads in little-endian format + public static bool TryReadWORDLittleEndian(this Stream stream, out ushort value) + => stream.TryReadUInt16LittleEndian(out value); + + /// + /// Read a WORD (2-byte) from the stream + /// + /// Reads in both-endian format + public static bool TryReadWORDBothEndian(this Stream stream, out BothUInt16 value) + => stream.TryReadUInt16BothEndian(out value); + +#if NET5_0_OR_GREATER + /// + /// Read a Half from the stream + /// + /// Reads in machine native format + public static bool TryReadHalf(this Stream stream, out Half value) + { + if (BitConverter.IsLittleEndian) + return stream.TryReadHalfLittleEndian(out value); + else + return stream.TryReadHalfBigEndian(out value); + } + + /// + /// Read a Half from the stream + /// + /// Reads in big-endian format + public static bool TryReadHalfBigEndian(this Stream stream, out Half value) + { + if (stream.Position > stream.Length - 2) + { + value = default; + return false; + } + + value = stream.ReadHalfBigEndian(); + return true; + } + + /// + /// Read a Half from the stream + /// + /// Reads in little-endian format + public static bool TryReadHalfLittleEndian(this Stream stream, out Half value) + { + if (stream.Position > stream.Length - 2) + { + value = default; + return false; + } + + value = stream.ReadHalfLittleEndian(); + return true; + } +#endif + + /// + /// Read an Int24 from the stream + /// + /// Reads in machine native format + public static bool TryReadInt24(this Stream stream, out Int24 value) + { + if (BitConverter.IsLittleEndian) + return stream.TryReadInt24LittleEndian(out value); + else + return stream.TryReadInt24BigEndian(out value); + } + + /// + /// Read an Int24 from the stream + /// + /// Reads in big-endian format + public static bool TryReadInt24BigEndian(this Stream stream, out Int24 value) + { + if (stream.Position > stream.Length - 3) + { + value = new Int24(); + return false; + } + + value = stream.ReadInt24BigEndian(); + return true; + } + + /// + /// Read an Int24 from the stream + /// + /// Reads in little-endian format + public static bool TryReadInt24LittleEndian(this Stream stream, out Int24 value) + { + if (stream.Position > stream.Length - 3) + { + value = new Int24(); + return false; + } + + value = stream.ReadInt24LittleEndian(); + return true; + } + + /// + /// Read a UInt24 from the stream + /// + /// Reads in machine native format + public static bool TryReadUInt24(this Stream stream, out UInt24 value) + { + if (BitConverter.IsLittleEndian) + return stream.TryReadUInt24LittleEndian(out value); + else + return stream.TryReadUInt24BigEndian(out value); + } + + /// + /// Read a UInt24 from the stream + /// + /// Reads in big-endian format + public static bool TryReadUInt24BigEndian(this Stream stream, out UInt24 value) + { + if (stream.Position > stream.Length - 3) + { + value = new UInt24(); + return false; + } + + value = stream.ReadUInt24BigEndian(); + return true; + } + + /// + /// Read a UInt24 from the stream + /// + /// Reads in little-endian format + public static bool TryReadUInt24LittleEndian(this Stream stream, out UInt24 value) + { + if (stream.Position > stream.Length - 3) + { + value = new UInt24(); + return false; + } + + value = stream.ReadUInt24LittleEndian(); + return true; + } + + /// + /// Read an Int32 from the stream + /// + /// Reads in machine native format + public static bool TryReadInt32(this Stream stream, out int value) + { + if (BitConverter.IsLittleEndian) + return stream.TryReadInt32LittleEndian(out value); + else + return stream.TryReadInt32BigEndian(out value); + } + + /// + /// Read an Int32 from the stream + /// + /// Reads in big-endian format + public static bool TryReadInt32BigEndian(this Stream stream, out int value) + { + if (stream.Position > stream.Length - 4) + { + value = default; + return false; + } + + value = stream.ReadInt32BigEndian(); + return true; + } + + /// + /// Read an Int32 from the stream + /// + /// Reads in little-endian format + public static bool TryReadInt32LittleEndian(this Stream stream, out int value) + { + if (stream.Position > stream.Length - 4) + { + value = default; + return false; + } + + value = stream.ReadInt32LittleEndian(); + return true; + } + + /// + /// Read a Int32 from the stream + /// + /// Reads in both-endian format + public static bool TryReadInt32BothEndian(this Stream stream, out BothInt32 value) + { + if (stream.Position > stream.Length - 8) + { + value = default(int); + return false; + } + + value = stream.ReadInt32BothEndian(); + return true; + } + + /// + /// Read a UInt32 from the stream + /// + /// Reads in machine native format + public static bool TryReadUInt32(this Stream stream, out uint value) + { + if (BitConverter.IsLittleEndian) + return stream.TryReadUInt32LittleEndian(out value); + else + return stream.TryReadUInt32BigEndian(out value); + } + + /// + /// Read a UInt32 from the stream + /// + /// Reads in big-endian format + public static bool TryReadUInt32BigEndian(this Stream stream, out uint value) + { + if (stream.Position > stream.Length - 4) + { + value = default; + return false; + } + + value = stream.ReadUInt32BigEndian(); + return true; + } + + /// + /// Read a UInt32 from the stream + /// + /// Reads in little-endian format + public static bool TryReadUInt32LittleEndian(this Stream stream, out uint value) + { + if (stream.Position > stream.Length - 4) + { + value = default; + return false; + } + + value = stream.ReadUInt32LittleEndian(); + return true; + } + + /// + /// Read a UInt32 from the stream + /// + /// Reads in both-endian format + public static bool TryReadUInt32BothEndian(this Stream stream, out BothUInt32 value) + { + if (stream.Position > stream.Length - 8) + { + value = default(uint); + return false; + } + + value = stream.ReadUInt32BothEndian(); + return true; + } + + /// + /// Read a DWORD (4-byte) from the stream + /// + /// Reads in machine native format + public static bool TryReadDWORD(this Stream stream, out uint value) + => stream.TryReadUInt32(out value); + + /// + /// Read a DWORD (4-byte) from the stream + /// + /// Reads in big-endian format + public static bool TryReadDWORDBigEndian(this Stream stream, out uint value) + => stream.TryReadUInt32BigEndian(out value); + + /// + /// Read a DWORD (4-byte) from the stream + /// + /// Reads in little-endian format + public static bool TryReadDWORDLittleEndian(this Stream stream, out uint value) + => stream.TryReadUInt32LittleEndian(out value); + + /// + /// Read a DWORD (4-byte) from the stream + /// + /// Reads in both-endian format + public static bool TryReadDWORDBothEndian(this Stream stream, out BothUInt32 value) + => stream.TryReadUInt32BothEndian(out value); + + /// + /// Read a Single from the stream + /// + /// Reads in machine native format + public static bool TryReadSingle(this Stream stream, out float value) + { + if (BitConverter.IsLittleEndian) + return stream.TryReadSingleLittleEndian(out value); + else + return stream.TryReadSingleBigEndian(out value); + } + + /// + /// Read a Single from the stream + /// + /// Reads in big-endian format + public static bool TryReadSingleBigEndian(this Stream stream, out float value) + { + if (stream.Position > stream.Length - 4) + { + value = default; + return false; + } + + value = stream.ReadSingleBigEndian(); + return true; + } + + /// + /// Read a Single from the stream + /// + /// Reads in little-endian format + public static bool TryReadSingleLittleEndian(this Stream stream, out float value) + { + if (stream.Position > stream.Length - 4) + { + value = default; + return false; + } + + value = stream.ReadSingleLittleEndian(); + return true; + } + + /// + /// Read an Int48 from the stream + /// + /// Reads in machine native format + public static bool TryReadInt48(this Stream stream, out Int48 value) + { + if (BitConverter.IsLittleEndian) + return stream.TryReadInt48LittleEndian(out value); + else + return stream.TryReadInt48BigEndian(out value); + } + + /// + /// Read an Int48 from the stream + /// + /// Reads in big-endian format + public static bool TryReadInt48BigEndian(this Stream stream, out Int48 value) + { + if (stream.Position > stream.Length - 6) + { + value = new Int48(); + return false; + } + + value = stream.ReadInt48BigEndian(); + return true; + } + + /// + /// Read an Int48 from the stream + /// + /// Reads in little-endian format + public static bool TryReadInt48LittleEndian(this Stream stream, out Int48 value) + { + if (stream.Position > stream.Length - 6) + { + value = new Int48(); + return false; + } + + value = stream.ReadInt48LittleEndian(); + return true; + } + + /// + /// Read a UInt48 from the stream + /// + /// Reads in machine native format + public static bool TryReadUInt48(this Stream stream, out UInt48 value) + { + if (BitConverter.IsLittleEndian) + return stream.TryReadUInt48LittleEndian(out value); + else + return stream.TryReadUInt48BigEndian(out value); + } + + /// + /// Read a UInt48 from the stream + /// + /// Reads in big-endian format + public static bool TryReadUInt48BigEndian(this Stream stream, out UInt48 value) + { + if (stream.Position > stream.Length - 6) + { + value = new UInt48(); + return false; + } + + value = stream.ReadUInt48BigEndian(); + return true; + } + + /// + /// Read an UInt48 from the stream + /// + /// Reads in little-endian format + public static bool TryReadUInt48LittleEndian(this Stream stream, out UInt48 value) + { + if (stream.Position > stream.Length - 6) + { + value = new UInt48(); + return false; + } + + value = stream.ReadUInt48LittleEndian(); + return true; + } + + /// + /// Read an Int64 from the stream + /// + /// Reads in machine native format + public static bool TryReadInt64(this Stream stream, out long value) + { + if (BitConverter.IsLittleEndian) + return stream.TryReadInt64LittleEndian(out value); + else + return stream.TryReadInt64BigEndian(out value); + } + + /// + /// Read an Int64 from the stream + /// + /// Reads in big-endian format + public static bool TryReadInt64BigEndian(this Stream stream, out long value) + { + if (stream.Position > stream.Length - 8) + { + value = default; + return false; + } + + value = stream.ReadInt64BigEndian(); + return true; + } + + /// + /// Read an Int64 from the stream + /// + /// Reads in little-endian format + public static bool TryReadInt64LittleEndian(this Stream stream, out long value) + { + if (stream.Position > stream.Length - 8) + { + value = default; + return false; + } + + value = stream.ReadInt64BigEndian(); + return true; + } + + /// + /// Read a Int64 from the stream + /// + /// Reads in both-endian format + public static bool TryReadInt64BothEndian(this Stream stream, out BothInt64 value) + { + if (stream.Position > stream.Length - 16) + { + value = default(long); + return false; + } + + value = stream.ReadInt64BothEndian(); + return true; + } + + /// + /// Read a UInt64 from the stream + /// + /// Reads in machine native format + public static bool TryReadUInt64(this Stream stream, out ulong value) + { + if (BitConverter.IsLittleEndian) + return stream.TryReadUInt64LittleEndian(out value); + else + return stream.TryReadUInt64BigEndian(out value); + } + + /// + /// Read a UInt64 from the stream + /// + /// Reads in big-endian format + public static bool TryReadUInt64BigEndian(this Stream stream, out ulong value) + { + if (stream.Position > stream.Length - 8) + { + value = default; + return false; + } + + value = stream.ReadUInt64BigEndian(); + return true; + } + + /// + /// Read a UInt64 from the stream + /// + /// Reads in little-endian format + public static bool TryReadUInt64LittleEndian(this Stream stream, out ulong value) + { + if (stream.Position > stream.Length - 8) + { + value = default; + return false; + } + + value = stream.ReadUInt64LittleEndian(); + return true; + } + + /// + /// Read a UInt64 from the stream + /// + /// Reads in both-endian format + public static bool TryReadUInt64BothEndian(this Stream stream, out BothUInt64 value) + { + if (stream.Position > stream.Length - 16) + { + value = default(ulong); + return false; + } + + value = stream.ReadUInt64BothEndian(); + return true; + } + + /// + /// Read a QWORD (8-byte) from the stream + /// + /// Reads in machine native format + public static bool TryReadQWORD(this Stream stream, out ulong value) + => stream.TryReadUInt64(out value); + + /// + /// Read a QWORD (8-byte) from the stream + /// + /// Reads in big-endian format + public static bool TryReadQWORDBigEndian(this Stream stream, out ulong value) + => stream.TryReadUInt64BigEndian(out value); + + /// + /// Read a QWORD (8-byte) from the stream + /// + /// Reads in little-endian format + public static bool TryReadQWORDLittleEndian(this Stream stream, out ulong value) + => stream.TryReadUInt64LittleEndian(out value); + + /// + /// Read a QWORD (8-byte) from the stream + /// + /// Reads in both-endian format + public static bool TryReadQWORDBothEndian(this Stream stream, out BothUInt64 value) + => stream.TryReadUInt64BothEndian(out value); + + /// + /// Read a Double from the stream + /// + /// Reads in machine native format + public static bool TryReadDouble(this Stream stream, out double value) + { + if (BitConverter.IsLittleEndian) + return stream.TryReadDoubleLittleEndian(out value); + else + return stream.TryReadDoubleBigEndian(out value); + } + + /// + /// Read a Double from the stream + /// + /// Reads in big-endian format + public static bool TryReadDoubleBigEndian(this Stream stream, out double value) + { + if (stream.Position > stream.Length - 8) + { + value = default; + return false; + } + + value = stream.ReadDoubleBigEndian(); + return true; + } + + /// + /// Read a Double from the stream + /// + /// Reads in little-endian format + public static bool TryReadDoubleLittleEndian(this Stream stream, out double value) + { + if (stream.Position > stream.Length - 8) + { + value = default; + return false; + } + + value = stream.ReadDoubleLittleEndian(); + return true; + } + + /// + /// Read a Guid from the stream + /// + /// Reads in machine native format + public static bool TryReadGuid(this Stream stream, out Guid value) + { + if (BitConverter.IsLittleEndian) + return stream.TryReadGuidLittleEndian(out value); + else + return stream.TryReadGuidBigEndian(out value); + } + + /// + /// Read a Guid from the stream + /// + /// Reads in big-endian format + public static bool TryReadGuidBigEndian(this Stream stream, out Guid value) + { + if (stream.Position > stream.Length - 16) + { + value = default; + return false; + } + + value = stream.ReadGuidBigEndian(); + return true; + } + + /// + /// Read a Guid from the stream + /// + /// Reads in little-endian format + public static bool TryReadGuidLittleEndian(this Stream stream, out Guid value) + { + if (stream.Position > stream.Length - 16) + { + value = default; + return false; + } + + value = stream.ReadGuidLittleEndian(); + return true; + } + +#if NET7_0_OR_GREATER + /// + /// Read an Int128 from the stream + /// + /// Reads in machine native format + public static bool TryReadInt128(this Stream stream, out Int128 value) + { + if (BitConverter.IsLittleEndian) + return stream.TryReadInt128LittleEndian(out value); + else + return stream.TryReadInt128BigEndian(out value); + } + + /// + /// Read an Int128 from the stream + /// + /// Reads in big-endian format + public static bool TryReadInt128BigEndian(this Stream stream, out Int128 value) + { + if (stream.Position > stream.Length - 16) + { + value = default; + return false; + } + + value = stream.ReadInt128BigEndian(); + return true; + } + + /// + /// Read an Int128 from the stream + /// + /// Reads in little-endian format + public static bool TryReadInt128LittleEndian(this Stream stream, out Int128 value) + { + if (stream.Position > stream.Length - 16) + { + value = default; + return false; + } + + value = stream.ReadInt128LittleEndian(); + return true; + } + + /// + /// Read a UInt128 from the stream + /// + /// Reads in machine native format + public static bool TryReadUInt128(this Stream stream, out UInt128 value) + { + if (BitConverter.IsLittleEndian) + return stream.TryReadUInt128LittleEndian(out value); + else + return stream.TryReadUInt128BigEndian(out value); + } + + /// + /// Read a UInt128 from the stream + /// + /// Reads in big-endian format + public static bool TryReadUInt128BigEndian(this Stream stream, out UInt128 value) + { + if (stream.Position > stream.Length - 16) + { + value = default; + return false; + } + + value = stream.ReadUInt128BigEndian(); + return true; + } + + /// + /// Read a UInt128 from the stream + /// + /// Reads in little-endian format + public static bool TryReadUInt128LittleEndian(this Stream stream, out UInt128 value) + { + if (stream.Position > stream.Length - 16) + { + value = default; + return false; + } + + value = stream.ReadUInt128LittleEndian(); + return true; + } +#endif + + /// + /// Read a Decimal from the stream + /// + /// Reads in machine native format + public static bool TryReadDecimal(this Stream stream, out decimal value) + { + if (BitConverter.IsLittleEndian) + return stream.TryReadDecimalLittleEndian(out value); + else + return stream.TryReadDecimalBigEndian(out value); + } + + /// + /// Read a Decimal from the stream + /// + /// Reads in big-endian format + public static bool TryReadDecimalBigEndian(this Stream stream, out decimal value) + { + if (stream.Position > stream.Length - 16) + { + value = default; + return false; + } + + value = stream.ReadDecimalBigEndian(); + return true; + } + + /// + /// Read a Decimal from the stream + /// + /// Reads in little-endian format + public static bool TryReadDecimalLittleEndian(this Stream stream, out decimal value) + { + if (stream.Position > stream.Length - 16) + { + value = default; + return false; + } + + value = stream.ReadDecimalLittleEndian(); + return true; + } + + #endregion + } +} diff --git a/SabreTools.Numerics.Extensions/StreamWriterExtensions.cs b/SabreTools.Numerics.Extensions/StreamWriterExtensions.cs new file mode 100644 index 0000000..9a4a115 --- /dev/null +++ b/SabreTools.Numerics.Extensions/StreamWriterExtensions.cs @@ -0,0 +1,711 @@ +using System; +using System.IO; +using System.Text; + +namespace SabreTools.Numerics.Extensions +{ + /// + /// Extensions for Streams + /// + public static class StreamWriterExtensions + { + /// + /// Write a UInt8 + /// + public static bool Write(this Stream stream, byte value) + => WriteFromBuffer(stream, [value]); + + /// + /// Write a UInt8 + /// + /// Writes in both-endian format + public static bool WriteBothEndian(this Stream stream, BothUInt8 value) + { + bool actual = stream.Write(value.LittleEndian); + actual &= stream.Write(value.BigEndian); + return actual; + } + + /// + /// Write a UInt8[] + /// + public static bool Write(this Stream stream, byte[] value) + => WriteFromBuffer(stream, value); + + /// + /// Write a UInt8[] + /// + /// Writes in big-endian format + public static bool WriteBigEndian(this Stream stream, byte[] value) + { + Array.Reverse(value); + return WriteFromBuffer(stream, value); + } + + /// + /// Write an Int8 + /// + public static bool Write(this Stream stream, sbyte value) + => WriteFromBuffer(stream, [(byte)value]); + + /// + /// Write a Int8 + /// + /// Writes in both-endian format + public static bool WriteBothEndian(this Stream stream, BothInt8 value) + { + bool actual = stream.Write(value.LittleEndian); + actual &= stream.Write(value.BigEndian); + return actual; + } + + /// + /// Write a Char + /// + public static bool Write(this Stream stream, char value) + { + byte[] buffer = BitConverter.GetBytes(value); + return WriteFromBuffer(stream, buffer); + } + + /// + /// Write a Char with an Encoding + /// + public static bool Write(this Stream stream, char value, Encoding encoding) + { + byte[] buffer = encoding.GetBytes($"{value}"); + return WriteFromBuffer(stream, buffer); + } + + /// + /// Write an Int16 + /// + /// Writes in machine native format + public static bool Write(this Stream stream, short value) + { + if (BitConverter.IsLittleEndian) + return stream.WriteLittleEndian(value); + else + return stream.WriteBigEndian(value); + } + + /// + /// Write an Int16 + /// + /// Writes in big-endian format + public static bool WriteBigEndian(this Stream stream, short value) + { + byte[] buffer = value.GetBytesBigEndian(); + return WriteFromBuffer(stream, buffer); + } + + /// + /// Write an Int16 + /// + /// Writes in little-endian format + public static bool WriteLittleEndian(this Stream stream, short value) + { + byte[] buffer = value.GetBytesLittleEndian(); + return WriteFromBuffer(stream, buffer); + } + + /// + /// Write a Int16 + /// + /// Writes in both-endian format + public static bool WriteBothEndian(this Stream stream, BothInt16 value) + { + bool actual = stream.WriteLittleEndian(value.LittleEndian); + actual &= stream.WriteBigEndian(value.BigEndian); + return actual; + } + + /// + /// Write a UInt16 + /// + /// Writes in machine native format + public static bool Write(this Stream stream, ushort value) + { + if (BitConverter.IsLittleEndian) + return stream.WriteLittleEndian(value); + else + return stream.WriteBigEndian(value); + } + + /// + /// Write a UInt16 + /// + /// Writes in big-endian format + public static bool WriteBigEndian(this Stream stream, ushort value) + { + byte[] buffer = value.GetBytesBigEndian(); + return WriteFromBuffer(stream, buffer); + } + + /// + /// Write a UInt16 + /// + /// Writes in little-endian format + public static bool WriteLittleEndian(this Stream stream, ushort value) + { + byte[] buffer = value.GetBytesLittleEndian(); + return WriteFromBuffer(stream, buffer); + } + + /// + /// Write a UInt16 + /// + /// Writes in both-endian format + public static bool WriteBothEndian(this Stream stream, BothUInt16 value) + { + bool actual = stream.WriteLittleEndian(value.LittleEndian); + actual &= stream.WriteBigEndian(value.BigEndian); + return actual; + } + +#if NET5_0_OR_GREATER + /// + /// Write a Half + /// + /// Writes in machine native format + public static bool Write(this Stream stream, Half value) + { + if (BitConverter.IsLittleEndian) + return stream.WriteLittleEndian(value); + else + return stream.WriteBigEndian(value); + } + + /// + /// Write a Half + /// + /// Writes in big-endian format + public static bool WriteBigEndian(this Stream stream, Half value) + { + byte[] buffer = value.GetBytesBigEndian(); + return WriteFromBuffer(stream, buffer); + } + + /// + /// Write a Half + /// + /// Writes in little-endian format + public static bool WriteLittleEndian(this Stream stream, Half value) + { + byte[] buffer = value.GetBytesLittleEndian(); + return WriteFromBuffer(stream, buffer); + } +#endif + + /// + /// Write an Int24 + /// + /// Writes in machine native format + public static bool Write(this Stream stream, Int24 value) + { + if (BitConverter.IsLittleEndian) + return stream.WriteLittleEndian(value); + else + return stream.WriteBigEndian(value); + } + + /// + /// Write an Int24 + /// + /// Writes in big-endian format + public static bool WriteBigEndian(this Stream stream, Int24 value) + { + byte[] buffer = value.GetBytesBigEndian(); + return WriteFromBuffer(stream, buffer); + } + + /// + /// Write an Int24 + /// + /// Writes in little-endian format + public static bool WriteLittleEndian(this Stream stream, Int24 value) + { + byte[] buffer = value.GetBytesLittleEndian(); + return WriteFromBuffer(stream, buffer); + } + + /// + /// Write a UInt24 + /// + /// Writes in machine native format + public static bool Write(this Stream stream, UInt24 value) + { + if (BitConverter.IsLittleEndian) + return stream.WriteLittleEndian(value); + else + return stream.WriteBigEndian(value); + } + + /// + /// Write a UInt24 + /// + /// Writes in big-endian format + public static bool WriteBigEndian(this Stream stream, UInt24 value) + { + byte[] buffer = value.GetBytesBigEndian(); + return WriteFromBuffer(stream, buffer); + } + + /// + /// Write a UInt24 + /// + /// Writes in little-endian format + public static bool WriteLittleEndian(this Stream stream, UInt24 value) + { + byte[] buffer = value.GetBytesLittleEndian(); + return WriteFromBuffer(stream, buffer); + } + + /// + /// Write an Int32 + /// + /// Writes in machine native format + public static bool Write(this Stream stream, int value) + { + if (BitConverter.IsLittleEndian) + return stream.WriteLittleEndian(value); + else + return stream.WriteBigEndian(value); + } + + /// + /// Write an Int32 + /// + /// Writes in big-endian format + public static bool WriteBigEndian(this Stream stream, int value) + { + byte[] buffer = value.GetBytesBigEndian(); + return WriteFromBuffer(stream, buffer); + } + + /// + /// Write an Int32 + /// + /// Writes in little-endian format + public static bool WriteLittleEndian(this Stream stream, int value) + { + byte[] buffer = value.GetBytesLittleEndian(); + return WriteFromBuffer(stream, buffer); + } + + /// + /// Write a Int32 + /// + /// Writes in both-endian format + public static bool WriteBothEndian(this Stream stream, BothInt32 value) + { + bool actual = stream.WriteLittleEndian(value.LittleEndian); + actual &= stream.WriteBigEndian(value.BigEndian); + return actual; + } + + /// + /// Write a UInt32 + /// + /// Writes in machine native format + public static bool Write(this Stream stream, uint value) + { + if (BitConverter.IsLittleEndian) + return stream.WriteLittleEndian(value); + else + return stream.WriteBigEndian(value); + } + + /// + /// Write a UInt32 + /// + /// Writes in big-endian format + public static bool WriteBigEndian(this Stream stream, uint value) + { + byte[] buffer = value.GetBytesBigEndian(); + return WriteFromBuffer(stream, buffer); + } + + /// + /// Write a UInt32 + /// + /// Writes in little-endian format + public static bool WriteLittleEndian(this Stream stream, uint value) + { + byte[] buffer = value.GetBytesLittleEndian(); + return WriteFromBuffer(stream, buffer); + } + + /// + /// Write a UInt32 + /// + /// Writes in both-endian format + public static bool WriteBothEndian(this Stream stream, BothUInt32 value) + { + bool actual = stream.WriteLittleEndian(value.LittleEndian); + actual &= stream.WriteBigEndian(value.BigEndian); + return actual; + } + + /// + /// Write a Single + /// + /// Writes in machine native format + public static bool Write(this Stream stream, float value) + { + if (BitConverter.IsLittleEndian) + return stream.WriteLittleEndian(value); + else + return stream.WriteBigEndian(value); + } + + /// + /// Write a Single + /// + /// Writes in big-endian format + public static bool WriteBigEndian(this Stream stream, float value) + { + byte[] buffer = value.GetBytesBigEndian(); + return WriteFromBuffer(stream, buffer); + } + + /// + /// Write a Single + /// + public static bool WriteLittleEndian(this Stream stream, float value) + { + byte[] buffer = value.GetBytesLittleEndian(); + return WriteFromBuffer(stream, buffer); + } + + /// + /// Write an Int48 + /// + /// Writes in machine native format + public static bool Write(this Stream stream, Int48 value) + { + if (BitConverter.IsLittleEndian) + return stream.WriteLittleEndian(value); + else + return stream.WriteBigEndian(value); + } + + /// + /// Write an Int48 + /// + /// Writes in big-endian format + public static bool WriteBigEndian(this Stream stream, Int48 value) + { + byte[] buffer = value.GetBytesBigEndian(); + return WriteFromBuffer(stream, buffer); + } + + /// + /// Write an Int48 + /// + /// Writes in little-endian format + public static bool WriteLittleEndian(this Stream stream, Int48 value) + { + byte[] buffer = value.GetBytesLittleEndian(); + return WriteFromBuffer(stream, buffer); + } + + /// + /// Write a UInt48 + /// + /// Writes in machine native format + public static bool Write(this Stream stream, UInt48 value) + { + if (BitConverter.IsLittleEndian) + return stream.WriteLittleEndian(value); + else + return stream.WriteBigEndian(value); + } + + /// + /// Write a UInt48 + /// + /// Writes in big-endian format + public static bool WriteBigEndian(this Stream stream, UInt48 value) + { + byte[] buffer = value.GetBytesBigEndian(); + return WriteFromBuffer(stream, buffer); + } + + /// + /// Write a UInt48 + /// + /// Writes in little-endian format + public static bool WriteLittleEndian(this Stream stream, UInt48 value) + { + byte[] buffer = value.GetBytesLittleEndian(); + return WriteFromBuffer(stream, buffer); + } + + /// + /// Write an Int64 + /// + /// Writes in machine native format + public static bool Write(this Stream stream, long value) + { + if (BitConverter.IsLittleEndian) + return stream.WriteLittleEndian(value); + else + return stream.WriteBigEndian(value); + } + + /// + /// Write an Int64 + /// + /// Writes in big-endian format + public static bool WriteBigEndian(this Stream stream, long value) + { + byte[] buffer = value.GetBytesBigEndian(); + return WriteFromBuffer(stream, buffer); + } + + /// + /// Write an Int64 + /// + /// Writes in little-endian format + public static bool WriteLittleEndian(this Stream stream, long value) + { + byte[] buffer = value.GetBytesLittleEndian(); + return WriteFromBuffer(stream, buffer); + } + + /// + /// Write a Int64 + /// + /// Writes in both-endian format + public static bool WriteBothEndian(this Stream stream, BothInt64 value) + { + bool actual = stream.WriteLittleEndian(value.LittleEndian); + actual &= stream.WriteBigEndian(value.BigEndian); + return actual; + } + + /// + /// Write a UInt64 + /// + /// Writes in machine native format + public static bool Write(this Stream stream, ulong value) + { + if (BitConverter.IsLittleEndian) + return stream.WriteLittleEndian(value); + else + return stream.WriteBigEndian(value); + } + + /// + /// Write a UInt64 + /// + /// Writes in big-endian format + public static bool WriteBigEndian(this Stream stream, ulong value) + { + byte[] buffer = value.GetBytesBigEndian(); + return WriteFromBuffer(stream, buffer); + } + + /// + /// Write a UInt64 + /// + /// Writes in little-endian format + public static bool WriteLittleEndian(this Stream stream, ulong value) + { + byte[] buffer = value.GetBytesLittleEndian(); + return WriteFromBuffer(stream, buffer); + } + + /// + /// Write a UInt64 + /// + /// Writes in both-endian format + public static bool WriteBothEndian(this Stream stream, BothUInt64 value) + { + bool actual = stream.WriteLittleEndian(value.LittleEndian); + actual &= stream.WriteBigEndian(value.BigEndian); + return actual; + } + + /// + /// Write a Double + /// + /// Writes in machine native format + public static bool Write(this Stream stream, double value) + { + if (BitConverter.IsLittleEndian) + return stream.WriteLittleEndian(value); + else + return stream.WriteBigEndian(value); + } + + /// + /// Write a Double + /// + /// Writes in big-endian format + public static bool WriteBigEndian(this Stream stream, double value) + { + byte[] buffer = value.GetBytesBigEndian(); + return WriteFromBuffer(stream, buffer); + } + + /// + /// Write a Double + /// + /// Writes in little-endian format + public static bool WriteLittleEndian(this Stream stream, double value) + { + byte[] buffer = value.GetBytesLittleEndian(); + return WriteFromBuffer(stream, buffer); + } + + /// + /// Write a Guid + /// + /// Writes in machine native format + public static bool Write(this Stream stream, Guid value) + { + if (BitConverter.IsLittleEndian) + return stream.WriteLittleEndian(value); + else + return stream.WriteBigEndian(value); + } + + /// + /// Write a Guid + /// + /// Writes in big-endian format + public static bool WriteBigEndian(this Stream stream, Guid value) + { + byte[] buffer = value.GetBytesBigEndian(); + return WriteFromBuffer(stream, buffer); + } + + /// + /// Write a Guid + /// + /// Writes in little-endian format + public static bool WriteLittleEndian(this Stream stream, Guid value) + { + byte[] buffer = value.GetBytesLittleEndian(); + return WriteFromBuffer(stream, buffer); + } + +#if NET7_0_OR_GREATER + /// + /// Write an Int128 + /// + /// Writes in machine native format + public static bool Write(this Stream stream, Int128 value) + { + if (BitConverter.IsLittleEndian) + return stream.WriteLittleEndian(value); + else + return stream.WriteBigEndian(value); + } + + /// + /// Write an Int128 + /// + /// Writes in big-endian format + public static bool WriteBigEndian(this Stream stream, Int128 value) + { + byte[] buffer = value.GetBytesBigEndian(); + return WriteFromBuffer(stream, buffer); + } + + /// + /// Write an Int128 + /// + /// Writes in little-endian format + public static bool WriteLittleEndian(this Stream stream, Int128 value) + { + byte[] buffer = value.GetBytesLittleEndian(); + return WriteFromBuffer(stream, buffer); + } + + /// + /// Write a UInt128 + /// + /// Writes in machine native format + public static bool Write(this Stream stream, UInt128 value) + { + if (BitConverter.IsLittleEndian) + return stream.WriteLittleEndian(value); + else + return stream.WriteBigEndian(value); + } + + /// + /// Write a UInt128 + /// + /// Writes in big-endian format + public static bool WriteBigEndian(this Stream stream, UInt128 value) + { + byte[] buffer = value.GetBytesBigEndian(); + return WriteFromBuffer(stream, buffer); + } + + /// + /// Write a UInt128 + /// + /// Writes in little-endian format + public static bool WriteLittleEndian(this Stream stream, UInt128 value) + { + byte[] buffer = value.GetBytesLittleEndian(); + return WriteFromBuffer(stream, buffer); + } +#endif + + /// + /// Write a Decimal and increment the pointer to an array + /// + /// Writes in machine native format + public static bool Write(this Stream stream, decimal value) + { + if (BitConverter.IsLittleEndian) + return stream.WriteLittleEndian(value); + else + return stream.WriteBigEndian(value); + } + + /// + /// Write a Decimal and increment the pointer to an array + /// + /// Writes in big-endian format + public static bool WriteBigEndian(this Stream stream, decimal value) + { + byte[] buffer = value.GetBytesBigEndian(); + return WriteFromBuffer(stream, buffer); + } + + /// + /// Write a Decimal and increment the pointer to an array + /// + /// Writes in little-endian format + public static bool WriteLittleEndian(this Stream stream, decimal value) + { + byte[] buffer = value.GetBytesLittleEndian(); + return WriteFromBuffer(stream, buffer); + } + + /// + /// Write an array of bytes to the stream + /// + private static bool WriteFromBuffer(Stream stream, byte[] value) + { + // If the stream is not writable + if (!stream.CanWrite) + return false; + + // Handle the 0-byte case + if (value.Length == 0) + return true; + + // Handle the general case, forcing a write of the correct length + stream.Write(value, 0, value.Length); + return true; + } + } +} diff --git a/SabreTools.Security.Cryptography/AESCTR.cs b/SabreTools.Security.Cryptography/AESCTR.cs index 2b8cbfd..a09fd69 100644 --- a/SabreTools.Security.Cryptography/AESCTR.cs +++ b/SabreTools.Security.Cryptography/AESCTR.cs @@ -4,6 +4,7 @@ using Org.BouncyCastle.Crypto; using Org.BouncyCastle.Crypto.Parameters; using Org.BouncyCastle.Security; using SabreTools.IO.Extensions; +using SabreTools.Numerics.Extensions; namespace SabreTools.Security.Cryptography {