Esempio n. 1
0
        private string ReadPString(FieldInfo Info)
        {
            if (Info != null)
            {
                string Type          = Tools.GetAttributePropertyValue(Info, Const.PSTRING, "PrefixType");
                bool   UnicodeLength = Tools.GetAttributePropertyValue(Info, Const.PSTRING, "UnicodeLength");
                long   Len           = ReadValueByType(Type);

                if (UnicodeLength)
                {
                    Len *= 2;
                }

                if (Len > BaseStream.Length - BaseStream.Position)
                {
                    throw new Exception("Invalid Length");
                }

                byte[] Buff = new byte[Len];
                while (Len > 0)
                {
                    Len -= BaseStream.Read(Buff, 0, Len > int.MaxValue ? int.MaxValue : (int)Len);
                }
                return(Encoding.GetString(Buff));
            }
            else
            {
                return(ReadString());
            }
        }
        internal void WriteField(dynamic Value, string Type, FieldInfo field, ref object Instance)
        {
            switch (Type)
            {
            case Const.STRING:
                if (HasAttribute(field, Const.CSTRING))
                {
                    Write(Value, StringStyle.CString);
                    break;
                }
                if (HasAttribute(field, Const.UCSTRING))
                {
                    Write(Value, StringStyle.UCString);
                    break;
                }
                if (HasAttribute(field, Const.PSTRING))
                {
                    Write(field, Value, StringStyle.PString);
                    break;
                }
                if (HasAttribute(field, Const.FSTRING))
                {
                    byte[] Buffer = new byte[Tools.GetAttributePropertyValue(field, Const.FSTRING, "Length")];
                    Encoding.GetBytes((string)Value).CopyTo(Buffer, 0);
                    Write(Buffer);
                    break;
                }
                throw new Exception("String Attribute Not Specified.");

            default:
                if (HasAttribute(field, Const.STRUCT))
                {
                    WriteStruct(System.Type.GetType(Type), ref Value);
                }
                else
                {
                    if (field.FieldType.BaseType.ToString() == Const.DELEGATE)
                    {
                        FieldInvoke Invoker = ((FieldInvoke)Value);
                        if (Invoker == null)
                        {
                            break;
                        }
                        Instance = Invoker.Invoke(BaseStream, false, Instance);
                        field.SetValue(Instance, Invoker);
                    }
                    else if (BigEndian)
                    {
                        Write(Tools.Reverse(Value));
                    }
                    else
                    {
                        Write(Value);
                    }
                }
                break;
            }
        }
        internal void ReadStruct(Type type, ref object Instance)
        {
            FieldInfo[] fields = type.GetFields(BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Instance);
            for (int i = 0; i < fields.Length; i++)
            {
                FieldInfo field = fields[i];
                if (Tools.HasAttribute(field, Const.IGNORE))
                {
                    continue;
                }

                string  FType = field.FieldType.ToString();
                dynamic Value = null;

                if (!FType.EndsWith("[]"))
                {
                    Value = ReadField(FType, field, ref Instance);
                }
                else
                {
                    if (Tools.HasAttribute(field, Const.FARRAY))
                    {
                        byte[] Buffer = new byte[Tools.GetAttributePropertyValue(field, Const.FARRAY, "Length")];

                        if (Read(Buffer, 0, Buffer.Length) != Buffer.Length)
                        {
                            throw new Exception("Failed to Read");
                        }

                        Value = Buffer;
                    }
                    else if (Tools.HasAttribute(field, Const.PARRAY))
                    {
                        FType = FType.Substring(0, FType.Length - 2);

                        string  PType = Tools.GetAttributePropertyValue(field, Const.PARRAY, "PrefixType");
                        dynamic Count = ReadField(PType, field, ref Instance);


                        object[] Content = new object[Count];
                        for (long x = 0; x < Count; x++)
                        {
                            Content[x] = ReadField(FType, field, ref Instance);
                        }


                        Value = Array.CreateInstance(Type.GetType(FType), Count);
                        Content.CopyTo(Value, 0);
                    }
                    else
                    {
                        throw new Exception("Bad Struct Configuration");
                    }
                }

                field.SetValue(Instance, Value);
            }
        }
        internal void ReadStruct(Type type, ref object Instance)
        {
            FieldInfo[] fields = type.GetFields(BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Instance);
            for (int i = 0; i < fields.Length; i++)
            {
                FieldInfo field = fields[i];
                if (Tools.HasAttribute(field, Const.IGNORE))
                {
                    continue;
                }

                string  FType = field.FieldType.ToString();
                dynamic Value = null;

                if (!FType.EndsWith("[]"))
                {
                    Value = ReadField(FType, field, ref Instance);
                }
                else
                {
                    dynamic Count = null;
                    if (Tools.HasAttribute(field, Const.FARRAY))
                    {
                        Count = Tools.GetAttributePropertyValue(field, Const.FARRAY, "Length");
                    }
                    else if (Tools.HasAttribute(field, Const.PARRAY))
                    {
                        string PType = Tools.GetAttributePropertyValue(field, Const.PARRAY, "PrefixType");
                        Count = ReadField(PType, field, ref Instance);
                    }
                    else if (Tools.HasAttribute(field, Const.RARRAY))
                    {
                        string    PFieldName  = Tools.GetAttributePropertyValue(field, Const.RARRAY, "FieldName");
                        FieldInfo PrefixField = (from x in fields where x.Name == PFieldName select x).First();
                        Count = PrefixField.GetValue(Instance);
                    }
                    else
                    {
                        throw new Exception("Bad Struct Array Configuration");
                    }

                    FType = FType.Substring(0, FType.Length - 2);

                    object[] Content = new object[Count];
                    for (long x = 0; x < Count; x++)
                    {
                        Content[x] = ReadField(FType, field, ref Instance);
                    }


                    Value = Array.CreateInstance(Type.GetType(FType), Count);
                    Content.CopyTo(Value, 0);
                }

                field.SetValue(Instance, Value);
            }
        }
        internal void WriteStruct(Type type, ref object Instance)
        {
            FieldInfo[] fields = type.GetFields(BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Instance);
            foreach (FieldInfo field in fields)
            {
                if (HasAttribute(field, Const.IGNORE))
                {
                    continue;
                }
                dynamic Value = field.GetValue(Instance);
                string  Type  = field.FieldType.ToString();
                if (!Type.EndsWith("[]"))
                {
                    WriteField(Value, Type, field, ref Instance);
                    continue;
                }
                if (HasAttribute(field, Const.FARRAY))
                {
                    long BufferLen = Tools.GetAttributePropertyValue(field, Const.FARRAY, "Length");

                    if (Value.Length > BufferLen)
                    {
                        throw new Exception("Wrong Array Buffer Length");
                    }

                    byte[] Arr = new byte[BufferLen];
                    Value.CopyTo(Arr, 0);

                    Write(Arr);
                    continue;
                }

                if (!HasAttribute(field, Const.PARRAY))
                {
                    throw new Exception("Bad Struct Array Configuration.");
                }

                string  PType  = Tools.GetAttributePropertyValue(field, Const.PARRAY, "PrefixType");
                dynamic Length = ParseType(Value.LongLength, PType);

                if (BigEndian)
                {
                    Length = Tools.Reverse(Length);
                }
                Write(Length);

                System.Collections.IEnumerator Enum = Value.GetEnumerator();
                for (long i = 0; i < Length; i++)
                {
                    Enum.MoveNext();
                    WriteField(Enum.Current, Type.Substring(0, Type.Length - 2), field, ref Instance);
                }
            }
        }
        public string ReadString(StringStyle Style, FieldInfo Info = null)
        {
            List <byte> Buffer = new List <byte>();

            switch (Style)
            {
            case StringStyle.CString:
                while (true)
                {
                    byte Byte = base.ReadByte();
                    if (Byte < 1)
                    {
                        break;
                    }
                    Buffer.Add(Byte);
                }
                return(Encoding.GetString(Buffer.ToArray()));

            case StringStyle.UCString:
                while (true)
                {
                    byte Byte1 = base.ReadByte();
                    byte Byte2 = base.ReadByte();
                    if (Byte1 == 0x00 && Byte2 == 0x00)
                    {
                        break;
                    }
                    Buffer.Add(Byte1);
                    Buffer.Add(Byte2);
                }
                return(Encoding.GetString(Buffer.ToArray()));

            case StringStyle.PString:
                if (Info != null)
                {
                    long   Len;
                    string Prefix        = Tools.GetAttributePropertyValue(Info, Const.PSTRING, "PrefixType");
                    bool   UnicodeLength = Tools.GetAttributePropertyValue(Info, Const.PSTRING, "UnicodeLength");
                    switch (Prefix)
                    {
                    case Const.INT16:
                        if (BigEndian)
                        {
                            Len = Tools.Reverse(ReadInt16());
                        }
                        else
                        {
                            Len = ReadInt16();
                        }
                        break;

                    case Const.UINT16:
                        if (BigEndian)
                        {
                            Len = Tools.Reverse(ReadUInt16());
                        }
                        else
                        {
                            Len = ReadUInt16();
                        }
                        break;

                    case Const.UINT8:
                        if (BigEndian)
                        {
                            Len = Tools.Reverse(ReadByte());
                        }
                        else
                        {
                            Len = ReadByte();
                        }
                        break;

                    case Const.INT8:
                        if (BigEndian)
                        {
                            Len = Tools.Reverse(ReadSByte());
                        }
                        else
                        {
                            Len = ReadSByte();
                        }
                        break;

                    case Const.INT32:
                        if (BigEndian)
                        {
                            Len = Tools.Reverse(ReadInt32());
                        }
                        else
                        {
                            Len = ReadInt32();
                        }
                        break;

                    case Const.UINT32:
                        if (BigEndian)
                        {
                            Len = Tools.Reverse(ReadUInt32());
                        }
                        else
                        {
                            Len = ReadUInt32();
                        }
                        break;

                    case Const.INT64:
                        if (BigEndian)
                        {
                            Len = Tools.Reverse(ReadInt64());
                        }
                        else
                        {
                            Len = ReadInt64();
                        }
                        break;

                    default:
                        throw new Exception("Invalid Data Type");
                    }
                    if (UnicodeLength)
                    {
                        Len *= 2;
                    }
                    if (Len > BaseStream.Length - BaseStream.Position)
                    {
                        throw new Exception("Invalid Length");
                    }
                    byte[] Buff = new byte[Len];
                    while (Len > 0)
                    {
                        Len -= BaseStream.Read(Buff, 0, Len > int.MaxValue ? int.MaxValue : (int)Len);
                    }
                    return(Encoding.GetString(Buff));
                }
                else
                {
                    return(ReadString());
                }

            default:
                throw new Exception("Unk Value Type");
            }
        }
        private void ReadStruct(Type type, ref object Instance)
        {
            FieldInfo[] fields = type.GetFields(BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Instance);
            for (int i = 0; i < fields.Length; i++)
            {
                FieldInfo field = fields[i];
                if (Tools.HasAttribute(field, Const.IGNORE))
                {
                    continue;
                }
                dynamic Value    = null;
                bool    IsNumber = true;
                switch (field.FieldType.ToString())
                {
                case Const.INT8:
                    Value = base.ReadSByte();
                    break;

                case Const.INT16:
                    Value = base.ReadInt16();
                    break;

                case Const.UINT16:
                    Value = base.ReadUInt16();
                    break;

                case Const.UINT8:
                    Value = base.ReadByte();
                    break;

                case Const.INT32:
                    Value = base.ReadInt32();
                    break;

                case Const.UINT32:
                    Value = base.ReadUInt32();
                    break;

                case Const.DOUBLE:
                    Value = base.ReadDouble();
                    break;

                case Const.FLOAT:
                    Value = base.ReadSingle();
                    break;

                case Const.INT64:
                    Value = base.ReadInt64();
                    break;

                case Const.UINT64:
                    Value = base.ReadUInt64();
                    break;

                case Const.STRING:
                    IsNumber = false;
                    if (Tools.HasAttribute(field, Const.CSTRING) && Tools.HasAttribute(field, Const.PSTRING))
                    {
                        throw new Exception("You can't use CString and PString Attribute into the same field.");
                    }
                    if (Tools.HasAttribute(field, Const.CSTRING))
                    {
                        Value = ReadString(StringStyle.CString);
                        break;
                    }
                    if (Tools.HasAttribute(field, Const.UCSTRING))
                    {
                        Value = ReadString(StringStyle.UCString);
                        break;
                    }
                    if (Tools.HasAttribute(field, Const.PSTRING))
                    {
                        Value = ReadString(StringStyle.PString, field);
                        break;
                    }
                    if (Tools.HasAttribute(field, Const.FSTRING))
                    {
                        byte[] Bffr = new byte[Tools.GetAttributePropertyValue(field, Const.FSTRING, "Length")];
                        if (Read(Bffr, 0, Bffr.Length) != Bffr.Length)
                        {
                            throw new Exception("Failed to Read a String");
                        }
                        Value = Encoding.GetString(Bffr);
                        break;
                    }
                    throw new Exception("String Attribute Not Specified.");

                case Const.BYTEARR:
                    byte[] Buffer = new byte[Tools.GetAttributePropertyValue(field, Const.ArrSize, "Length")];
                    if (Read(Buffer, 0, Buffer.Length) != Buffer.Length)
                    {
                        throw new Exception("Failed to Read");
                    }
                    Value = Buffer;
                    break;

                default:
                    IsNumber = false;
                    if (Tools.HasAttribute(field, Const.STRUCT))
                    {
                        Value = Activator.CreateInstance(field.FieldType);
                        ReadStruct(field.FieldType, ref Value);
                    }
                    else
                    {
                        if (field.FieldType.BaseType.ToString() == Const.DELEGATE)
                        {
                            FieldInvoke Invoker = (FieldInvoke)field.GetValue(Instance);
                            Value = Invoker;
                            if (Invoker == null)
                            {
                                break;
                            }
                            Instance = Invoker.Invoke(BaseStream, true, Instance);
                            break;
                        }
                        throw new Exception("Unk Struct Field: " + field.FieldType.ToString());
                    }
                    break;
                }
                if (IsNumber && BigEndian)
                {
                    Value = Tools.Reverse(Value);
                }
                field.SetValue(Instance, Value);
            }
        }
        private void WritePString(FieldInfo Field, dynamic Value)
        {
            byte[] Arr = Encoding.GetBytes(Value);

            string Prefix        = Tools.GetAttributePropertyValue(Field, Const.PSTRING, "PrefixType");
            bool   UnicodeLength = Tools.GetAttributePropertyValue(Field, Const.PSTRING, "UnicodeLength");


            long Length = Arr.LongLength;

            if (UnicodeLength)
            {
                Length /= 2;
            }

            switch (Prefix)
            {
            case Const.INT16:
                if (BigEndian)
                {
                    base.Write((short)Tools.Reverse((short)Length));
                }
                else
                {
                    base.Write((short)Length);
                }
                break;

            case Const.UINT16:
                if (BigEndian)
                {
                    base.Write((ushort)Tools.Reverse((ushort)Length));
                }
                else
                {
                    base.Write((ushort)Length);
                }
                break;

            case Const.UINT8:
                if (BigEndian)
                {
                    base.Write((byte)Tools.Reverse((byte)Length));
                }
                else
                {
                    base.Write((byte)Length);
                }
                break;

            case Const.INT8:
                if (BigEndian)
                {
                    base.Write((sbyte)Tools.Reverse((sbyte)Length));
                }
                else
                {
                    base.Write((sbyte)Length);
                }
                break;

            case Const.INT32:
                if (BigEndian)
                {
                    base.Write((int)Tools.Reverse((int)Length));
                }
                else
                {
                    base.Write((int)Length);
                }
                break;

            case Const.UINT32:
                if (BigEndian)
                {
                    base.Write((uint)Tools.Reverse((uint)Length));
                }
                else
                {
                    base.Write((uint)Length);
                }
                break;

            case Const.INT64:
                if (BigEndian)
                {
                    base.Write((long)Tools.Reverse(Length));
                }
                else
                {
                    base.Write(Length);
                }
                break;

            default:
                throw new Exception("Invalid Data Type");
            }
            base.Write(Arr);
        }
        private void WriteStruct(Type type, ref object Instance)
        {
            FieldInfo[] fields = type.GetFields(BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Instance);
            foreach (FieldInfo field in fields)
            {
                if (HasAttribute(field, Const.IGNORE))
                {
                    break;
                }
                dynamic Value = field.GetValue(Instance);
                switch (field.FieldType.ToString())
                {
                case Const.STRING:
                    if (HasAttribute(field, Const.CSTRING))
                    {
                        Write(Value, StringStyle.CString);
                        break;
                    }
                    if (HasAttribute(field, Const.UCSTRING))
                    {
                        Write(Value, StringStyle.UCString);
                        break;
                    }
                    if (HasAttribute(field, Const.PSTRING))
                    {
                        Write(field, Value, StringStyle.PString);
                        break;
                    }
                    if (HasAttribute(field, Const.FSTRING))
                    {
                        byte[] Buffer = new byte[Tools.GetAttributePropertyValue(field, Const.FSTRING, "Length")];
                        Encoding.GetBytes((string)Value).CopyTo(Buffer, 0);
                        Write(Buffer);
                        break;
                    }
                    throw new Exception("String Attribute Not Specified.");

                case Const.BYTEARR:
                    if (HasAttribute(field, Const.ArrSize))
                    {
                        long BufferLen = Tools.GetAttributePropertyValue(field, Const.ArrSize, "Length");
                        if (Value.Length > BufferLen)
                        {
                            throw new Exception("Wrong Array Buffer Length");
                        }
                        byte[] Arr = new byte[BufferLen];
                        Value.CopyTo(Arr, 0);

                        Write(Arr);
                    }
                    else
                    {
                        throw new Exception("The Array don't have the length specified");
                    }
                    break;

                default:
                    if (HasAttribute(field, Const.STRUCT))
                    {
                        WriteStruct(field.FieldType, ref Value);
                    }
                    else
                    {
                        if (field.FieldType.BaseType.ToString() == Const.DELEGATE)
                        {
                            FieldInvoke Invoker = ((FieldInvoke)Value);
                            if (Invoker == null)
                            {
                                break;
                            }
                            Instance = Invoker.Invoke(BaseStream, false, Instance);
                            field.SetValue(Instance, Invoker);
                        }
                        else if (BigEndian)
                        {
                            Write(Tools.Reverse(Value));
                        }
                        else
                        {
                            Write(Value);
                        }
                    }
                    break;
                }
            }
        }
        internal dynamic ReadField(string Type, FieldInfo field, ref object Instance)
        {
            bool    IsNumber = true;
            dynamic Value    = null;

            switch (Type)
            {
            case Const.INT8:
                Value = base.ReadSByte();
                break;

            case Const.INT16:
                Value = base.ReadInt16();
                break;

            case Const.CHAR:
                Value = base.ReadChar();
                break;

            case Const.UINT16:
                Value = base.ReadUInt16();
                break;

            case Const.UINT8:
                Value = base.ReadByte();
                break;

            case Const.INT32:
                Value = base.ReadInt32();
                break;

            case Const.UINT32:
                Value = base.ReadUInt32();
                break;

            case Const.DOUBLE:
                Value = base.ReadDouble();
                break;

            case Const.FLOAT:
                Value = base.ReadSingle();
                break;

            case Const.INT64:
                Value = base.ReadInt64();
                break;

            case Const.UINT64:
                Value = base.ReadUInt64();
                break;

            case Const.STRING:
                IsNumber = false;
                if (Tools.HasAttribute(field, Const.CSTRING) && Tools.HasAttribute(field, Const.PSTRING))
                {
                    throw new Exception("You can't use CString and PString Attribute into the same field.");
                }
                if (Tools.HasAttribute(field, Const.CSTRING))
                {
                    Value = ReadString(StringStyle.CString);
                    break;
                }
                if (Tools.HasAttribute(field, Const.UCSTRING))
                {
                    Value = ReadString(StringStyle.UCString);
                    break;
                }
                if (Tools.HasAttribute(field, Const.PSTRING))
                {
                    Value = ReadString(StringStyle.PString, field);
                    break;
                }
                if (Tools.HasAttribute(field, Const.FSTRING))
                {
                    byte[] Bffr = new byte[Tools.GetAttributePropertyValue(field, Const.FSTRING, "Length")];
                    if (Read(Bffr, 0, Bffr.Length) != Bffr.Length)
                    {
                        throw new Exception("Failed to Read a String");
                    }
                    bool TrimEnd = Tools.GetAttributePropertyValue(field, Const.FSTRING, "TrimNull");
                    Value = TrimEnd ? Encoding.GetString(Bffr).TrimEnd('\x0') : Encoding.GetString(Bffr);
                    break;
                }
                throw new Exception("String Attribute Not Specified.");

            default:
                IsNumber = false;
                if (Tools.HasAttribute(field, Const.STRUCT))
                {
                    Type FieldType = System.Type.GetType(Type);
                    Value = Activator.CreateInstance(FieldType);
                    ReadStruct(FieldType, ref Value);
                }
                else
                {
                    if (field.FieldType.BaseType.ToString() == Const.DELEGATE)
                    {
                        FieldInvoke Invoker = (FieldInvoke)field.GetValue(Instance);
                        Value = Invoker;
                        if (Invoker == null)
                        {
                            break;
                        }
                        Instance = Invoker.Invoke(BaseStream, true, Instance);
                        break;
                    }
                    throw new Exception("Unk Struct Field: " + field.FieldType.ToString());
                }
                break;
            }
            if (IsNumber && BigEndian)
            {
                Value = Tools.Reverse(Value);
            }
            return(Value);
        }
        internal void WriteStruct(Type type, ref object Instance)
        {
            FieldInfo[] fields = type.GetFields(BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Instance);

            foreach (FieldInfo field in fields)
            {
                if (HasAttribute(field, Const.IGNORE) || !HasAttribute(field, Const.RARRAY))
                {
                    continue;
                }

                dynamic   Value       = field.GetValue(Instance);
                string    PFieldName  = Tools.GetAttributePropertyValue(field, Const.RARRAY, "FieldName");
                FieldInfo PrefixField = (from x in fields where x.Name == PFieldName select x).First();
                PrefixField.SetValue(Instance, ParseType(Value.LongLength, PrefixField.FieldType.FullName));
            }

            foreach (FieldInfo field in fields)
            {
                if (HasAttribute(field, Const.IGNORE))
                {
                    continue;
                }
                dynamic Value = field.GetValue(Instance);
                string  Type  = field.FieldType.ToString();
                if (!Type.EndsWith("[]"))
                {
                    WriteField(Value, Type, field, ref Instance);
                    continue;
                }

                if (HasAttribute(field, Const.FARRAY))
                {
                    long Length = Tools.GetAttributePropertyValue(field, Const.FARRAY, "Length");

                    if (Value.LongLength > Length)
                    {
                        throw new Exception("Wrong Array Buffer Length");
                    }
                }
                else if (HasAttribute(field, Const.PARRAY))
                {
                    string  PType  = Tools.GetAttributePropertyValue(field, Const.PARRAY, "PrefixType");
                    dynamic Length = ParseType(Value.LongLength, PType);

                    if (BigEndian)
                    {
                        Length = Tools.Reverse(Length);
                    }
                    Write(Length);
                }
                else if (!HasAttribute(field, Const.RARRAY))
                {
                    throw new Exception("Bad Struct Array Configuration");
                }

                string      RealType = Type.Substring(0, Type.Length - 2);
                IEnumerator Enum     = Value.GetEnumerator();
                for (long i = 0; i < Value.LongLength; i++)
                {
                    Enum.MoveNext();
                    WriteField(Enum.Current, RealType, field, ref Instance);
                }
            }
        }
