Add both-endian try read extensions

This commit is contained in:
Matt Nadareski
2025-10-27 11:21:29 -04:00
parent 244b7411d4
commit edd3e6eef2
6 changed files with 788 additions and 0 deletions

View File

@@ -1713,6 +1713,17 @@ namespace SabreTools.IO.Test.Extensions
Assert.Equal(default, read);
}
[Fact]
public void TryReadByteBothEndianTest()
{
var stream = new MemoryStream([]);
var br = new BinaryReader(stream);
bool actual = br.TryReadByteBothEndian(out BothUInt8 read);
Assert.False(actual);
Assert.Equal(default, read.LittleEndian);
Assert.Equal(default, read.BigEndian);
}
[Fact]
public void TryReadBytesTest()
{
@@ -1734,6 +1745,17 @@ namespace SabreTools.IO.Test.Extensions
Assert.Equal(default, read);
}
[Fact]
public void TryReadSByteBothEndianTest()
{
var stream = new MemoryStream([]);
var br = new BinaryReader(stream);
bool actual = br.TryReadSByteBothEndian(out BothInt8 read);
Assert.False(actual);
Assert.Equal(default, read.LittleEndian);
Assert.Equal(default, read.BigEndian);
}
[Fact]
public void TryReadCharTest()
{
@@ -1774,6 +1796,17 @@ namespace SabreTools.IO.Test.Extensions
Assert.Equal(default, read);
}
[Fact]
public void TryReadInt16BothEndianTest()
{
var stream = new MemoryStream([]);
var br = new BinaryReader(stream);
bool actual = br.TryReadInt16BothEndian(out BothInt16 read);
Assert.False(actual);
Assert.Equal(default, read.LittleEndian);
Assert.Equal(default, read.BigEndian);
}
[Fact]
public void TryReadUInt16Test()
{
@@ -1804,6 +1837,17 @@ namespace SabreTools.IO.Test.Extensions
Assert.Equal(default, read);
}
[Fact]
public void TryReadUInt16BothEndianTest()
{
var stream = new MemoryStream([]);
var br = new BinaryReader(stream);
bool actual = br.TryReadUInt16BothEndian(out BothUInt16 read);
Assert.False(actual);
Assert.Equal(default, read.LittleEndian);
Assert.Equal(default, read.BigEndian);
}
[Fact]
public void TryReadWORDTest()
{
@@ -1834,6 +1878,17 @@ namespace SabreTools.IO.Test.Extensions
Assert.Equal(default, read);
}
[Fact]
public void TryReadWORDBothEndianTest()
{
var stream = new MemoryStream([]);
var br = new BinaryReader(stream);
bool actual = br.TryReadWORDBothEndian(out BothUInt16 read);
Assert.False(actual);
Assert.Equal(default, read.LittleEndian);
Assert.Equal(default, read.BigEndian);
}
[Fact]
public void TryReadHalfTest()
{
@@ -1946,6 +2001,17 @@ namespace SabreTools.IO.Test.Extensions
Assert.Equal(default, read);
}
[Fact]
public void TryReadInt32BothEndianTest()
{
var stream = new MemoryStream([]);
var br = new BinaryReader(stream);
bool actual = br.TryReadInt32BothEndian(out BothInt32 read);
Assert.False(actual);
Assert.Equal(default, read.LittleEndian);
Assert.Equal(default, read.BigEndian);
}
[Fact]
public void TryReadUInt32Test()
{
@@ -1976,6 +2042,17 @@ namespace SabreTools.IO.Test.Extensions
Assert.Equal(default, read);
}
[Fact]
public void TryReadUInt32BothEndianTest()
{
var stream = new MemoryStream([]);
var br = new BinaryReader(stream);
bool actual = br.TryReadUInt32BothEndian(out BothUInt32 read);
Assert.False(actual);
Assert.Equal(default, read.LittleEndian);
Assert.Equal(default, read.BigEndian);
}
[Fact]
public void TryReadDWORDTest()
{
@@ -2006,6 +2083,17 @@ namespace SabreTools.IO.Test.Extensions
Assert.Equal(default, read);
}
[Fact]
public void TryReadDWORDBothEndianTest()
{
var stream = new MemoryStream([]);
var br = new BinaryReader(stream);
bool actual = br.TryReadDWORDBothEndian(out BothUInt32 read);
Assert.False(actual);
Assert.Equal(default, read.LittleEndian);
Assert.Equal(default, read.BigEndian);
}
[Fact]
public void TryReadSingleTest()
{
@@ -2118,6 +2206,17 @@ namespace SabreTools.IO.Test.Extensions
Assert.Equal(default, read);
}
[Fact]
public void TryReadInt64BothEndianTest()
{
var stream = new MemoryStream([]);
var br = new BinaryReader(stream);
bool actual = br.TryReadInt64BothEndian(out BothInt64 read);
Assert.False(actual);
Assert.Equal(default, read.LittleEndian);
Assert.Equal(default, read.BigEndian);
}
[Fact]
public void TryReadUInt64Test()
{
@@ -2148,6 +2247,17 @@ namespace SabreTools.IO.Test.Extensions
Assert.Equal(default, read);
}
[Fact]
public void TryReadUInt64BothEndianTest()
{
var stream = new MemoryStream([]);
var br = new BinaryReader(stream);
bool actual = br.TryReadUInt64BothEndian(out BothUInt64 read);
Assert.False(actual);
Assert.Equal(default, read.LittleEndian);
Assert.Equal(default, read.BigEndian);
}
[Fact]
public void TryReadQWORDTest()
{
@@ -2178,6 +2288,17 @@ namespace SabreTools.IO.Test.Extensions
Assert.Equal(default, read);
}
[Fact]
public void TryReadQWORDBothEndianTest()
{
var stream = new MemoryStream([]);
var br = new BinaryReader(stream);
bool actual = br.TryReadQWORDBothEndian(out BothUInt64 read);
Assert.False(actual);
Assert.Equal(default, read.LittleEndian);
Assert.Equal(default, read.BigEndian);
}
[Fact]
public void TryReadDoubleTest()
{

View File

@@ -1550,6 +1550,16 @@ namespace SabreTools.IO.Test.Extensions
Assert.Equal(default, read);
}
[Fact]
public void TryReadByteBothEndianTest()
{
int offset = 0;
bool actual = Array.Empty<byte>().TryReadByteBothEndian(ref offset, out BothUInt8 read);
Assert.False(actual);
Assert.Equal(default, read.LittleEndian);
Assert.Equal(default, read.BigEndian);
}
[Fact]
public void TryReadBytesTest()
{
@@ -1568,6 +1578,16 @@ namespace SabreTools.IO.Test.Extensions
Assert.Equal(default, read);
}
[Fact]
public void TryReadSByteBothEndianTest()
{
int offset = 0;
bool actual = Array.Empty<byte>().TryReadSByteBothEndian(ref offset, out BothInt8 read);
Assert.False(actual);
Assert.Equal(default, read.LittleEndian);
Assert.Equal(default, read.BigEndian);
}
[Fact]
public void TryReadCharTest()
{
@@ -1604,6 +1624,16 @@ namespace SabreTools.IO.Test.Extensions
Assert.Equal(default, read);
}
[Fact]
public void TryReadInt16BothEndianTest()
{
int offset = 0;
bool actual = Array.Empty<byte>().TryReadInt16BothEndian(ref offset, out BothInt16 read);
Assert.False(actual);
Assert.Equal(default, read.LittleEndian);
Assert.Equal(default, read.BigEndian);
}
[Fact]
public void TryReadUInt16Test()
{
@@ -1631,6 +1661,16 @@ namespace SabreTools.IO.Test.Extensions
Assert.Equal(default, read);
}
[Fact]
public void TryReadUInt16BothEndianTest()
{
int offset = 0;
bool actual = Array.Empty<byte>().TryReadUInt16BothEndian(ref offset, out BothUInt16 read);
Assert.False(actual);
Assert.Equal(default, read.LittleEndian);
Assert.Equal(default, read.BigEndian);
}
[Fact]
public void TryReadWORDTest()
{
@@ -1658,6 +1698,16 @@ namespace SabreTools.IO.Test.Extensions
Assert.Equal(default, read);
}
[Fact]
public void TryReadWORDBothEndianTest()
{
int offset = 0;
bool actual = Array.Empty<byte>().TryReadWORDBothEndian(ref offset, out BothUInt16 read);
Assert.False(actual);
Assert.Equal(default, read.LittleEndian);
Assert.Equal(default, read.BigEndian);
}
[Fact]
public void TryReadHalfTest()
{
@@ -1759,6 +1809,16 @@ namespace SabreTools.IO.Test.Extensions
Assert.Equal(default, read);
}
[Fact]
public void TryReadInt32BothEndianTest()
{
int offset = 0;
bool actual = Array.Empty<byte>().TryReadInt32BothEndian(ref offset, out BothInt32 read);
Assert.False(actual);
Assert.Equal(default, read.LittleEndian);
Assert.Equal(default, read.BigEndian);
}
[Fact]
public void TryReadUInt32Test()
{
@@ -1786,6 +1846,16 @@ namespace SabreTools.IO.Test.Extensions
Assert.Equal(default, read);
}
[Fact]
public void TryReadUInt32BothEndianTest()
{
int offset = 0;
bool actual = Array.Empty<byte>().TryReadUInt32BothEndian(ref offset, out BothUInt32 read);
Assert.False(actual);
Assert.Equal(default, read.LittleEndian);
Assert.Equal(default, read.BigEndian);
}
[Fact]
public void TryReadDWORDTest()
{
@@ -1813,6 +1883,16 @@ namespace SabreTools.IO.Test.Extensions
Assert.Equal(default, read);
}
[Fact]
public void TryReadDWORDBothEndianTest()
{
int offset = 0;
bool actual = Array.Empty<byte>().TryReadDWORDBothEndian(ref offset, out BothUInt32 read);
Assert.False(actual);
Assert.Equal(default, read.LittleEndian);
Assert.Equal(default, read.BigEndian);
}
[Fact]
public void TryReadSingleTest()
{
@@ -1914,6 +1994,16 @@ namespace SabreTools.IO.Test.Extensions
Assert.Equal(default, read);
}
[Fact]
public void TryReadInt64BothEndianTest()
{
int offset = 0;
bool actual = Array.Empty<byte>().TryReadInt64BothEndian(ref offset, out BothInt64 read);
Assert.False(actual);
Assert.Equal(default, read.LittleEndian);
Assert.Equal(default, read.BigEndian);
}
[Fact]
public void TryReadUInt64Test()
{
@@ -1941,6 +2031,16 @@ namespace SabreTools.IO.Test.Extensions
Assert.Equal(default, read);
}
[Fact]
public void TryReadUInt64BothEndianTest()
{
int offset = 0;
bool actual = Array.Empty<byte>().TryReadUInt64BothEndian(ref offset, out BothUInt64 read);
Assert.False(actual);
Assert.Equal(default, read.LittleEndian);
Assert.Equal(default, read.BigEndian);
}
[Fact]
public void TryReadQWORDTest()
{
@@ -1968,6 +2068,16 @@ namespace SabreTools.IO.Test.Extensions
Assert.Equal(default, read);
}
[Fact]
public void TryReadQWORDBothEndianTest()
{
int offset = 0;
bool actual = Array.Empty<byte>().TryReadQWORDBothEndian(ref offset, out BothUInt64 read);
Assert.False(actual);
Assert.Equal(default, read.LittleEndian);
Assert.Equal(default, read.BigEndian);
}
[Fact]
public void TryReadDoubleTest()
{

View File

@@ -1547,6 +1547,16 @@ namespace SabreTools.IO.Test.Extensions
Assert.Empty(read);
}
[Fact]
public void TryReadByteBothEndianTest()
{
var stream = new MemoryStream([]);
bool actual = stream.TryReadByteBothEndian(out BothUInt8 read);
Assert.False(actual);
Assert.Equal(default, read.LittleEndian);
Assert.Equal(default, read.BigEndian);
}
[Fact]
public void TryReadSByteTest()
{
@@ -1556,6 +1566,16 @@ namespace SabreTools.IO.Test.Extensions
Assert.Equal(default, read);
}
[Fact]
public void TryReadSByteBothEndianTest()
{
var stream = new MemoryStream([]);
bool actual = stream.TryReadSByteBothEndian(out BothInt8 read);
Assert.False(actual);
Assert.Equal(default, read.LittleEndian);
Assert.Equal(default, read.BigEndian);
}
[Fact]
public void TryReadCharTest()
{
@@ -1592,6 +1612,16 @@ namespace SabreTools.IO.Test.Extensions
Assert.Equal(default, read);
}
[Fact]
public void TryReadInt16BothEndianTest()
{
var stream = new MemoryStream([]);
bool actual = stream.TryReadInt16BothEndian(out BothInt16 read);
Assert.False(actual);
Assert.Equal(default, read.LittleEndian);
Assert.Equal(default, read.BigEndian);
}
[Fact]
public void TryReadUInt16Test()
{
@@ -1619,6 +1649,16 @@ namespace SabreTools.IO.Test.Extensions
Assert.Equal(default, read);
}
[Fact]
public void TryReadUInt16BothEndianTest()
{
var stream = new MemoryStream([]);
bool actual = stream.TryReadUInt16BothEndian(out BothUInt16 read);
Assert.False(actual);
Assert.Equal(default, read.LittleEndian);
Assert.Equal(default, read.BigEndian);
}
[Fact]
public void TryReadWORDTest()
{
@@ -1646,6 +1686,16 @@ namespace SabreTools.IO.Test.Extensions
Assert.Equal(default, read);
}
[Fact]
public void TryReadWORDBothEndianTest()
{
var stream = new MemoryStream([]);
bool actual = stream.TryReadWORDBothEndian(out BothUInt16 read);
Assert.False(actual);
Assert.Equal(default, read.LittleEndian);
Assert.Equal(default, read.BigEndian);
}
[Fact]
public void TryReadHalfTest()
{
@@ -1747,6 +1797,16 @@ namespace SabreTools.IO.Test.Extensions
Assert.Equal(default, read);
}
[Fact]
public void TryReadInt32BothEndianTest()
{
var stream = new MemoryStream([]);
bool actual = stream.TryReadInt32BothEndian(out BothInt32 read);
Assert.False(actual);
Assert.Equal(default, read.LittleEndian);
Assert.Equal(default, read.BigEndian);
}
[Fact]
public void TryReadUInt32Test()
{
@@ -1774,6 +1834,16 @@ namespace SabreTools.IO.Test.Extensions
Assert.Equal(default, read);
}
[Fact]
public void TryReadUInt32BothEndianTest()
{
var stream = new MemoryStream([]);
bool actual = stream.TryReadUInt32BothEndian(out BothUInt32 read);
Assert.False(actual);
Assert.Equal(default, read.LittleEndian);
Assert.Equal(default, read.BigEndian);
}
[Fact]
public void TryReadDWORDTest()
{
@@ -1801,6 +1871,16 @@ namespace SabreTools.IO.Test.Extensions
Assert.Equal(default, read);
}
[Fact]
public void TryReadDWORDBothEndianTest()
{
var stream = new MemoryStream([]);
bool actual = stream.TryReadDWORDBothEndian(out BothUInt32 read);
Assert.False(actual);
Assert.Equal(default, read.LittleEndian);
Assert.Equal(default, read.BigEndian);
}
[Fact]
public void TryReadSingleTest()
{
@@ -1902,6 +1982,16 @@ namespace SabreTools.IO.Test.Extensions
Assert.Equal(default, read);
}
[Fact]
public void TryReadInt64BothEndianTest()
{
var stream = new MemoryStream([]);
bool actual = stream.TryReadInt64BothEndian(out BothInt64 read);
Assert.False(actual);
Assert.Equal(default, read.LittleEndian);
Assert.Equal(default, read.BigEndian);
}
[Fact]
public void TryReadUInt64Test()
{
@@ -1929,6 +2019,16 @@ namespace SabreTools.IO.Test.Extensions
Assert.Equal(default, read);
}
[Fact]
public void TryReadUInt64BothEndianTest()
{
var stream = new MemoryStream([]);
bool actual = stream.TryReadUInt64BothEndian(out BothUInt64 read);
Assert.False(actual);
Assert.Equal(default, read.LittleEndian);
Assert.Equal(default, read.BigEndian);
}
[Fact]
public void TryReadQWORDTest()
{
@@ -1956,6 +2056,16 @@ namespace SabreTools.IO.Test.Extensions
Assert.Equal(default, read);
}
[Fact]
public void TryReadQWORDBothEndianTest()
{
var stream = new MemoryStream([]);
bool actual = stream.TryReadQWORDBothEndian(out BothUInt64 read);
Assert.False(actual);
Assert.Equal(default, read.LittleEndian);
Assert.Equal(default, read.BigEndian);
}
[Fact]
public void TryReadDoubleTest()
{

View File

@@ -1760,6 +1760,22 @@ namespace SabreTools.IO.Extensions
return true;
}
/// <summary>
/// Read a UInt8 from the base stream
/// </summary>
/// <remarks>Reads in both-endian format</remarks>
public static bool TryReadByteBothEndian(this BinaryReader reader, out BothUInt8 value)
{
if (reader.BaseStream.Position > reader.BaseStream.Length - 2)
{
value = default(byte);
return false;
}
value = reader.ReadByteBothEndian();
return true;
}
/// <summary>
/// Read a UInt8[] from the base stream
/// </summary>
@@ -1790,6 +1806,22 @@ namespace SabreTools.IO.Extensions
return true;
}
/// <summary>
/// Read a Int8 from the base stream
/// </summary>
/// <remarks>Reads in both-endian format</remarks>
public static bool TryReadSByteBothEndian(this BinaryReader reader, out BothInt8 value)
{
if (reader.BaseStream.Position > reader.BaseStream.Length - 2)
{
value = default(sbyte);
return false;
}
value = reader.ReadSByteBothEndian();
return true;
}
/// <summary>
/// Read a Char from the base stream
/// </summary>
@@ -1849,6 +1881,22 @@ namespace SabreTools.IO.Extensions
return true;
}
/// <summary>
/// Read a Int16 from the base stream
/// </summary>
/// <remarks>Reads in both-endian format</remarks>
public static bool TryReadInt16BothEndian(this BinaryReader reader, out BothInt16 value)
{
if (reader.BaseStream.Position > reader.BaseStream.Length - 4)
{
value = default(short);
return false;
}
value = reader.ReadInt16BothEndian();
return true;
}
/// <summary>
/// Read a UInt16 from the base stream
/// </summary>
@@ -1893,6 +1941,22 @@ namespace SabreTools.IO.Extensions
return true;
}
/// <summary>
/// Read a UInt16 from the base stream
/// </summary>
/// <remarks>Reads in both-endian format</remarks>
public static bool TryReadUInt16BothEndian(this BinaryReader reader, out BothUInt16 value)
{
if (reader.BaseStream.Position > reader.BaseStream.Length - 4)
{
value = default(ushort);
return false;
}
value = reader.ReadUInt16BothEndian();
return true;
}
/// <summary>
/// Read a WORD (2-byte) from the base stream
/// </summary>
@@ -1914,6 +1978,13 @@ namespace SabreTools.IO.Extensions
public static bool TryReadWORDLittleEndian(this BinaryReader reader, out ushort value)
=> reader.TryReadUInt16LittleEndian(out value);
/// <summary>
/// Read a WORD (2-byte) from the base stream
/// </summary>
/// <remarks>Reads in both-endian format</remarks>
public static bool TryReadWORDBothEndian(this BinaryReader reader, out BothUInt16 value)
=> reader.TryReadUInt16BothEndian(out value);
// Half was introduced in net5.0 but doesn't have a BitConverter implementation until net6.0
#if NET6_0_OR_GREATER
/// <summary>
@@ -2081,6 +2152,22 @@ namespace SabreTools.IO.Extensions
return true;
}
/// <summary>
/// Read a Int32 from the base stream
/// </summary>
/// <remarks>Reads in both-endian format</remarks>
public static bool TryReadInt32BothEndian(this BinaryReader reader, out BothInt32 value)
{
if (reader.BaseStream.Position > reader.BaseStream.Length - 8)
{
value = default(int);
return false;
}
value = reader.ReadInt32BothEndian();
return true;
}
/// <summary>
/// Read a UInt32 from the base stream
/// </summary>
@@ -2125,6 +2212,22 @@ namespace SabreTools.IO.Extensions
return true;
}
/// <summary>
/// Read a UInt32 from the base stream
/// </summary>
/// <remarks>Reads in both-endian format</remarks>
public static bool TryReadUInt32BothEndian(this BinaryReader reader, out BothUInt32 value)
{
if (reader.BaseStream.Position > reader.BaseStream.Length - 8)
{
value = default(uint);
return false;
}
value = reader.ReadUInt32BothEndian();
return true;
}
/// <summary>
/// Read a DWORD (4-byte) from the base stream
/// </summary>
@@ -2146,6 +2249,13 @@ namespace SabreTools.IO.Extensions
public static bool TryReadDWORDLittleEndian(this BinaryReader reader, out uint value)
=> reader.TryReadUInt32LittleEndian(out value);
/// <summary>
/// Read a DWORD (4-byte) from the base stream
/// </summary>
/// <remarks>Reads in both-endian format</remarks>
public static bool TryReadDWORDBothEndian(this BinaryReader reader, out BothUInt32 value)
=> reader.TryReadUInt32BothEndian(out value);
/// <summary>
/// Read a Single from the base stream
/// </summary>
@@ -2310,6 +2420,22 @@ namespace SabreTools.IO.Extensions
return true;
}
/// <summary>
/// Read a Int64 from the base stream
/// </summary>
/// <remarks>Reads in both-endian format</remarks>
public static bool TryReadInt64BothEndian(this BinaryReader reader, out BothInt64 value)
{
if (reader.BaseStream.Position > reader.BaseStream.Length - 16)
{
value = default(long);
return false;
}
value = reader.ReadInt64BothEndian();
return true;
}
/// <summary>
/// Read a UInt64 from the base stream
/// </summary>
@@ -2354,6 +2480,22 @@ namespace SabreTools.IO.Extensions
return true;
}
/// <summary>
/// Read a UInt64 from the base stream
/// </summary>
/// <remarks>Reads in both-endian format</remarks>
public static bool TryReadUInt64BothEndian(this BinaryReader reader, out BothUInt64 value)
{
if (reader.BaseStream.Position > reader.BaseStream.Length - 16)
{
value = default(ulong);
return false;
}
value = reader.ReadUInt64BothEndian();
return true;
}
/// <summary>
/// Read a QWORD (8-byte) from the base stream
/// </summary>
@@ -2375,6 +2517,13 @@ namespace SabreTools.IO.Extensions
public static bool TryReadQWORDLittleEndian(this BinaryReader reader, out ulong value)
=> reader.TryReadUInt64LittleEndian(out value);
/// <summary>
/// Read a QWORD (8-byte) from the base stream
/// </summary>
/// <remarks>Reads in both-endian format</remarks>
public static bool TryReadQWORDBothEndian(this BinaryReader reader, out BothUInt64 value)
=> reader.TryReadUInt64BothEndian(out value);
/// <summary>
/// Read a Double from the base stream
/// </summary>

