Пример #1
0
        public static byte[] Construct(ushort Header, HDestinations Destination, HProtocols Protocol, params object[] Chunks)
        {
            if (Protocol == HProtocols.Unknown)
            {
                throw new Exception("You must specify a supported HProtocols value for this method. ( Ancient / Modern )");
            }
            if (Protocol == HProtocols.Ancient && Destination == HDestinations.Unknown)
            {
                throw new Exception("Cannot construct the body of an Ancient type packet without a valid HDestinations value. ( Client / Server )");
            }

            List <byte> Buffer    = new List <byte>();
            bool        IsAncient = Protocol == HProtocols.Ancient;

            if (IsAncient && Destination == HDestinations.Server)
            {
                Buffer.Add(64);
            }
            Buffer.AddRange(IsAncient ? Ancient.CypherShort(Header) : BigEndian.CypherShort(Header));

            Buffer.AddRange(ConstructBody(Destination, Protocol, Chunks));

            if (!IsAncient || Destination == HDestinations.Server)
            {
                Buffer.InsertRange(IsAncient ? 1 : 0, IsAncient ? Ancient.CypherShort((ushort)(Buffer.Count - 1)) : BigEndian.CypherInt(Buffer.Count));
            }
            else if (Buffer[Buffer.Count - 1] != 1)
            {
                Buffer.Add(1);
            }

            return(Buffer.ToArray());
        }
Пример #2
0
        protected ushort ReadShort(ref int index, HProtocol protocol)
        {
            if (index >= Body.Length || index + 2 > Body.Length)
            {
                return(0);
            }

            var chunk = new byte[] { Body[index++], Body[index++] };

            return(Protocol == HProtocol.Ancient ? Ancient.DecypherShort(chunk) : Modern.DecypherShort(chunk));
        }
Пример #3
0
        public int ReadShort(ref int Index)
        {
            if (IsCorrupted)
            {
                return(0);
            }
            switch (Protocol)
            {
            case HProtocols.Modern: return(BigEndian.DecypherShort(Body[Index++], Body[Index++]));

            case HProtocols.Ancient: return(Ancient.DecypherShort(Body[Index++], Body[Index++]));

            default: return(0);
            }
        }
Пример #4
0
        public int ReadInt(ref int Index)
        {
            if (IsCorrupted)
            {
                return(0);
            }
            switch (Protocol)
            {
            case HProtocols.Modern: return(BigEndian.DecypherInt(Body[Index++], Body[Index++], Body[Index++], Body[Index++]));

            case HProtocols.Ancient:
            {
                int Value = Ancient.DecypherInt(Body, Index);
                Index += Ancient.CypherInt(Value).Length;
                return(Value);
            }

            default: return(0);
            }
        }
Пример #5
0
        protected int ReadInt(ref int index, HProtocol protocol)
        {
            if (index >= Body.Length)
            {
                return(0);
            }

            switch (Protocol)
            {
            case HProtocol.Modern:
            {
                if (index + 4 > Body.Length)
                {
                    return(0);
                }
                return(Modern.DecypherInt(Body[index++], Body[index++], Body[index++], Body[index++]));
            }

            case HProtocol.Ancient:
            {
                int length = (Body[index] >> 3) & 7;
                if (length < 1)
                {
                    length++;
                }
                if (index + length > Body.Length)
                {
                    return(0);
                }

                int value = Ancient.DecypherInt(Body, index);
                index += length;

                return(value);
            }

            default: return(0);
            }
        }
Пример #6
0
        public static byte[] Construct(ushort header, HDestination destination, HProtocol protocol, params object[] chunks)
        {
            var  buffer    = new List <byte>();
            bool isAncient = (protocol == HProtocol.Ancient);

            if (isAncient && destination == HDestination.Server)
            {
                buffer.Add(64);
            }
            buffer.AddRange(protocol == HProtocol.Ancient ? Ancient.CypherShort(header) : Modern.CypherShort(header));

            buffer.AddRange(ConstructBody(destination, protocol, chunks));

            if (!isAncient || destination == HDestination.Server)
            {
                buffer.InsertRange(isAncient ? 1 : 0, isAncient ? Ancient.CypherShort((ushort)(buffer.Count - 1)) : Modern.CypherInt(buffer.Count));
            }
            else if (buffer[buffer.Count - 1] != 1)
            {
                buffer.Add(1);
            }

            return(buffer.ToArray());
        }
