diff --git a/SabreTools.IO/Extensions/BinaryReaderExtensions.cs b/SabreTools.IO/Extensions/BinaryReaderExtensions.cs index 9fcac99..62c8b01 100644 --- a/SabreTools.IO/Extensions/BinaryReaderExtensions.cs +++ b/SabreTools.IO/Extensions/BinaryReaderExtensions.cs @@ -6,6 +6,9 @@ namespace SabreTools.IO.Extensions /// /// Big endian reading overloads for BinaryReader /// + /// TODO: Add U/Int24 and U/Int48 methods + /// TODO: Add GUID methods + /// TODO: Add U/Int128 methods public static class BinaryReaderExtensions { /// @@ -44,6 +47,77 @@ namespace SabreTools.IO.Extensions return retval; } + /// + /// Reads in big-endian format + public static short ReadInt16BigEndian(this BinaryReader reader) + { + byte[] retval = reader.ReadBytes(2); + Array.Reverse(retval); + return BitConverter.ToInt16(retval, 0); + } + + /// + /// Reads in big-endian format + public static ushort ReadUInt16BigEndian(this BinaryReader reader) + { + byte[] retval = reader.ReadBytes(2); + Array.Reverse(retval); + return BitConverter.ToUInt16(retval, 0); + } + + /// + /// Reads in big-endian format + public static int ReadInt32BigEndian(this BinaryReader reader) + { + byte[] retval = reader.ReadBytes(4); + Array.Reverse(retval); + return BitConverter.ToInt32(retval, 0); + } + + /// Reads in big-endian format + public static uint ReadUInt32BigEndian(this BinaryReader reader) + { + byte[] retval = reader.ReadBytes(4); + Array.Reverse(retval); + return BitConverter.ToUInt32(retval, 0); + } + + /// + /// Reads in big-endian format + public static float ReadSingleBigEndian(this BinaryReader reader) + { + byte[] retval = reader.ReadBytes(4); + Array.Reverse(retval); + return BitConverter.ToSingle(retval, 0); + } + + /// + /// Reads in big-endian format + public static long ReadInt64BigEndian(this BinaryReader reader) + { + byte[] retval = reader.ReadBytes(8); + Array.Reverse(retval); + return BitConverter.ToInt64(retval, 0); + } + + /// + /// Reads in big-endian format + public static ulong ReadUInt64BigEndian(this BinaryReader reader) + { + byte[] retval = reader.ReadBytes(8); + Array.Reverse(retval); + return BitConverter.ToUInt64(retval, 0); + } + + /// + /// Reads in big-endian format + public static double ReadDoubleBigEndian(this BinaryReader reader) + { + byte[] retval = reader.ReadBytes(8); + Array.Reverse(retval); + return BitConverter.ToDouble(retval, 0); + } + /// /// Reads in big-endian format public static decimal ReadDecimalBigEndian(this BinaryReader reader) @@ -58,76 +132,5 @@ namespace SabreTools.IO.Extensions return new decimal([i1, i2, i3, i4]); } - - /// - /// Reads in big-endian format - public static double ReadDoubleBigEndian(this BinaryReader reader) - { - byte[] retval = reader.ReadBytes(8); - Array.Reverse(retval); - return BitConverter.ToDouble(retval, 0); - } - - /// - /// Reads in big-endian format - public static short ReadInt16BigEndian(this BinaryReader reader) - { - byte[] retval = reader.ReadBytes(2); - Array.Reverse(retval); - return BitConverter.ToInt16(retval, 0); - } - - /// - /// Reads in big-endian format - public static int ReadInt32BigEndian(this BinaryReader reader) - { - byte[] retval = reader.ReadBytes(4); - Array.Reverse(retval); - return BitConverter.ToInt32(retval, 0); - } - - /// - /// Reads in big-endian format - public static long ReadInt64BigEndian(this BinaryReader reader) - { - byte[] retval = reader.ReadBytes(8); - Array.Reverse(retval); - return BitConverter.ToInt64(retval, 0); - } - - /// - /// Reads in big-endian format - public static float ReadSingleBigEndian(this BinaryReader reader) - { - byte[] retval = reader.ReadBytes(4); - Array.Reverse(retval); - return BitConverter.ToSingle(retval, 0); - } - - /// - /// Reads in big-endian format - public static ushort ReadUInt16BigEndian(this BinaryReader reader) - { - byte[] retval = reader.ReadBytes(2); - Array.Reverse(retval); - return BitConverter.ToUInt16(retval, 0); - } - - /// Reads in big-endian format - public static uint ReadUInt32BigEndian(this BinaryReader reader) - { - byte[] retval = reader.ReadBytes(4); - Array.Reverse(retval); - return BitConverter.ToUInt32(retval, 0); - } - - /// - /// Reads in big-endian format - public static ulong ReadUInt64BigEndian(this BinaryReader reader) - { - byte[] retval = reader.ReadBytes(8); - Array.Reverse(retval); - return BitConverter.ToUInt64(retval, 0); - } } } diff --git a/SabreTools.IO/Extensions/ByteArrayExtensions.cs b/SabreTools.IO/Extensions/ByteArrayExtensions.cs index 1355ada..519f294 100644 --- a/SabreTools.IO/Extensions/ByteArrayExtensions.cs +++ b/SabreTools.IO/Extensions/ByteArrayExtensions.cs @@ -31,6 +31,17 @@ namespace SabreTools.IO.Extensions public static byte[] ReadBytes(this byte[] content, ref int offset, int count) => ReadToBuffer(content, ref offset, count); + /// + /// Read a UInt8[] and increment the pointer to an array + /// + /// Reads in big-endian format + public static byte[] ReadBytesBigEndian(this byte[] content, ref int offset, int count) + { + byte[] buffer = ReadToBuffer(content, ref offset, count); + Array.Reverse(buffer); + return buffer; + } + /// /// Read an Int8 and increment the pointer to an array /// @@ -59,8 +70,9 @@ namespace SabreTools.IO.Extensions } /// - /// Read an Int16 in big-endian format and increment the pointer to an array + /// Read an Int16 and increment the pointer to an array /// + /// Reads in big-endian format public static short ReadInt16BigEndian(this byte[] content, ref int offset) { byte[] buffer = ReadToBuffer(content, ref offset, 2); @@ -78,8 +90,9 @@ namespace SabreTools.IO.Extensions } /// - /// Read a UInt16 in big-endian format and increment the pointer to an array + /// Read a UInt16 and increment the pointer to an array /// + /// Reads in big-endian format public static ushort ReadUInt16BigEndian(this byte[] content, ref int offset) { byte[] buffer = ReadToBuffer(content, ref offset, 2); @@ -97,8 +110,9 @@ namespace SabreTools.IO.Extensions } /// - /// Read an Int32 in big-endian format and increment the pointer to an array + /// Read an Int32 and increment the pointer to an array /// + /// Reads in big-endian format public static int ReadInt32BigEndian(this byte[] content, ref int offset) { byte[] buffer = ReadToBuffer(content, ref offset, 4); @@ -116,8 +130,9 @@ namespace SabreTools.IO.Extensions } /// - /// Read a UInt32 in big-endian format and increment the pointer to an array + /// Read a UInt32 and increment the pointer to an array /// + /// Reads in big-endian format public static uint ReadUInt32BigEndian(this byte[] content, ref int offset) { byte[] buffer = ReadToBuffer(content, ref offset, 4); @@ -135,8 +150,9 @@ namespace SabreTools.IO.Extensions } /// - /// Read a Single in big-endian format and increment the pointer to an array + /// Read a Single and increment the pointer to an array /// + /// Reads in big-endian format public static float ReadSingleBigEndian(this byte[] content, ref int offset) { byte[] buffer = ReadToBuffer(content, ref offset, 4); @@ -154,8 +170,9 @@ namespace SabreTools.IO.Extensions } /// - /// Read an Int64 in big-endian format and increment the pointer to an array + /// Read an Int64 and increment the pointer to an array /// + /// Reads in big-endian format public static long ReadInt64BigEndian(this byte[] content, ref int offset) { byte[] buffer = ReadToBuffer(content, ref offset, 8); @@ -173,8 +190,9 @@ namespace SabreTools.IO.Extensions } /// - /// Read a UInt64 in big-endian format and increment the pointer to an array + /// Read a UInt64 and increment the pointer to an array /// + /// Reads in big-endian format public static ulong ReadUInt64BigEndian(this byte[] content, ref int offset) { byte[] buffer = ReadToBuffer(content, ref offset, 8); @@ -192,8 +210,9 @@ namespace SabreTools.IO.Extensions } /// - /// Read a Double in big-endian format and increment the pointer to an array + /// Read a Double and increment the pointer to an array /// + /// Reads in big-endian format public static double ReadDoubleBigEndian(this byte[] content, ref int offset) { byte[] buffer = ReadToBuffer(content, ref offset, 8); @@ -211,8 +230,9 @@ namespace SabreTools.IO.Extensions } /// - /// Read a Guid in big-endian format and increment the pointer to an array + /// Read a Guid and increment the pointer to an array /// + /// Reads in big-endian format public static Guid ReadGuidBigEndian(this byte[] content, ref int offset) { byte[] buffer = ReadToBuffer(content, ref offset, 16); @@ -232,8 +252,9 @@ namespace SabreTools.IO.Extensions } /// - /// Read an Int128 in big-endian format and increment the pointer to an array + /// Read an Int128 and increment the pointer to an array /// + /// Reads in big-endian format public static Int128 ReadInt128BigEndian(this byte[] content, ref int offset) { byte[] buffer = ReadToBuffer(content, ref offset, 16); @@ -251,8 +272,9 @@ namespace SabreTools.IO.Extensions } /// - /// Read a UInt128 in big-endian format and increment the pointer to an array + /// Read a UInt128 and increment the pointer to an array /// + /// Reads in big-endian format public static UInt128 ReadUInt128BigEndian(this byte[] content, ref int offset) { byte[] buffer = ReadToBuffer(content, ref offset, 16);