From 3e0a6dbf326639a87ef3462ed79f783708b4f1f0 Mon Sep 17 00:00:00 2001 From: Matt Nadareski Date: Thu, 12 Feb 2026 15:53:40 -0500 Subject: [PATCH] Move old version of AlignToBoundary --- .../Extensions/PortableExecutable.cs | 32 -------- .../Readers/PortableExecutable.cs | 76 +++++++++++++------ 2 files changed, 54 insertions(+), 54 deletions(-) diff --git a/SabreTools.Serialization/Extensions/PortableExecutable.cs b/SabreTools.Serialization/Extensions/PortableExecutable.cs index b77a0b26..e2e6f34d 100644 --- a/SabreTools.Serialization/Extensions/PortableExecutable.cs +++ b/SabreTools.Serialization/Extensions/PortableExecutable.cs @@ -205,37 +205,5 @@ namespace SabreTools.Data.Extensions } #endregion - - #region Helpers - - /// - /// Align the array position to a byte-size boundary - /// - /// Input array to try aligning - /// Offset into the byte array - /// Number of bytes to align on - /// True if the array could be aligned, false otherwise - /// TODO: Remove when IO is updated - internal static bool AlignToBoundary(this byte[]? input, ref int offset, byte alignment) - { - // If the array is invalid - if (input is null || input.Length == 0) - return false; - - // If already at the end of the array - if (offset >= input.Length) - return false; - - // Align the stream position - while (offset % alignment != 0 && offset < input.Length) - { - _ = input.ReadByteValue(ref offset); - } - - // Return if the alignment completed - return offset % alignment == 0; - } - - #endregion } } diff --git a/SabreTools.Serialization/Readers/PortableExecutable.cs b/SabreTools.Serialization/Readers/PortableExecutable.cs index 3e5bebf3..bd60f3af 100644 --- a/SabreTools.Serialization/Readers/PortableExecutable.cs +++ b/SabreTools.Serialization/Readers/PortableExecutable.cs @@ -904,7 +904,7 @@ namespace SabreTools.Serialization.Readers obj.ClassResource = data.ReadNullTerminatedUnicodeString(ref offset) ?? string.Empty; // Align to the WORD boundary if we're not at the end - data.AlignToBoundary(ref offset, 2); + AlignToBoundary(data, ref offset, 2); } #endregion @@ -930,7 +930,7 @@ namespace SabreTools.Serialization.Readers obj.TitleResource = data.ReadNullTerminatedUnicodeString(ref offset) ?? string.Empty; // Align to the WORD boundary if we're not at the end - data.AlignToBoundary(ref offset, 2); + AlignToBoundary(data, ref offset, 2); } #endregion @@ -944,7 +944,7 @@ namespace SabreTools.Serialization.Readers #endregion // Align to the DWORD boundary if we're not at the end - data.AlignToBoundary(ref offset, 4); + AlignToBoundary(data, ref offset, 4); return obj; } @@ -994,7 +994,7 @@ namespace SabreTools.Serialization.Readers obj.ClassResource = data.ReadNullTerminatedUnicodeString(ref offset) ?? string.Empty; // Align to the WORD boundary if we're not at the end - data.AlignToBoundary(ref offset, 2); + AlignToBoundary(data, ref offset, 2); } #endregion @@ -1020,7 +1020,7 @@ namespace SabreTools.Serialization.Readers obj.TitleResource = data.ReadNullTerminatedUnicodeString(ref offset) ?? string.Empty; // Align to the WORD boundary if we're not at the end - data.AlignToBoundary(ref offset, 2); + AlignToBoundary(data, ref offset, 2); } #endregion @@ -1034,7 +1034,7 @@ namespace SabreTools.Serialization.Readers #endregion // Align to the DWORD boundary if we're not at the end - data.AlignToBoundary(ref offset, 4); + AlignToBoundary(data, ref offset, 4); return obj; } @@ -1080,7 +1080,7 @@ namespace SabreTools.Serialization.Readers obj.MenuResource = data.ReadNullTerminatedUnicodeString(ref offset) ?? string.Empty; // Align to the WORD boundary if we're not at the end - data.AlignToBoundary(ref offset, 2); + AlignToBoundary(data, ref offset, 2); // Read the ordinal if we have the flag set if (menuResourceHasOrdinal) @@ -1116,7 +1116,7 @@ namespace SabreTools.Serialization.Readers obj.ClassResource = data.ReadNullTerminatedUnicodeString(ref offset) ?? string.Empty; // Align to the WORD boundary if we're not at the end - data.AlignToBoundary(ref offset, 2); + AlignToBoundary(data, ref offset, 2); // Read the ordinal if we have the flag set if (classResourcehasOrdinal) @@ -1147,7 +1147,7 @@ namespace SabreTools.Serialization.Readers obj.TitleResource = data.ReadNullTerminatedUnicodeString(ref offset) ?? string.Empty; // Align to the WORD boundary if we're not at the end - data.AlignToBoundary(ref offset, 2); + AlignToBoundary(data, ref offset, 2); } #endregion @@ -1168,7 +1168,7 @@ namespace SabreTools.Serialization.Readers } // Align to the DWORD boundary if we're not at the end - data.AlignToBoundary(ref offset, 4); + AlignToBoundary(data, ref offset, 4); #endregion @@ -1219,7 +1219,7 @@ namespace SabreTools.Serialization.Readers obj.MenuResource = data.ReadNullTerminatedUnicodeString(ref offset) ?? string.Empty; // Align to the WORD boundary if we're not at the end - data.AlignToBoundary(ref offset, 2); + AlignToBoundary(data, ref offset, 2); // Read the ordinal if we have the flag set if (menuResourceHasOrdinal) @@ -1251,7 +1251,7 @@ namespace SabreTools.Serialization.Readers obj.ClassResource = data.ReadNullTerminatedUnicodeString(ref offset) ?? string.Empty; // Align to the WORD boundary if we're not at the end - data.AlignToBoundary(ref offset, 2); + AlignToBoundary(data, ref offset, 2); // Read the ordinal if we have the flag set if (classResourcehasOrdinal) @@ -1278,7 +1278,7 @@ namespace SabreTools.Serialization.Readers obj.TitleResource = data.ReadNullTerminatedUnicodeString(ref offset) ?? string.Empty; // Align to the WORD boundary if we're not at the end - data.AlignToBoundary(ref offset, 2); + AlignToBoundary(data, ref offset, 2); } #endregion @@ -1300,7 +1300,7 @@ namespace SabreTools.Serialization.Readers } // Align to the DWORD boundary if we're not at the end - data.AlignToBoundary(ref offset, 4); + AlignToBoundary(data, ref offset, 4); #endregion @@ -2063,7 +2063,7 @@ namespace SabreTools.Serialization.Readers extendedMenuItems.Add(extendedMenuItem); // Align to the DWORD boundary if we're not at the end - data.AlignToBoundary(ref offset, 4); + AlignToBoundary(data, ref offset, 4); } } @@ -2094,7 +2094,7 @@ namespace SabreTools.Serialization.Readers menuItem = ParseNormalMenuItem(data, ref offset); // Align to the DWORD boundary if we're not at the end - data.AlignToBoundary(ref offset, 4); + AlignToBoundary(data, ref offset, 4); if (menuItem is null) return null; @@ -2734,7 +2734,7 @@ namespace SabreTools.Serialization.Readers } // Align to the DWORD boundary if we're not at the end - data.AlignToBoundary(ref offset, 4); + AlignToBoundary(data, ref offset, 4); var stringFileInfoChildren = new List(); while ((offset - currentOffset) < obj.Length) @@ -2765,7 +2765,7 @@ namespace SabreTools.Serialization.Readers obj.Key = data.ReadNullTerminatedUnicodeString(ref offset) ?? string.Empty; // Align to the DWORD boundary if we're not at the end - data.AlignToBoundary(ref offset, 4); + AlignToBoundary(data, ref offset, 4); if (obj.ValueLength > 0) { @@ -2775,7 +2775,7 @@ namespace SabreTools.Serialization.Readers } // Align to the DWORD boundary if we're not at the end - data.AlignToBoundary(ref offset, 4); + AlignToBoundary(data, ref offset, 4); return obj; } @@ -2825,7 +2825,7 @@ namespace SabreTools.Serialization.Readers obj.Key = data.ReadNullTerminatedUnicodeString(ref offset) ?? string.Empty; // Align to the DWORD boundary if we're not at the end - data.AlignToBoundary(ref offset, 4); + AlignToBoundary(data, ref offset, 4); var stringTableChildren = new List(); while ((offset - initialOffset) < obj.Length) @@ -2942,7 +2942,7 @@ namespace SabreTools.Serialization.Readers return null; // Align to the DWORD boundary if we're not at the end - data.AlignToBoundary(ref offset, 4); + AlignToBoundary(data, ref offset, 4); var varFileInfoChildren = new List(); while ((offset - initialOffset) < obj.Length) @@ -2960,7 +2960,7 @@ namespace SabreTools.Serialization.Readers } // Align to the DWORD boundary if we're not at the end - data.AlignToBoundary(ref offset, 4); + AlignToBoundary(data, ref offset, 4); // Cache the current offset int currentOffset = offset; @@ -3041,5 +3041,37 @@ namespace SabreTools.Serialization.Readers return obj; } + + #region Helpers + + /// + /// Align the array position to a byte-size boundary + /// + /// Input array to try aligning + /// Offset into the byte array + /// Number of bytes to align on + /// True if the array could be aligned, false otherwise + /// TODO: Remove when IO is updated + private static bool AlignToBoundary(byte[]? input, ref int offset, byte alignment) + { + // If the array is invalid + if (input is null || input.Length == 0) + return false; + + // If already at the end of the array + if (offset >= input.Length) + return false; + + // Align the stream position + while (offset % alignment != 0 && offset < input.Length) + { + _ = input.ReadByteValue(ref offset); + } + + // Return if the alignment completed + return offset % alignment == 0; + } + + #endregion } }