Пример #7
0
        public HMessage(byte[] data, HDestination destination)
            : this()
        {
            if (data == null)
            {
                throw new NullReferenceException();
            }
            if (data.Length < 1)
            {
                throw new Exception("The minimum amount of bytes required to initialize an HMessage instance is 1(One). If the amount of bytes passed is < 3(Three), and >= 1(One), it will be immediately be identified as a corrupted packet. { IsCorrupted = true }");
            }

            Destination = destination;
            bool hasByteZero     = data.Contains(byte.MinValue);
            bool isAncientHeader = !hasByteZero && data.Length == 2 && data[1] != 1;

            if (!isAncientHeader && data.Length >= 6 && Modern.DecypherInt(data) == data.Length - 4)
            {
                Protocol = HProtocol.Modern;

                _header = Modern.DecypherShort(data, 4);
                Append(ByteUtils.CopyBlock(data, 4, data.Length - 4));

                if (data.Length == 6)
                {
                    _logWriting = true;
                }
            }
            else if ((destination == HDestination.Server && isAncientHeader) || (!hasByteZero && data.Length >= 5 && Ancient.DecypherShort(data, 1) == data.Length - 3))
            {
                Destination = HDestination.Server;
                Protocol    = HProtocol.Ancient;

                _header = Ancient.DecypherShort(data, isAncientHeader ? 0 : 3);
                Append(isAncientHeader ? data : ByteUtils.CopyBlock(data, 3, data.Length - 3));

                if (data.Length == 5 || isAncientHeader)
                {
                    _logWriting = true;
                }
            }
            else if (isAncientHeader || (!hasByteZero && data.Length >= 3 && data[data.Length - 1] == 1 && Destination != HDestination.Server))
            {
                Destination = HDestination.Client;
                Protocol    = HProtocol.Ancient;

                if (isAncientHeader)
                {
                    data = new byte[] { data[0], data[1], 1 }
                }
                ;
                _header = Ancient.DecypherShort(data);
                Append(data);

                if (data.Length == 3 || isAncientHeader)
                {
                    _logWriting = true;
                }
            }
            else
            {
                Body         = data;
                _bufferCache = data;
                IsCorrupted  = true;
                Length       = data.Length;
                _buffer.AddRange(data);
                _stringCache = ToString(data);
            }
        }
Пример #8
0
        public static byte[] ConstructBody(HDestination destination, HProtocol protocol, params object[] chunks)
        {
            var  buffer    = new List <byte>();
            bool isAncient = (protocol == HProtocol.Ancient);

            for (int i = 0; i < chunks.Length; i++)
            {
                object chunk = chunks[i];
                if (chunk == null)
                {
                    throw new NullReferenceException(string.Format("Unable to encode a null object. {{ Index = {0} }}", i));
                }

                var data = chunk as byte[];
                if (data != null)
                {
                    buffer.AddRange(data);
                }
                else
                {
                    switch (Type.GetTypeCode(chunk.GetType()))
                    {
                    case TypeCode.Int32:
                    {
                        var value = (int)chunk;
                        buffer.AddRange(protocol == HProtocol.Ancient ? Ancient.CypherInt(value) : Modern.CypherInt(value));
                        break;
                    }

                    case TypeCode.Boolean:
                    {
                        var value = (bool)chunk;
                        buffer.Add(isAncient ? (byte)(value ? 73 : 72) : Convert.ToByte(value));
                        break;
                    }

                    case TypeCode.Byte:
                    {
                        var value = (byte)chunk;
                        buffer.Add(value);
                        break;
                    }

                    default:
                    {
                        string value = chunk.ToString();
                        if (!isAncient || destination == HDestination.Server)
                        {
                            ushort valueLength = (ushort)value.Length;
                            buffer.AddRange(protocol == HProtocol.Ancient ? Ancient.CypherShort(valueLength) : Modern.CypherShort(valueLength));
                            buffer.AddRange(Encoding.Default.GetBytes(value));
                        }
                        else
                        {
                            buffer.AddRange(Encoding.Default.GetBytes(value));
                            buffer.Add(2);
                        }
                        break;
                    }
                    }
                }
            }
            return(buffer.ToArray());
        }