View File

@@ -1945,6 +1945,22 @@ namespace SabreTools.IO.Extensions
return true;
}
/// <summary>
/// Read a UInt8 and increment the pointer to an array
/// </summary>
/// <remarks>Reads in both-endian format</remarks>
public static bool TryReadByteBothEndian(this byte[] content, ref int offset, out BothUInt8 value)
{
if (offset > content.Length - 2)
{
value = default(byte);
return false;
}
value = content.ReadByteBothEndian(ref offset);
return true;
}
/// <summary>
/// Read a UInt8[] and increment the pointer to an array
/// </summary>
@@ -1975,6 +1991,22 @@ namespace SabreTools.IO.Extensions
return true;
}
/// <summary>
/// Read a Int8 and increment the pointer to an array
/// </summary>
/// <remarks>Reads in both-endian format</remarks>
public static bool TryReadSByteBothEndian(this byte[] content, ref int offset, out BothInt8 value)
{
if (offset > content.Length - 2)
{
value = default(sbyte);
return false;
}
value = content.ReadSByteBothEndian(ref offset);
return true;
}
/// <summary>
/// Read a Char and increment the pointer to an array
/// </summary>
@@ -2034,6 +2066,22 @@ namespace SabreTools.IO.Extensions
return true;
}
/// <summary>
/// Read a Int16 and increment the pointer to an array
/// </summary>
/// <remarks>Reads in both-endian format</remarks>
public static bool TryReadInt16BothEndian(this byte[] content, ref int offset, out BothInt16 value)
{
if (offset > content.Length - 4)
{
value = default(short);
return false;
}
value = content.ReadInt16BothEndian(ref offset);
return true;
}
/// <summary>
/// Read a UInt16 and increment the pointer to an array
/// </summary>
@@ -2078,6 +2126,22 @@ namespace SabreTools.IO.Extensions
return true;
}
/// <summary>
/// Read a UInt16 and increment the pointer to an array
/// </summary>
/// <remarks>Reads in both-endian format</remarks>
public static bool TryReadUInt16BothEndian(this byte[] content, ref int offset, out BothUInt16 value)
{
if (offset > content.Length - 4)
{
value = default(ushort);
return false;
}
value = content.ReadUInt16BothEndian(ref offset);
return true;
}
/// <summary>
/// Read a WORD (2-byte) and increment the pointer to an array
/// </summary>
@@ -2099,6 +2163,13 @@ namespace SabreTools.IO.Extensions
public static bool TryReadWORDLittleEndian(this byte[] content, ref int offset, out ushort value)
=> content.TryReadUInt16LittleEndian(ref offset, out value);
/// <summary>
/// Read a WORD (2-byte) and increment the pointer to an array
/// </summary>
/// <remarks>Reads in both-endian format</remarks>
public static bool TryReadWORDBothEndian(this byte[] content, ref int offset, out BothUInt16 value)
=> content.TryReadUInt16BothEndian(ref offset, out value);
// Half was introduced in net5.0 but doesn't have a BitConverter implementation until net6.0
#if NET6_0_OR_GREATER
/// <summary>
@@ -2266,6 +2337,22 @@ namespace SabreTools.IO.Extensions
return true;
}
/// <summary>
/// Read a Int32 and increment the pointer to an array
/// </summary>
/// <remarks>Reads in both-endian format</remarks>
public static bool TryReadInt32BothEndian(this byte[] content, ref int offset, out BothInt32 value)
{
if (offset > content.Length - 8)
{
value = default(int);
return false;
}
value = content.ReadInt32BothEndian(ref offset);
return true;
}
/// <summary>
/// Read a UInt32 and increment the pointer to an array
/// </summary>
@@ -2310,6 +2397,22 @@ namespace SabreTools.IO.Extensions
return true;
}
/// <summary>
/// Read a UInt32 and increment the pointer to an array
/// </summary>
/// <remarks>Reads in both-endian format</remarks>
public static bool TryReadUInt32BothEndian(this byte[] content, ref int offset, out BothUInt32 value)
{
if (offset > content.Length - 8)
{
value = default(uint);
return false;
}
value = content.ReadUInt32BothEndian(ref offset);
return true;
}
/// <summary>
/// Read a DWORD (4-byte) and increment the pointer to an array
/// </summary>
@@ -2331,6 +2434,13 @@ namespace SabreTools.IO.Extensions
public static bool TryReadDWORDLittleEndian(this byte[] content, ref int offset, out uint value)
=> content.TryReadUInt32LittleEndian(ref offset, out value);
/// <summary>
/// Read a DWORD (4-byte) and increment the pointer to an array
/// </summary>
/// <remarks>Reads in both-endian format</remarks>
public static bool TryReadDWORDBothEndian(this byte[] content, ref int offset, out BothUInt32 value)
=> content.TryReadUInt32BothEndian(ref offset, out value);
/// <summary>
/// Read a Single and increment the pointer to an array
/// </summary>
@@ -2495,6 +2605,22 @@ namespace SabreTools.IO.Extensions
return true;
}
/// <summary>
/// Read a Int64 and increment the pointer to an array
/// </summary>
/// <remarks>Reads in both-endian format</remarks>
public static bool TryReadInt64BothEndian(this byte[] content, ref int offset, out BothInt64 value)
{
if (offset > content.Length - 16)
{
value = default(long);
return false;
}
value = content.ReadInt64BothEndian(ref offset);
return true;
}
/// <summary>
/// Read a UInt64 and increment the pointer to an array
/// </summary>
@@ -2539,6 +2665,22 @@ namespace SabreTools.IO.Extensions
return true;
}
/// <summary>
/// Read a UInt64 and increment the pointer to an array
/// </summary>
/// <remarks>Reads in both-endian format</remarks>
public static bool TryReadUInt64BothEndian(this byte[] content, ref int offset, out BothUInt64 value)
{
if (offset > content.Length - 16)
{
value = default(ulong);
return false;
}
value = content.ReadUInt64BothEndian(ref offset);
return true;
}
/// <summary>
/// Read a QWORD (8-byte) and increment the pointer to an array
/// </summary>
@@ -2560,6 +2702,13 @@ namespace SabreTools.IO.Extensions
public static bool TryReadQWORDLittleEndian(this byte[] content, ref int offset, out ulong value)
=> content.TryReadUInt64LittleEndian(ref offset, out value);
/// <summary>
/// Read a QWORD (8-byte) and increment the pointer to an array
/// </summary>
/// <remarks>Reads in both-endian format</remarks>
public static bool TryReadQWORDBothEndian(this byte[] content, ref int offset, out BothUInt64 value)
=> content.TryReadUInt64BothEndian(ref offset, out value);
/// <summary>
/// Read a Double and increment the pointer to an array
/// </summary>

