{#/* This Source Code Form is subject to the terms of the Mozilla Public * License, v. 2.0. If a copy of the MPL was not distributed with this * file, You can obtain one at http://mozilla.org/MPL/2.0/. */#} // Big endian streams are not yet available in dotnet :'( // https://github.com/dotnet/runtime/issues/26904 class StreamUnderflowException: Exception { public StreamUnderflowException() { } } class BigEndianStream { Stream stream; public BigEndianStream(Stream stream) { this.stream = stream; } public bool HasRemaining() { return (stream.Length - stream.Position) > 0; } public long Position { get => stream.Position; set => stream.Position = value; } public void WriteBytes(byte[] value) { stream.Write(value, 0, value.Length); } public void WriteByte(byte value) { stream.WriteByte(value); } public void WriteUShort(ushort value) { stream.WriteByte((byte)(value >> 8)); stream.WriteByte((byte)value); } public void WriteUInt(uint value) { stream.WriteByte((byte)(value >> 24)); stream.WriteByte((byte)(value >> 16)); stream.WriteByte((byte)(value >> 8)); stream.WriteByte((byte)value); } public void WriteULong(ulong value) { WriteUInt((uint)(value >> 32)); WriteUInt((uint)value); } public void WriteSByte(sbyte value) { stream.WriteByte((byte)value); } public void WriteShort(short value) { WriteUShort((ushort)value); } public void WriteInt(int value) { WriteUInt((uint)value); } public void WriteFloat(float value) { WriteInt(BitConverter.SingleToInt32Bits(value)); } public void WriteLong(long value) { WriteULong((ulong)value); } public void WriteDouble(double value) { WriteLong(BitConverter.DoubleToInt64Bits(value)); } public byte[] ReadBytes(int length) { CheckRemaining(length); byte[] result = new byte[length]; stream.Read(result, 0, length); return result; } public byte ReadByte() { CheckRemaining(1); return Convert.ToByte(stream.ReadByte()); } public ushort ReadUShort() { CheckRemaining(2); return (ushort)(stream.ReadByte() << 8 | stream.ReadByte()); } public uint ReadUInt() { CheckRemaining(4); return (uint)(stream.ReadByte() << 24 | stream.ReadByte() << 16 | stream.ReadByte() << 8 | stream.ReadByte()); } public ulong ReadULong() { return (ulong)ReadUInt() << 32 | (ulong)ReadUInt(); } public sbyte ReadSByte() { return (sbyte)ReadByte(); } public short ReadShort() { return (short)ReadUShort(); } public int ReadInt() { return (int)ReadUInt(); } public float ReadFloat() { return BitConverter.Int32BitsToSingle(ReadInt()); } public long ReadLong() { return (long)ReadULong(); } public double ReadDouble() { return BitConverter.Int64BitsToDouble(ReadLong()); } private void CheckRemaining(int length) { if (stream.Length - stream.Position < length) { throw new StreamUnderflowException(); } } }