Пример #9
0
        public static byte[][] Split(ref byte[] Cache, byte[] Data, HDestinations Destination, HProtocols Protocol)
        {
            lock (SplitLock)
            {
                if (Cache != null)
                {
                    Data  = Merge(Cache, Data);
                    Cache = null;
                }

                List <byte[]> Chunks = new List <byte[]>();
                if (Protocol == HProtocols.Ancient && Destination == HDestinations.Client)
                {
                    if (!Data.Contains((byte)1))
                    {
                        Cache = Data;
                    }
                    else
                    {
                        List <byte> Buffer = new List <byte>();
                        foreach (byte Value in Data)
                        {
                            Buffer.Add(Value);
                            if (Value == 1)
                            {
                                Chunks.Add(Buffer.ToArray());
                                Buffer.Clear();
                            }
                        }
                        if (Buffer.Count > 0)
                        {
                            Cache = Buffer.ToArray();
                        }
                    }
                }
                else
                {
                    bool IsAncient = Protocol == HProtocols.Ancient;
                    int  Offset    = IsAncient ? 3 : 4;
                    int  Length    = IsAncient ? Ancient.DecypherShort(Data, 1) : BigEndian.DecypherInt(Data);
                    if (Length == Data.Length - Offset)
                    {
                        Chunks.Add(Data);
                    }
                    else
                    {
                        do
                        {
                            if (Length > Data.Length - Offset)
                            {
                                Cache = Data; break;
                            }
                            Chunks.Add(CutBlock(ref Data, 0, Length + Offset));
                            if (Data.Length >= Offset)
                            {
                                Length = IsAncient ? Ancient.DecypherShort(Data, 1) : BigEndian.DecypherInt(Data);
                            }
                        }while (Data.Length != 0);
                    }
                }
                return(Chunks.ToArray());
            }
        }
Пример #10
0
        public HMessage(byte[] Data, HDestinations Destination)
            : this()
        {
            if (Data == null)
            {
                throw new NullReferenceException();
            }
            if (Data.Length < 1)
            {
                throw new Exception("The minimum amount of bytes required to initialize an HMessage instance is 1(One). If the amount of bytes passed is < 3(Three), and >= 1(One), it will be immediately be identified as a corrupted packet. { IsCorrupted = true }");
            }

            this.Destination = Destination;
            bool HasByteZero     = Data.Contains(byte.MinValue);
            bool IsAncientHeader = !HasByteZero && Data.Length == 2 && Data[1] != 1;

            if (!IsAncientHeader && Data.Length >= 6 && BigEndian.DecypherInt(Data) == Data.Length - 4)
            {
                Protocol = HProtocols.Modern;

                _Header = BigEndian.DecypherShort(Data, 4);
                Append(ByteUtils.CopyBlock(Data, 4, Data.Length - 4));

                if (Data.Length == 6)
                {
                    LogWriting = true;
                }
            }
            else if ((Destination == HDestinations.Server && IsAncientHeader) || (!HasByteZero && Data.Length >= 5 && Ancient.DecypherShort(Data, 1) == Data.Length - 3))
            {
                this.Destination = HDestinations.Server;
                Protocol         = HProtocols.Ancient;

                _Header = Ancient.DecypherShort(Data, IsAncientHeader ? 0 : 3);
                Append(IsAncientHeader ? Data : ByteUtils.CopyBlock(Data, 3, Data.Length - 3));

                if (Data.Length == 5 || IsAncientHeader)
                {
                    LogWriting = true;
                }
            }
            else if (IsAncientHeader || (!HasByteZero && Data.Length >= 3 && Data[Data.Length - 1] == 1))
            {
                this.Destination = HDestinations.Client;
                Protocol         = HProtocols.Ancient;

                if (IsAncientHeader)
                {
                    Data = new byte[3] {
                        Data[0], Data[1], 1
                    }
                }
                ;
                _Header = Ancient.DecypherShort(Data);
                Append(Data);

                if (Data.Length == 3 || IsAncientHeader)
                {
                    LogWriting = true;
                }
            }
            else
            {
                Body        = Data;
                BCache      = Data;
                IsCorrupted = true;
                Length      = Data.Length;
                Buffer.AddRange(Data);
                SCache = ToString(Data);
            }
        }