View File

@@ -1987,6 +1987,22 @@ namespace SabreTools.IO.Extensions
return true;
}
/// <summary>
/// Read a UInt8 from the stream
/// </summary>
/// <remarks>Reads in both-endian format</remarks>
public static bool TryReadByteBothEndian(this Stream stream, out BothUInt8 value)
{
if (stream.Position > stream.Length - 2)
{
value = default(byte);
return false;
}
value = stream.ReadByteBothEndian();
return true;
}
/// <summary>
/// Read a UInt8[] from the stream
/// </summary>
@@ -2017,6 +2033,22 @@ namespace SabreTools.IO.Extensions
return true;
}
/// <summary>
/// Read a Int8 from the stream
/// </summary>
/// <remarks>Reads in both-endian format</remarks>
public static bool TryReadSByteBothEndian(this Stream stream, out BothInt8 value)
{
if (stream.Position > stream.Length - 2)
{
value = default(sbyte);
return false;
}
value = stream.ReadSByteBothEndian();
return true;
}
/// <summary>
/// Read a Char from the stream
/// </summary>
@@ -2076,6 +2108,22 @@ namespace SabreTools.IO.Extensions
return true;
}
/// <summary>
/// Read a Int16 from the stream
/// </summary>
/// <remarks>Reads in both-endian format</remarks>
public static bool TryReadInt16BothEndian(this Stream stream, out BothInt16 value)
{
if (stream.Position > stream.Length - 4)
{
value = default(short);
return false;
}
value = stream.ReadInt16BothEndian();
return true;
}
/// <summary>
/// Read a UInt16 from the stream
/// </summary>
@@ -2120,6 +2168,22 @@ namespace SabreTools.IO.Extensions
return true;
}
/// <summary>
/// Read a UInt16 from the stream
/// </summary>
/// <remarks>Reads in both-endian format</remarks>
public static bool TryReadUInt16BothEndian(this Stream stream, out BothUInt16 value)
{
if (stream.Position > stream.Length - 4)
{
value = default(ushort);
return false;
}
value = stream.ReadUInt16BothEndian();
return true;
}
/// <summary>
/// Read a WORD (2-byte) from the stream
/// </summary>
@@ -2141,6 +2205,13 @@ namespace SabreTools.IO.Extensions
public static bool TryReadWORDLittleEndian(this Stream stream, out ushort value)
=> stream.TryReadUInt16LittleEndian(out value);
/// <summary>
/// Read a WORD (2-byte) from the stream
/// </summary>
/// <remarks>Reads in both-endian format</remarks>
public static bool TryReadWORDBothEndian(this Stream stream, out BothUInt16 value)
=> stream.TryReadUInt16BothEndian(out value);
// Half was introduced in net5.0 but doesn't have a BitConverter implementation until net6.0
#if NET6_0_OR_GREATER
/// <summary>
@@ -2308,6 +2379,22 @@ namespace SabreTools.IO.Extensions
return true;
}
/// <summary>
/// Read a Int32 from the stream
/// </summary>
/// <remarks>Reads in both-endian format</remarks>
public static bool TryReadInt32BothEndian(this Stream stream, out BothInt32 value)
{
if (stream.Position > stream.Length - 8)
{
value = default(int);
return false;
}
value = stream.ReadInt32BothEndian();
return true;
}
/// <summary>
/// Read a UInt32 from the stream
/// </summary>
@@ -2352,6 +2439,22 @@ namespace SabreTools.IO.Extensions
return true;
}
/// <summary>
/// Read a UInt32 from the stream
/// </summary>
/// <remarks>Reads in both-endian format</remarks>
public static bool TryReadUInt32BothEndian(this Stream stream, out BothUInt32 value)
{
if (stream.Position > stream.Length - 8)
{
value = default(uint);
return false;
}
value = stream.ReadUInt32BothEndian();
return true;
}
/// <summary>
/// Read a DWORD (4-byte) from the stream
/// </summary>
@@ -2373,6 +2476,13 @@ namespace SabreTools.IO.Extensions
public static bool TryReadDWORDLittleEndian(this Stream stream, out uint value)
=> stream.TryReadUInt32LittleEndian(out value);
/// <summary>
/// Read a DWORD (4-byte) from the stream
/// </summary>
/// <remarks>Reads in both-endian format</remarks>
public static bool TryReadDWORDBothEndian(this Stream stream, out BothUInt32 value)
=> stream.TryReadUInt32BothEndian(out value);
/// <summary>
/// Read a Single from the stream
/// </summary>
@@ -2537,6 +2647,22 @@ namespace SabreTools.IO.Extensions
return true;
}
/// <summary>
/// Read a Int64 from the stream
/// </summary>
/// <remarks>Reads in both-endian format</remarks>
public static bool TryReadInt64BothEndian(this Stream stream, out BothInt64 value)
{
if (stream.Position > stream.Length - 16)
{
value = default(long);
return false;
}
value = stream.ReadInt64BothEndian();
return true;
}
/// <summary>
/// Read a UInt64 from the stream
/// </summary>
@@ -2581,6 +2707,22 @@ namespace SabreTools.IO.Extensions
return true;
}
/// <summary>
/// Read a UInt64 from the stream
/// </summary>
/// <remarks>Reads in both-endian format</remarks>
public static bool TryReadUInt64BothEndian(this Stream stream, out BothUInt64 value)
{
if (stream.Position > stream.Length - 16)
{
value = default(ulong);
return false;
}
value = stream.ReadUInt64BothEndian();
return true;
}
/// <summary>
/// Read a QWORD (8-byte) from the stream
/// </summary>
@@ -2602,6 +2744,13 @@ namespace SabreTools.IO.Extensions
public static bool TryReadQWORDLittleEndian(this Stream stream, out ulong value)
=> stream.TryReadUInt64LittleEndian(out value);
/// <summary>
/// Read a QWORD (8-byte) from the stream
/// </summary>
/// <remarks>Reads in both-endian format</remarks>
public static bool TryReadQWORDBothEndian(this Stream stream, out BothUInt64 value)
=> stream.TryReadUInt64BothEndian(out value);
/// <summary>
/// Read a Double from the stream
/// </summary>