From a5e95294a0f9385c0227714f3f439e7e42b4e3ae Mon Sep 17 00:00:00 2001 From: Mikhail Yakshin Date: Tue, 9 Aug 2016 12:28:21 +0300 Subject: [PATCH] Added nested regions --- KaitaiStream.cs | 104 +++++++++++++++++++++++++++++++----------------- 1 file changed, 68 insertions(+), 36 deletions(-) diff --git a/KaitaiStream.cs b/KaitaiStream.cs index 9ac440d..53d1ff6 100644 --- a/KaitaiStream.cs +++ b/KaitaiStream.cs @@ -83,49 +83,57 @@ public long Size #region Integer types + #region Signed + /// - /// Read an unsigned byte from the stream + /// Read a signed byte from the stream /// /// - public byte ReadU1() + public sbyte ReadS1() { - return ReadByte(); + return ReadSByte(); } + #region Big-endian + /// - /// Read a signed byte from the stream + /// Read a signed short from the stream (big endian) /// /// - public sbyte ReadS1() + public short ReadS2be() { - return ReadSByte(); + return BitConverter.ToInt16(ReadBytesNormalisedBigEndian(2), 0); } /// - /// Read an unsigned short from the stream (little endian) + /// Read a signed int from the stream (big endian) /// /// - public ushort ReadU2le() + public int ReadS4be() { - return BitConverter.ToUInt16(ReadBytesNormalisedLittleEndian(2), 0); + return BitConverter.ToInt32(ReadBytesNormalisedBigEndian(4), 0); } /// - /// Read a signed short from the stream (little endian) + /// Read a signed long from the stream (big endian) /// /// - public short ReadS2le() + public long ReadS8be() { - return BitConverter.ToInt16(ReadBytesNormalisedLittleEndian(2), 0); + return BitConverter.ToInt64(ReadBytesNormalisedBigEndian(8), 0); } + #endregion + + #region Little-endian + /// - /// Read an unsigned int from the stream (little endian) + /// Read a signed short from the stream (little endian) /// /// - public uint ReadU4le() + public short ReadS2le() { - return BitConverter.ToUInt32(ReadBytesNormalisedLittleEndian(4), 0); + return BitConverter.ToInt16(ReadBytesNormalisedLittleEndian(2), 0); } /// @@ -138,23 +146,31 @@ public int ReadS4le() } /// - /// Read an unsigned long from the stream (little endian) + /// Read a signed long from the stream (little endian) /// /// - public ulong ReadU8le() + public long ReadS8le() { - return BitConverter.ToUInt64(ReadBytesNormalisedLittleEndian(8), 0); + return BitConverter.ToInt64(ReadBytesNormalisedLittleEndian(8), 0); } + #endregion + + #endregion + + #region Unsigned + /// - /// Read a signed long from the stream (little endian) + /// Read an unsigned byte from the stream /// /// - public long ReadS8le() + public byte ReadU1() { - return BitConverter.ToInt64(ReadBytesNormalisedLittleEndian(8), 0); + return ReadByte(); } + #region Big-endian + /// /// Read an unsigned short from the stream (big endian) /// @@ -165,54 +181,64 @@ public ushort ReadU2be() } /// - /// Read a signed short from the stream (big endian) + /// Read an unsigned int from the stream (big endian) /// /// - public short ReadS2be() + public uint ReadU4be() { - return BitConverter.ToInt16(ReadBytesNormalisedBigEndian(2), 0); + return BitConverter.ToUInt32(ReadBytesNormalisedBigEndian(4), 0); } /// - /// Read an unsigned int from the stream (big endian) + /// Read an unsigned long from the stream (big endian) /// /// - public uint ReadU4be() + public ulong ReadU8be() { - return BitConverter.ToUInt32(ReadBytesNormalisedBigEndian(4), 0); + return BitConverter.ToUInt64(ReadBytesNormalisedBigEndian(8), 0); } + #endregion + + #region Little-endian + /// - /// Read a signed int from the stream (big endian) + /// Read an unsigned short from the stream (little endian) /// /// - public int ReadS4be() + public ushort ReadU2le() { - return BitConverter.ToInt32(ReadBytesNormalisedBigEndian(4), 0); + return BitConverter.ToUInt16(ReadBytesNormalisedLittleEndian(2), 0); } /// - /// Read an unsigned long from the stream (big endian) + /// Read an unsigned int from the stream (little endian) /// /// - public ulong ReadU8be() + public uint ReadU4le() { - return BitConverter.ToUInt64(ReadBytesNormalisedBigEndian(8), 0); + return BitConverter.ToUInt32(ReadBytesNormalisedLittleEndian(4), 0); } /// - /// Read a signed long from the stream (big endian) + /// Read an unsigned long from the stream (little endian) /// /// - public long ReadS8be() + public ulong ReadU8le() { - return BitConverter.ToInt64(ReadBytesNormalisedBigEndian(8), 0); + return BitConverter.ToUInt64(ReadBytesNormalisedLittleEndian(8), 0); } #endregion + #endregion + + #endregion + #region Floating point types + #region Big-endian + /// /// Read a single-precision floating point value from the stream (big endian) /// @@ -231,6 +257,10 @@ public double ReadF8be() return BitConverter.ToDouble(ReadBytesNormalisedBigEndian(8), 0); } + #endregion + + #region Little-endian + /// /// Read a single-precision floating point value from the stream (little endian) /// @@ -251,6 +281,8 @@ public double ReadF8le() #endregion + #endregion + #region Byte arrays ///