From a93da97bbd7983aec7539c53f5a68b22bf1a0a75 Mon Sep 17 00:00:00 2001 From: Matt Nadareski Date: Thu, 18 Apr 2024 11:55:25 -0400 Subject: [PATCH] Add floating-point extensions for byte arrays and streams --- .../Extensions/ByteArrayExtensionsTests.cs | 36 ++++++++++++ .../Extensions/StreamExtensionsTests.cs | 36 ++++++++++++ .../Extensions/ByteArrayExtensions.cs | 56 ++++++++++++++++--- SabreTools.IO/Extensions/StreamExtensions.cs | 52 ++++++++++++++--- 4 files changed, 164 insertions(+), 16 deletions(-) diff --git a/SabreTools.IO.Test/Extensions/ByteArrayExtensionsTests.cs b/SabreTools.IO.Test/Extensions/ByteArrayExtensionsTests.cs index 1068976..72bf756 100644 --- a/SabreTools.IO.Test/Extensions/ByteArrayExtensionsTests.cs +++ b/SabreTools.IO.Test/Extensions/ByteArrayExtensionsTests.cs @@ -118,6 +118,24 @@ namespace SabreTools.IO.Test Assert.Equal((uint)0x00010203, read); } + [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 ReadInt64Test() { @@ -150,6 +168,24 @@ namespace SabreTools.IO.Test Assert.Equal((ulong)0x0001020304050607, read); } + [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 ReadGuidTest() { diff --git a/SabreTools.IO.Test/Extensions/StreamExtensionsTests.cs b/SabreTools.IO.Test/Extensions/StreamExtensionsTests.cs index 1e7e838..15fb149 100644 --- a/SabreTools.IO.Test/Extensions/StreamExtensionsTests.cs +++ b/SabreTools.IO.Test/Extensions/StreamExtensionsTests.cs @@ -112,6 +112,24 @@ namespace SabreTools.IO.Test Assert.Equal((uint)0x00010203, read); } + [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 ReadInt64Test() { @@ -144,6 +162,24 @@ namespace SabreTools.IO.Test Assert.Equal((ulong)0x0001020304050607, read); } + [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 ReadGuidTest() { diff --git a/SabreTools.IO/Extensions/ByteArrayExtensions.cs b/SabreTools.IO/Extensions/ByteArrayExtensions.cs index 37bf992..1355ada 100644 --- a/SabreTools.IO/Extensions/ByteArrayExtensions.cs +++ b/SabreTools.IO/Extensions/ByteArrayExtensions.cs @@ -32,7 +32,7 @@ namespace SabreTools.IO.Extensions => ReadToBuffer(content, ref offset, count); /// - /// Read a Int8 and increment the pointer to an array + /// Read an Int8 and increment the pointer to an array /// public static sbyte ReadSByte(this byte[] content, ref int offset) { @@ -50,7 +50,7 @@ namespace SabreTools.IO.Extensions } /// - /// Read a Int16 and increment the pointer to an array + /// Read an Int16 and increment the pointer to an array /// public static short ReadInt16(this byte[] content, ref int offset) { @@ -59,7 +59,7 @@ namespace SabreTools.IO.Extensions } /// - /// Read a Int16 in big-endian format and increment the pointer to an array + /// Read an Int16 in big-endian format and increment the pointer to an array /// public static short ReadInt16BigEndian(this byte[] content, ref int offset) { @@ -88,7 +88,7 @@ namespace SabreTools.IO.Extensions } /// - /// Read a Int32 and increment the pointer to an array + /// Read an Int32 and increment the pointer to an array /// public static int ReadInt32(this byte[] content, ref int offset) { @@ -97,7 +97,7 @@ namespace SabreTools.IO.Extensions } /// - /// Read a Int32 in big-endian format and increment the pointer to an array + /// Read an Int32 in big-endian format and increment the pointer to an array /// public static int ReadInt32BigEndian(this byte[] content, ref int offset) { @@ -126,7 +126,26 @@ namespace SabreTools.IO.Extensions } /// - /// Read a Int64 and increment the pointer to an array + /// Read a Single and increment the pointer to an array + /// + public static float ReadSingle(this byte[] content, ref int offset) + { + byte[] buffer = ReadToBuffer(content, ref offset, 4); + return BitConverter.ToSingle(buffer, 0); + } + + /// + /// Read a Single in big-endian format and increment the pointer to an array + /// + public static float ReadSingleBigEndian(this byte[] content, ref int offset) + { + byte[] buffer = ReadToBuffer(content, ref offset, 4); + Array.Reverse(buffer); + return BitConverter.ToSingle(buffer, 0); + } + + /// + /// Read an Int64 and increment the pointer to an array /// public static long ReadInt64(this byte[] content, ref int offset) { @@ -135,7 +154,7 @@ namespace SabreTools.IO.Extensions } /// - /// Read a Int64 in big-endian format and increment the pointer to an array + /// Read an Int64 in big-endian format and increment the pointer to an array /// public static long ReadInt64BigEndian(this byte[] content, ref int offset) { @@ -163,6 +182,25 @@ namespace SabreTools.IO.Extensions return BitConverter.ToUInt64(buffer, 0); } + /// + /// Read a Double and increment the pointer to an array + /// + public static double ReadDouble(this byte[] content, ref int offset) + { + byte[] buffer = ReadToBuffer(content, ref offset, 8); + return BitConverter.ToDouble(buffer, 0); + } + + /// + /// Read a Double in big-endian format and increment the pointer to an array + /// + public static double ReadDoubleBigEndian(this byte[] content, ref int offset) + { + byte[] buffer = ReadToBuffer(content, ref offset, 8); + Array.Reverse(buffer); + return BitConverter.ToDouble(buffer, 0); + } + /// /// Read a Guid and increment the pointer to an array /// @@ -185,7 +223,7 @@ namespace SabreTools.IO.Extensions // TODO: Determine if the reverse reads are doing what are expected #if NET7_0_OR_GREATER /// - /// Read a Int128 and increment the pointer to an array + /// Read an Int128 and increment the pointer to an array /// public static Int128 ReadInt128(this byte[] content, ref int offset) { @@ -194,7 +232,7 @@ namespace SabreTools.IO.Extensions } /// - /// Read a Int128 in big-endian format and increment the pointer to an array + /// Read an Int128 in big-endian format and increment the pointer to an array /// public static Int128 ReadInt128BigEndian(this byte[] content, ref int offset) { diff --git a/SabreTools.IO/Extensions/StreamExtensions.cs b/SabreTools.IO/Extensions/StreamExtensions.cs index e482f12..26af225 100644 --- a/SabreTools.IO/Extensions/StreamExtensions.cs +++ b/SabreTools.IO/Extensions/StreamExtensions.cs @@ -28,7 +28,7 @@ namespace SabreTools.IO.Extensions => ReadToBuffer(stream, count); /// - /// Read a Int8 from the stream + /// Read an Int8 from the stream /// public static sbyte ReadSByte(this Stream stream) { @@ -46,7 +46,7 @@ namespace SabreTools.IO.Extensions } /// - /// Read a Int16 from the stream + /// Read an Int16 from the stream /// public static short ReadInt16(this Stream stream) { @@ -55,7 +55,7 @@ namespace SabreTools.IO.Extensions } /// - /// Read a Int16 from the stream in big-endian format + /// Read an Int16 from the stream in big-endian format /// public static short ReadInt16BigEndian(this Stream stream) { @@ -122,7 +122,26 @@ namespace SabreTools.IO.Extensions } /// - /// Read a Int64 from the stream + /// Read a Single from the stream + /// + public static float ReadSingle(this Stream stream) + { + byte[] buffer = ReadToBuffer(stream, 4); + return BitConverter.ToSingle(buffer, 0); + } + + /// + /// Read a Single from the stream in big-endian format + /// + public static float ReadSingleBigEndian(this Stream stream) + { + byte[] buffer = ReadToBuffer(stream, 4); + Array.Reverse(buffer); + return BitConverter.ToSingle(buffer, 0); + } + + /// + /// Read an Int64 from the stream /// public static long ReadInt64(this Stream stream) { @@ -131,7 +150,7 @@ namespace SabreTools.IO.Extensions } /// - /// Read a Int64 from the stream in big-endian format + /// Read an Int64 from the stream in big-endian format /// public static long ReadInt64BigEndian(this Stream stream) { @@ -159,6 +178,25 @@ namespace SabreTools.IO.Extensions return BitConverter.ToUInt64(buffer, 0); } + /// + /// Read a Double from the stream + /// + public static double ReadDouble(this Stream stream) + { + byte[] buffer = ReadToBuffer(stream, 8); + return BitConverter.ToDouble(buffer, 0); + } + + /// + /// Read a Double from the stream in big-endian format + /// + public static double ReadDoubleBigEndian(this Stream stream) + { + byte[] buffer = ReadToBuffer(stream, 8); + Array.Reverse(buffer); + return BitConverter.ToDouble(buffer, 0); + } + /// /// Read a Guid from the stream /// @@ -181,7 +219,7 @@ namespace SabreTools.IO.Extensions // TODO: Determine if the reverse reads are doing what are expected #if NET7_0_OR_GREATER /// - /// Read a Int128 from the stream + /// Read an Int128 from the stream /// public static Int128 ReadInt128(this Stream stream) { @@ -190,7 +228,7 @@ namespace SabreTools.IO.Extensions } /// - /// Read a Int128 from the stream in big-endian format + /// Read an Int128 from the stream in big-endian format /// public static Int128 ReadInt128BigEndian(this Stream stream) {