2024-10-24 00:06:58 -04:00
|
|
|
using System;
|
|
|
|
|
|
|
|
|
|
namespace SabreTools.IO.Extensions
|
|
|
|
|
{
|
|
|
|
|
public static class ByteArrayExtensions
|
|
|
|
|
{
|
2026-02-12 13:48:52 -05:00
|
|
|
/// <summary>
|
|
|
|
|
/// Align the array position to a byte-size boundary
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="input">Input array to try aligning</param>
|
|
|
|
|
/// <param name="offset">Offset into the byte array</param>
|
|
|
|
|
/// <param name="alignment">Number of bytes to align on</param>
|
|
|
|
|
/// <returns>True if the array could be aligned, false otherwise</returns>
|
|
|
|
|
public 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)
|
|
|
|
|
{
|
2026-03-22 15:22:46 -04:00
|
|
|
offset++;
|
2026-02-12 13:48:52 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Return if the alignment completed
|
|
|
|
|
return offset % alignment == 0;
|
|
|
|
|
}
|
|
|
|
|
|
2024-11-25 11:29:22 -05:00
|
|
|
/// <summary>
|
|
|
|
|
/// Indicates whether the specified array is null or has a length of zero
|
|
|
|
|
/// </summary>
|
|
|
|
|
public static bool IsNullOrEmpty(this Array? array)
|
|
|
|
|
{
|
2026-01-25 17:04:11 -05:00
|
|
|
return array is null || array.Length == 0;
|
2024-11-25 11:29:22 -05:00
|
|
|
}
|
2024-10-24 00:06:58 -04:00
|
|
|
}
|
2025-07-23 10:25:19 -04:00
|
|
|
}
|