diff --git a/SabreTools.Serialization/ByteArrayExtensions.cs b/SabreTools.Serialization/ByteArrayExtensions.cs new file mode 100644 index 00000000..73ba9705 --- /dev/null +++ b/SabreTools.Serialization/ByteArrayExtensions.cs @@ -0,0 +1,40 @@ +using System.Collections.Generic; +using System.Text; +using SabreTools.IO.Extensions; + +namespace SabreTools.Serialization +{ + // TODO: Move this to IO + public static class ByteArrayExtensions + { + /// + /// Read string data from the source + /// + /// Number of characters needed to be a valid string, default 5 + /// String list containing the requested data, null on error + public static List? ReadStringsFrom(this byte[]? input, int charLimit = 5) + { + // Validate the data + if (input == null) + return null; + + // Check for ASCII strings + var asciiStrings = input.ReadStringsWithEncoding(charLimit, Encoding.ASCII); + + // Check for UTF-8 strings + // We are limiting the check for Unicode characters with a second byte of 0x00 for now + var utf8Strings = input.ReadStringsWithEncoding(charLimit, Encoding.UTF8); + + // Check for Unicode strings + // We are limiting the check for Unicode characters with a second byte of 0x00 for now + var unicodeStrings = input.ReadStringsWithEncoding(charLimit, Encoding.Unicode); + + // Ignore duplicate strings across encodings + List sourceStrings = [.. asciiStrings, .. utf8Strings, .. unicodeStrings]; + + // Sort the strings and return + sourceStrings.Sort(); + return sourceStrings; + } + } +} \ No newline at end of file diff --git a/SabreTools.Serialization/Wrappers/NewExecutable.cs b/SabreTools.Serialization/Wrappers/NewExecutable.cs index e4e05f50..76915b9b 100644 --- a/SabreTools.Serialization/Wrappers/NewExecutable.cs +++ b/SabreTools.Serialization/Wrappers/NewExecutable.cs @@ -178,30 +178,17 @@ namespace SabreTools.Serialization.Wrappers if (Header == null || SegmentTable == null || ResourceTable?.ResourceTypes == null) return null; - // Get the overlay address if possible - long endOfSectionData = OverlayAddress; - - // If we didn't find the end of section data - if (endOfSectionData <= 0) - return null; - - // If we're at the end of the file, cache an empty byte array - if (endOfSectionData >= dataLength) + // Get the overlay data, if possible + byte[]? overlayData = OverlayData; + if (overlayData == null || overlayData.Length == 0) { _overlayStrings = []; return _overlayStrings; } - lock (_sourceDataLock) - { - // TODO: Revisit the 16 MiB limit - // Cap the check for overlay strings to 16 MiB (arbitrary) - long overlayLength = Math.Min(dataLength - endOfSectionData, 16 * 1024 * 1024); - - // Otherwise, cache and return the strings - _overlayStrings = _dataSource.ReadStringsFrom((int)endOfSectionData, (int)overlayLength, charLimit: 3); - return _overlayStrings; - } + // Otherwise, cache and return the strings + _overlayStrings = overlayData.ReadStringsFrom(charLimit: 3); + return _overlayStrings; } } } diff --git a/SabreTools.Serialization/Wrappers/PortableExecutable.cs b/SabreTools.Serialization/Wrappers/PortableExecutable.cs index 6ce94e9c..b21aa28d 100644 --- a/SabreTools.Serialization/Wrappers/PortableExecutable.cs +++ b/SabreTools.Serialization/Wrappers/PortableExecutable.cs @@ -353,30 +353,17 @@ namespace SabreTools.Serialization.Wrappers if (SectionTable == null) return null; - // Get the overlay address if possible - long endOfSectionData = OverlayAddress; - - // If we didn't find the end of section data - if (endOfSectionData <= 0) - return null; - - // If we're at the end of the file, cache an empty byte array - if (endOfSectionData >= dataLength) + // Get the overlay data, if possible + byte[]? overlayData = OverlayData; + if (overlayData == null || overlayData.Length == 0) { _overlayStrings = []; return _overlayStrings; } - lock (_sourceDataLock) - { - // TODO: Revisit the 16 MiB limit - // Cap the check for overlay strings to 16 MiB (arbitrary) - long overlayLength = Math.Min(dataLength - endOfSectionData, 16 * 1024 * 1024); - - // Otherwise, cache and return the strings - _overlayStrings = _dataSource.ReadStringsFrom((int)endOfSectionData, (int)overlayLength, charLimit: 3); - return _overlayStrings; - } + // Otherwise, cache and return the strings + _overlayStrings = overlayData.ReadStringsFrom(charLimit: 3); + return _overlayStrings; } } }