| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549 | using NPOI.SS.Formula.Functions;using System;using System.Collections;using System.Collections.Generic;using System.Collections.Immutable;using System.Linq;using System.Text;namespace InABox.DigitalMatter{    public interface IDMBuffer    {        /// <summary>        /// The remaining size of the buffer.        /// </summary>        public ushort BufferSize { get; }        #region Bit Manipulation        private static bool[] DecodeBits(byte[] value, int index, int length)        {            var result = new List<bool>();            var array = new BitArray(value);            for (var i = index; i < index + length && i < array.Count; i++)                result.Add(array[i]);            return result.ToArray();        }        private static byte[] EncodeBits(byte[] source, int index, bool[] bits)        {            var array = new BitArray(source);            for (var i = 0; i < bits.Length; i++)                array.Set(index + i, bits[i]);            var result = new byte[source.Length];            array.CopyTo(result, 0);            return result;        }        public static bool[] GetBits(byte[] value, int index, int length)        {            return DecodeBits(value, index, length);        }        public static byte[] SetBits(byte[] value, int index, bool[] bits)        {            return EncodeBits(value, index, bits);        }        public static bool[] DecodeByte(byte value, int index, int length)        {            return DecodeBits(new[] { value }, index, length);        }        public static bool[] DecodeBytes(byte[] value, int index, int length)        {            return DecodeBits(value, index, length);        }        public static bool[] DecodeInt8(sbyte value, int index, int length)        {            return DecodeBits(new[] { Convert.ToByte(value) }, index, length);        }        public static bool[] DecodeInt16(short value, int index, int length)        {            return DecodeBits(BitConverter.GetBytes(value), index, length);        }        public static bool[] DecodeUInt16(ushort value, int index, int length)        {            return DecodeBits(BitConverter.GetBytes(value), index, length);        }        public static bool[] DecodeInt32(int value, int index, int length)        {            return DecodeBits(BitConverter.GetBytes(value), index, length);        }        public static bool[] DecodeUInt32(uint value, int index, int length)        {            return DecodeBits(BitConverter.GetBytes(value), index, length);        }        public static byte EncodeByte(bool[] bits)        {            return EncodeBits(new byte[] { default }, 0, bits).First();        }        public static byte[] EncodeBytes(bool[] bits, int size)        {            return EncodeBits(Array.CreateInstance(typeof(byte), size).OfType<byte>().ToArray(), 0, bits);        }        public static sbyte EncodeInt8(bool[] bits)        {            return Convert.ToSByte(EncodeBits(new[] { Convert.ToByte(default(sbyte)) }, 0, bits).First());        }        public static short EncodeInt16(bool[] bits)        {            return BitConverter.ToInt16(EncodeBits(BitConverter.GetBytes(default(short)), 0, bits), 0);        }        public static ushort EncodeUInt16(bool[] bits)        {            return BitConverter.ToUInt16(EncodeBits(BitConverter.GetBytes(default(ushort)), 0, bits), 0);        }        public static int EncodeInt32(bool[] bits)        {            return BitConverter.ToInt32(EncodeBits(BitConverter.GetBytes(default(int)), 0, bits), 0);        }        public static uint EncodeUInt32(bool[] bits)        {            return BitConverter.ToUInt32(EncodeBits(BitConverter.GetBytes(default(uint)), 0, bits), 0);        }        public static byte UpdateByte(byte value, int index, bool[] bits)        {            return EncodeBits(new[] { value }, index, bits).First();        }        public static byte[] UpdateBytes(byte[] value, int index, bool[] bits, int size)        {            return EncodeBits(value, 0, bits);        }        public static sbyte UpdateInt8(sbyte value, int index, bool[] bits)        {            return Convert.ToSByte(EncodeBits(new[] { Convert.ToByte(value) }, 0, bits).First());        }        public static short UpdateInt16(short value, int index, bool[] bits)        {            return BitConverter.ToInt16(EncodeBits(BitConverter.GetBytes(value), 0, bits), 0);        }        public static ushort UpdateUInt16(ushort value, int index, bool[] bits)        {            return BitConverter.ToUInt16(EncodeBits(BitConverter.GetBytes(value), 0, bits), 0);        }        public static int UpdateInt32(int value, int index, bool[] bits)        {            return BitConverter.ToInt32(EncodeBits(BitConverter.GetBytes(value), 0, bits), 0);        }        public static uint UpdateUInt32(uint value, int index, bool[] bits)        {            return BitConverter.ToUInt32(EncodeBits(BitConverter.GetBytes(value), 0, bits), 0);        }        #endregion    }    public interface IDMReadBuffer : IDMBuffer    {        /// <summary>        /// Read <paramref name="length"/> bytes, starting at <paramref name="index"/>, but don't advance the read pointer.        /// </summary>        /// <param name="index">Index to start the read.</param>        /// <param name="length">Number of bytes to read.</param>        /// <returns>The bytes read.</returns>        protected byte[] Peek(int index, int length);        /// <summary>        /// Read <paramref name="length"/> bytes, advancing the read pointer by <paramref name="length"/>.        /// </summary>        /// <param name="length">Number of bytes to read.</param>        /// <returns>The bytes read.</returns>        protected byte[] Take(int length);        public byte PeekByte(int index)        {            return Peek(index, sizeof(byte)).First();        }        public byte[] PeekBytes(int index, int length)        {            return Peek(index, length);        }        public sbyte PeekInt8(int index)        {            return unchecked((sbyte)PeekByte(index));            //hmmm...        }        public ushort PeekUInt16(int index)        {            return BitConverter.ToUInt16(Peek(index, sizeof(ushort)), 0);        }        public short PeekInt16(int index)        {            return BitConverter.ToInt16(Peek(index, sizeof(short)), 0);        }        public uint PeekUInt32(int index)        {            return BitConverter.ToUInt32(Peek(index, sizeof(uint)), 0);        }        public int PeekInt32(int index)        {            return BitConverter.ToInt32(Peek(index, sizeof(int)), 0);        }        public string PeekString(int index, int length)        {            return Encoding.Default.GetString(Peek(index, length)).Trim();        }        public byte TakeByte()        {            return Take(sizeof(byte)).First();        }        public byte[] TakeBytes(int length)        {            return Take(length);        }        public sbyte TakeInt8()        {            return unchecked((sbyte)TakeByte());            //hmmm...        }        public ushort TakeUInt16()        {            return BitConverter.ToUInt16(Take(sizeof(ushort)), 0);        }        public short TakeInt16()        {            return BitConverter.ToInt16(Take(sizeof(short)), 0);        }        public uint TakeUInt32()        {            return BitConverter.ToUInt32(Take(sizeof(uint)), 0);        }        public int TakeInt32()        {            return BitConverter.ToInt32(Take(sizeof(int)), 0);        }        public string TakeString(int length)        {            return Encoding.Default.GetString(Take(length)).Trim();        }    }    public interface IDMWriteBuffer : IDMBuffer    {        protected ushort Add(byte[] bytes);        protected ushort Insert(int index, byte[] bytes);        void Reset();        public ushort AddByte(byte value)        {            return Add(new[] { value });        }        public ushort AddBytes(byte[] value)        {            return Add(value);        }        public ushort AddInt8(sbyte value)        {            return Add(BitConverter.GetBytes((short)value));        }        public ushort AddUInt16(ushort value)        {            return Add(BitConverter.GetBytes(value));        }        public ushort AddInt16(short value)        {            return Add(BitConverter.GetBytes(value));        }        public ushort AddUInt32(uint value)        {            return Add(BitConverter.GetBytes(value));        }        public ushort AddInt32(int value)        {            return Add(BitConverter.GetBytes(value));        }        public ushort AddString(string value, int length)        {            return Add(FixedLengthString(value, length));        }        public ushort InsertByte(int index, byte value)        {            return Insert(index, new[] { value });        }        public ushort InsertBytes(int index, byte[] value)        {            return Insert(index, value);        }        public ushort InsertInt8(int index, sbyte value)        {            return Insert(index, BitConverter.GetBytes((short)value));        }        public ushort InsertUInt16(int index, ushort value)        {            return Insert(index, BitConverter.GetBytes(value));        }        public ushort InsertInt16(int index, short value)        {            return Insert(index, BitConverter.GetBytes(value));        }        public ushort InsertUInt32(int index, uint value)        {            return Insert(index, BitConverter.GetBytes(value));        }        public ushort InsertInt32(int index, int value)        {            return Insert(index, BitConverter.GetBytes(value));        }        public ushort InsertString(int index, string value, int length)        {            return Insert(index, FixedLengthString(value, length));        }        #region Private Members        private static byte[] FixedLengthString(string value, int length)        {            var result = Encoding.Default.GetBytes(value).ToList();            while (result.Count < length)                result.Add(0x00);            return result.ToArray();        }        #endregion    }    public class DMArrayReadBuffer : IDMReadBuffer    {        private byte[] _buffer { get; set; }        private int _offset;        public ushort BufferSize => (ushort)(_buffer.Length - _offset);        public DMArrayReadBuffer(byte[] buffer)        {            _buffer = buffer;            _offset = 0;        }        byte[] IDMReadBuffer.Peek(int index, int length)        {            index += _offset;            length = Math.Min(length, _buffer.Length - index);            var newBytes = new byte[length];            Buffer.BlockCopy(_buffer, index, newBytes, 0, length);            return newBytes;        }        byte[] IDMReadBuffer.Take(int length)        {            length = Math.Min(length, _buffer.Length - _offset);            var newBytes = new byte[length];            Buffer.BlockCopy(_buffer, _offset, newBytes, 0, length);            _offset += length;            return newBytes;        }    }    public class DMListReadBuffer : IDMReadBuffer    {        private List<byte> _buffer { get; set; }        private int _offset;        public ushort BufferSize => (ushort)(_buffer.Count - _offset);        public DMListReadBuffer(List<byte> buffer)        {            _buffer = buffer;            _offset = 0;        }        byte[] IDMReadBuffer.Peek(int index, int length) => Peek(index, length);        private byte[] Peek(int index, int length)        {            index += _offset;            length = Math.Min(length, _buffer.Count - index);            var newBytes = new byte[length];            _buffer.CopyTo(index, newBytes, 0, length);            return newBytes;        }                byte[] IDMReadBuffer.Take(int length)        {            var result = Peek(0, length);            _offset += length;            return result;        }    }    public class DMPartialReadBuffer : IDMReadBuffer    {        private ushort _offset;        private ushort _length;        public IDMReadBuffer Buffer { get; }        public ushort BufferSize => (ushort)(_length - (Offset - _offset));        public ushort Offset { get; private set; }        public DMPartialReadBuffer(IDMReadBuffer buffer, ushort offset, ushort length)        {            _length = length;            _offset = offset;            Offset = offset;            Buffer = buffer;        }        byte[] IDMReadBuffer.Peek(int index, int length) => Peek(index, length);        private byte[] Peek(int index, int length)        {            index += Offset;            length = Math.Min(length, _offset + _length - index);            return Buffer.PeekBytes(index, length);        }        byte[] IDMReadBuffer.Take(int length)        {            var bytes = Peek(0, length);            Offset += (ushort)bytes.Length;            return bytes;        }    }    public class DMListWriteBuffer : IDMWriteBuffer    {        private List<byte> _buffer { get; set; }        private int _offset;        public List<byte> Buffer => _buffer;        public ushort BufferSize => (ushort)(_buffer.Count - _offset);        public DMListWriteBuffer(List<byte> buffer)        {            _buffer = buffer;            _offset = 0;        }        public void Reset()        {            _buffer.Clear();        }        ushort IDMWriteBuffer.Add(byte[] bytes)        {            _buffer.AddRange(bytes);            return BufferSize;        }        ushort IDMWriteBuffer.Insert(int index, byte[] bytes)        {            _buffer.InsertRange(index, bytes);            return BufferSize;        }    }    public class DMBuffer : IDMReadBuffer, IDMWriteBuffer    {        public byte[] Buffer        {            get => _buffer.ToArray();            set => _buffer = value.ToList();        }        public DMBuffer() { }        public DMBuffer(byte[] buffer)        {            Buffer = buffer;        }        public DMBuffer(List<byte> buffer)        {            _buffer = buffer;        }        public ushort BufferSize => (ushort)_buffer.Count;        public void Reset()        {            _buffer = new List<byte>();        }        #region Private Members        private List<byte> _buffer = new();        ushort IDMWriteBuffer.Add(byte[] bytes)        {            _buffer.AddRange(bytes);            return BufferSize;        }        ushort IDMWriteBuffer.Insert(int index, byte[] bytes)        {            _buffer.InsertRange(index, bytes);            return BufferSize;        }        private byte[] Peek(int index, int length)        {            return _buffer.Skip(index).Take(length).ToArray();        }        byte[] IDMReadBuffer.Peek(int index, int length) => Peek(index, length);        byte[] IDMReadBuffer.Take(int length)        {            var result = Peek(0, length);            _buffer.RemoveRange(0, length);            return result;        }        #endregion    }}
 |