Esempio n. 12
0
        public void Write(FieldInfo Field, dynamic Value, StringStyle Style)
        {
            switch (Style)
            {
            case StringStyle.UCString:
            case StringStyle.CString:
                List <byte> Buffer = new List <byte>(Encoding.GetBytes(Value + "\x0"));
                base.Write(Buffer.ToArray(), 0, Buffer.Count);
                break;

            case StringStyle.PString:
                byte[] Arr = Encoding.GetBytes(Value);

                string Prefix        = Tools.GetAttributePropertyValue(Field, Const.PSTRING, "PrefixType");
                bool   UnicodeLength = Tools.GetAttributePropertyValue(Field, Const.PSTRING, "UnicodeLength");


                long Length = Arr.LongLength;
                if (UnicodeLength)
                {
                    Length /= 2;
                }

                switch (Prefix)
                {
                case Const.INT16:
                    if (BigEndian)
                    {
                        base.Write((short)Tools.Reverse((short)Length));
                    }
                    else
                    {
                        base.Write((short)Length);
                    }
                    break;

                case Const.UINT16:
                    if (BigEndian)
                    {
                        base.Write((ushort)Tools.Reverse((ushort)Length));
                    }
                    else
                    {
                        base.Write((ushort)Length);
                    }
                    break;

                case Const.UINT8:
                    if (BigEndian)
                    {
                        base.Write((byte)Tools.Reverse((byte)Length));
                    }
                    else
                    {
                        base.Write((byte)Length);
                    }
                    break;

                case Const.INT8:
                    if (BigEndian)
                    {
                        base.Write((sbyte)Tools.Reverse((sbyte)Length));
                    }
                    else
                    {
                        base.Write((sbyte)Length);
                    }
                    break;

                case Const.INT32:
                    if (BigEndian)
                    {
                        base.Write((int)Tools.Reverse((int)Length));
                    }
                    else
                    {
                        base.Write((int)Length);
                    }
                    break;

                case Const.UINT32:
                    if (BigEndian)
                    {
                        base.Write((uint)Tools.Reverse((uint)Length));
                    }
                    else
                    {
                        base.Write((uint)Length);
                    }
                    break;

                case Const.INT64:
                    if (BigEndian)
                    {
                        base.Write((long)Tools.Reverse(Length));
                    }
                    else
                    {
                        base.Write(Length);
                    }
                    break;

                default:
                    throw new Exception("Invalid Data Type");
                }
                base.Write(Arr);
                break;
            }
        }