From 630b01283eeeb87ff59d769f834c66da2d05c849 Mon Sep 17 00:00:00 2001 From: Matt Nadareski Date: Sun, 21 Sep 2025 15:12:16 -0400 Subject: [PATCH] Latin1 instead of ASCII for .NET 5.0 and beyond --- .../Extensions/ByteArrayExtensionsTests.cs | 31 +++++++++++++++--- .../Extensions/StreamExtensionsTests.cs | 32 ++++++++++++++++--- .../Extensions/ByteArrayExtensions.cs | 9 ++++++ SabreTools.IO/Extensions/StreamExtensions.cs | 4 +++ 4 files changed, 68 insertions(+), 8 deletions(-) diff --git a/SabreTools.IO.Test/Extensions/ByteArrayExtensionsTests.cs b/SabreTools.IO.Test/Extensions/ByteArrayExtensionsTests.cs index ab6fbbb..ee4e78f 100644 --- a/SabreTools.IO.Test/Extensions/ByteArrayExtensionsTests.cs +++ b/SabreTools.IO.Test/Extensions/ByteArrayExtensionsTests.cs @@ -146,6 +146,23 @@ namespace SabreTools.IO.Test.Extensions Assert.Equal(2, actual.Count); } + [Fact] + public void ReadStringsFrom_Latin1Strings_Filled() + { + byte[]? arr = + [ + .. Encoding.Latin1.GetBytes("TEST"), + .. new byte[] { 0x00 }, + .. Encoding.Latin1.GetBytes("TWO"), + .. new byte[] { 0x00 }, + .. Encoding.Latin1.GetBytes("DATA"), + .. new byte[] { 0x00 }, + ]; + var actual = arr.ReadStringsFrom(4); + Assert.NotNull(actual); + Assert.Equal(2, actual.Count); + } + [Fact] public void ReadStringsFrom_UTF16_Filled() { @@ -174,16 +191,22 @@ namespace SabreTools.IO.Test.Extensions .. new byte[] { 0x00 }, .. Encoding.ASCII.GetBytes("DATA1"), .. new byte[] { 0x00 }, - .. Encoding.Unicode.GetBytes("TEST2"), + .. Encoding.ASCII.GetBytes("TEST2"), .. new byte[] { 0x00 }, - .. Encoding.Unicode.GetBytes("TWO2"), + .. Encoding.ASCII.GetBytes("TWO2"), .. new byte[] { 0x00 }, - .. Encoding.Unicode.GetBytes("DATA2"), + .. Encoding.ASCII.GetBytes("DATA2"), + .. new byte[] { 0x00 }, + .. Encoding.Unicode.GetBytes("TEST3"), + .. new byte[] { 0x00 }, + .. Encoding.Unicode.GetBytes("TWO3"), + .. new byte[] { 0x00 }, + .. Encoding.Unicode.GetBytes("DATA3"), .. new byte[] { 0x00 }, ]; var actual = arr.ReadStringsFrom(5); Assert.NotNull(actual); - Assert.Equal(4, actual.Count); + Assert.Equal(6, actual.Count); } /// diff --git a/SabreTools.IO.Test/Extensions/StreamExtensionsTests.cs b/SabreTools.IO.Test/Extensions/StreamExtensionsTests.cs index 9c35d0c..2609ab2 100644 --- a/SabreTools.IO.Test/Extensions/StreamExtensionsTests.cs +++ b/SabreTools.IO.Test/Extensions/StreamExtensionsTests.cs @@ -203,6 +203,24 @@ namespace SabreTools.IO.Test.Extensions Assert.Equal(2, actual.Count); } + [Fact] + public void ReadStringsFrom_Latin1Strings_Filled() + { + byte[]? bytes = + [ + .. Encoding.Latin1.GetBytes("TEST"), + .. new byte[] { 0x00 }, + .. Encoding.Latin1.GetBytes("TWO"), + .. new byte[] { 0x00 }, + .. Encoding.Latin1.GetBytes("DATA"), + .. new byte[] { 0x00 }, + ]; + Stream? stream = new MemoryStream(bytes); + var actual = stream.ReadStringsFrom(0, bytes.Length, 4); + Assert.NotNull(actual); + Assert.Equal(2, actual.Count); + } + [Fact] public void ReadStringsFrom_UTF16_Filled() { @@ -232,17 +250,23 @@ namespace SabreTools.IO.Test.Extensions .. new byte[] { 0x00 }, .. Encoding.ASCII.GetBytes("DATA1"), .. new byte[] { 0x00 }, - .. Encoding.Unicode.GetBytes("TEST2"), + .. Encoding.Latin1.GetBytes("TEST2"), .. new byte[] { 0x00 }, - .. Encoding.Unicode.GetBytes("TWO2"), + .. Encoding.Latin1.GetBytes("TWO2"), .. new byte[] { 0x00 }, - .. Encoding.Unicode.GetBytes("DATA2"), + .. Encoding.Latin1.GetBytes("DATA2"), + .. new byte[] { 0x00 }, + .. Encoding.Unicode.GetBytes("TEST3"), + .. new byte[] { 0x00 }, + .. Encoding.Unicode.GetBytes("TWO3"), + .. new byte[] { 0x00 }, + .. Encoding.Unicode.GetBytes("DATA3"), .. new byte[] { 0x00 }, ]; Stream? stream = new MemoryStream(bytes); var actual = stream.ReadStringsFrom(0, bytes.Length, 5); Assert.NotNull(actual); - Assert.Equal(4, actual.Count); + Assert.Equal(6, actual.Count); } #endregion diff --git a/SabreTools.IO/Extensions/ByteArrayExtensions.cs b/SabreTools.IO/Extensions/ByteArrayExtensions.cs index efb9f90..0c17574 100644 --- a/SabreTools.IO/Extensions/ByteArrayExtensions.cs +++ b/SabreTools.IO/Extensions/ByteArrayExtensions.cs @@ -59,15 +59,24 @@ namespace SabreTools.IO.Extensions /// /// Number of characters needed to be a valid string, default 5 /// String list containing the requested data, null on error +#if NET5_0_OR_GREATER + /// This reads both Latin1 and UTF-16 strings from the input data +#else /// This reads both ASCII and UTF-16 strings from the input data +#endif public static List? ReadStringsFrom(this byte[]? input, int charLimit = 5) { // Validate the data if (input == null || input.Length == 0) return null; +#if NET5_0_OR_GREATER + // Check for Latin1 strings + var asciiStrings = input.ReadStringsWithEncoding(charLimit, Encoding.Latin1); +#else // Check for ASCII strings var asciiStrings = input.ReadStringsWithEncoding(charLimit, Encoding.ASCII); +#endif // Check for Unicode strings // We are limiting the check for Unicode characters with a second byte of 0x00 for now diff --git a/SabreTools.IO/Extensions/StreamExtensions.cs b/SabreTools.IO/Extensions/StreamExtensions.cs index 1163cb7..a7663a9 100644 --- a/SabreTools.IO/Extensions/StreamExtensions.cs +++ b/SabreTools.IO/Extensions/StreamExtensions.cs @@ -79,7 +79,11 @@ namespace SabreTools.IO.Extensions /// Position in the source to read from /// Length of the requested data /// String list containing the requested data, null on error +#if NET5_0_OR_GREATER + /// This reads both Latin1 and UTF-16 strings from the input data +#else /// This reads both ASCII and UTF-16 strings from the input data +#endif public static List? ReadStringsFrom(this Stream? input, int position, int length, int charLimit = 5) { // Read the data as a byte array first