Пример #11
0
        public static byte[] ConstructBody(HDestinations Destination, HProtocols Protocol, params object[] Chunks)
        {
            if (Protocol == HProtocols.Unknown)
            {
                throw new Exception("You must specify a supported HProtocols value for this method. ( Ancient / Modern )");
            }
            if (Protocol == HProtocols.Ancient && Destination == HDestinations.Unknown)
            {
                throw new Exception("Cannot construct the body of an Ancient type packet without a valid HDestinations value. ( Client / Server )");
            }

            List <byte> Buffer    = new List <byte>();
            bool        IsAncient = Protocol == HProtocols.Ancient;

            for (int i = 0; i < Chunks.Length; i++)
            {
                object Chunk = Chunks[i];
                if (Chunk == null)
                {
                    throw new NullReferenceException(string.Format("Unable to encode a null object. {{ Index = {0} }}", i));
                }

                if (Chunk is byte[])
                {
                    Buffer.AddRange((byte[])Chunk);
                }
                else
                {
                    switch (Type.GetTypeCode(Chunk.GetType()))
                    {
                    case TypeCode.Int32:
                    {
                        int Value = (int)Chunk;
                        Buffer.AddRange(IsAncient ? Ancient.CypherInt(Value) : BigEndian.CypherInt(Value));
                        break;
                    }

                    default:
                    case TypeCode.String:
                    {
                        string Value = Chunk.ToString();
                        if (!IsAncient || Destination == HDestinations.Server)
                        {
                            Buffer.AddRange(IsAncient ? Ancient.CypherShort((ushort)Value.Length) : BigEndian.CypherShort((ushort)Value.Length));
                            Buffer.AddRange(Encoding.Default.GetBytes(Value));
                        }
                        else
                        {
                            Buffer.AddRange(Encoding.Default.GetBytes(Value));
                            Buffer.Add(2);
                        }
                        break;
                    }

                    case TypeCode.Boolean:
                    {
                        bool Value = (bool)Chunk;
                        Buffer.Add(IsAncient ? (byte)(Value ? 73 : 72) : Convert.ToByte(Value));
                        break;
                    }

                    case TypeCode.Byte:
                    {
                        byte Value = (byte)Chunk;
                        Buffer.Add(Value);
                        break;
                    }
                    }
                }
            }
            return(Buffer.ToArray());
        }
Пример #12
0
        public static byte[][] Split(ref byte[] cache, byte[] data, HDestination destination, HProtocol protocol)
        {
            lock (SplitLock)
            {
                if (cache != null)
                {
                    data  = Merge(cache, data);
                    cache = null;
                }

                var chunks = new List <byte[]>();
                if (protocol == HProtocol.Ancient && destination == HDestination.Client)
                {
                    if (!data.Contains((byte)1))
                    {
                        cache = data;
                    }
                    else
                    {
                        var buffer = new List <byte>();
                        foreach (byte value in data)
                        {
                            buffer.Add(value);
                            if (value == 1)
                            {
                                chunks.Add(buffer.ToArray());
                                buffer.Clear();
                            }
                        }
                        if (buffer.Count > 0)
                        {
                            cache = buffer.ToArray();
                        }
                    }
                }
                else
                {
                    bool isAncient = (protocol == HProtocol.Ancient);
                    int  offset    = isAncient ? 3 : 4;
                    int  length    = isAncient ? Ancient.DecypherShort(data, 1) : Modern.DecypherInt(data);

                    if (length == data.Length - offset)
                    {
                        chunks.Add(data);
                    }
                    else
                    {
                        do
                        {
                            if (length > data.Length - offset)
                            {
                                cache = data; break;
                            }
                            chunks.Add(CutBlock(ref data, 0, length + offset));
                            if (data.Length >= offset)
                            {
                                length = isAncient ? Ancient.DecypherShort(data, 1) : Modern.DecypherInt(data);
                            }
                        }while (data.Length != 0);
                    }
                }
                return(chunks.ToArray());
            }
        }