Esempio n. 1
0
        public static int ReadObj(DataSection obj, byte[] buffer, int offset, int length)
        {
            if ((obj == null) ||
                (buffer == null) ||
                ((offset + length) > buffer.Length))
            {
                return(1);
            }

            length += offset;

            Type objType = obj.GetType();

            foreach (FieldInfo fieldInfo in objType.GetFields(BindingFlags.Public | BindingFlags.Instance))
            {
                try
                {
                    if (fieldInfo.FieldType == typeof(String))
                    {
                        offset = obj._Pack(offset, 1);

                        FieldInfo fiLen = objType.GetField(fieldInfo.Name + "_Length");

                        String val = null;

                        if (fiLen != null)
                        {
                            int size = (int)fiLen.GetValue(obj);

                            val = BytesTool.readString(obj.EncodingOfString, buffer, offset, size);

                            if (obj.TypeOfString == StringType.FIXED_LENGTH)
                            {
                                offset += size;
                            }
                            else
                            {
                                string val2 = val == null ? "\0" : val + "\0";

                                offset += obj.EncodingOfString.GetByteCount(val2);
                            }
                        }
                        else
                        {
                            int maxSize = obj.EncodingOfString.GetMaxCharCount(length - offset);

                            val = BytesTool.readString(obj.EncodingOfString, buffer, offset, maxSize);

                            string val2 = val == null ? "\0" : val + "\0";

                            offset += obj.EncodingOfString.GetByteCount(val2);
                        }

                        if ((val != null) &&
                            (val.Length == 0))
                        {
                            val = null;
                        }

                        fieldInfo.SetValue(obj, val);
                    }
                    else if (fieldInfo.FieldType.IsArray &&
                             fieldInfo.FieldType.HasElementType)
                    {
                        if (fieldInfo.FieldType.GetElementType() == typeof(String))
                        {
                            offset = obj._Pack(offset, 1);

                            FieldInfo fiLen = objType.GetField(fieldInfo.Name + "_Length");
                            String[]  array = (String[])fieldInfo.GetValue(obj);

                            if (fiLen != null)
                            {
                                int size = (int)fiLen.GetValue(obj);

                                for (int i = 0; i < array.Length; i++)
                                {
                                    array[i] = BytesTool.readString(obj.EncodingOfString, buffer, offset, size);

                                    if (obj.TypeOfString == StringType.FIXED_LENGTH)
                                    {
                                        offset += size;
                                    }
                                    else
                                    {
                                        string val2 = array[i] == null ? "\0" : array[i] + "\0";

                                        offset += obj.EncodingOfString.GetByteCount(val2);
                                    }

                                    if ((array[i] != null) &&
                                        (array[i].Length == 0))
                                    {
                                        array[i] = null;
                                    }
                                }
                            }
                            else
                            {
                                for (int i = 0; i < array.Length; i++)
                                {
                                    int maxSize = obj.EncodingOfString.GetMaxCharCount(length - offset);

                                    array[i] = BytesTool.readString(obj.EncodingOfString, buffer, offset, maxSize);

                                    string val2 = array[i] == null ? "\0" : array[i] + "\0";

                                    offset += obj.EncodingOfString.GetByteCount(val2);
                                }
                            }
                        }
                        else if (fieldInfo.FieldType.GetElementType() == typeof(UInt64))
                        {
                            int size = 8;

                            UInt64[] array = (UInt64[])fieldInfo.GetValue(obj);

                            if (array != null)
                            {
                                offset = obj._Pack(offset, size);

                                for (int i = 0; i < array.Length; i++)
                                {
                                    array[i] = (UInt64)BytesTool.readBytes(buffer, offset, size, obj.LittleEndian);
                                    offset  += size;
                                }
                            }
                        }
                        else if (fieldInfo.FieldType.GetElementType() == typeof(Int64))
                        {
                            int size = 8;

                            Int64[] array = (Int64[])fieldInfo.GetValue(obj);

                            if (array != null)
                            {
                                offset = obj._Pack(offset, size);

                                for (int i = 0; i < array.Length; i++)
                                {
                                    array[i] = BytesTool.readBytes(buffer, offset, size, obj.LittleEndian);
                                    offset  += size;
                                }
                            }
                        }
                        else if (fieldInfo.FieldType.GetElementType() == typeof(UInt32))
                        {
                            int size = 4;

                            UInt32[] array = (UInt32[])fieldInfo.GetValue(obj);

                            if (array != null)
                            {
                                offset = obj._Pack(offset, size);

                                for (int i = 0; i < array.Length; i++)
                                {
                                    array[i] = (UInt32)BytesTool.readBytes(buffer, offset, size, obj.LittleEndian);
                                    offset  += size;
                                }
                            }
                        }
                        else if (fieldInfo.FieldType.GetElementType() == typeof(Int32))
                        {
                            int size = 4;

                            Int32[] array = (Int32[])fieldInfo.GetValue(obj);

                            if (array != null)
                            {
                                offset = obj._Pack(offset, size);

                                for (int i = 0; i < array.Length; i++)
                                {
                                    array[i] = (Int32)BytesTool.readBytes(buffer, offset, size, obj.LittleEndian);
                                    offset  += size;
                                }
                            }
                        }
                        else if (fieldInfo.FieldType.GetElementType() == typeof(UInt16))
                        {
                            int size = 2;

                            UInt16[] array = (UInt16[])fieldInfo.GetValue(obj);

                            if (array != null)
                            {
                                offset = obj._Pack(offset, size);

                                for (int i = 0; i < array.Length; i++)
                                {
                                    array[i] = (UInt16)BytesTool.readBytes(buffer, offset, size, obj.LittleEndian);
                                    offset  += size;
                                }
                            }
                        }
                        else if (fieldInfo.FieldType.GetElementType() == typeof(Int16))
                        {
                            int size = 2;

                            Int16[] array = (Int16[])fieldInfo.GetValue(obj);

                            if (array != null)
                            {
                                offset = obj._Pack(offset, size);

                                for (int i = 0; i < array.Length; i++)
                                {
                                    array[i] = (Int16)BytesTool.readBytes(buffer, offset, size, obj.LittleEndian);
                                    offset  += size;
                                }
                            }
                        }
                        else if (fieldInfo.FieldType.GetElementType() == typeof(Byte))
                        {
                            Byte[] array = (Byte[])fieldInfo.GetValue(obj);

                            if (array != null)
                            {
                                offset = obj._Pack(offset, 1);

                                for (int i = 0; i < array.Length; i++)
                                {
                                    array[i] = buffer[offset++];
                                }
                            }
                        }
                        else if (fieldInfo.FieldType.GetElementType() == typeof(SByte))
                        {
                            SByte[] array = (SByte[])fieldInfo.GetValue(obj);

                            if (array != null)
                            {
                                offset = obj._Pack(offset, 1);

                                for (int i = 0; i < array.Length; i++)
                                {
                                    array[i] = (SByte)buffer[offset++];
                                }
                            }
                        }
                        else if (fieldInfo.FieldType.GetElementType() == typeof(DataSection))
                        {
                            // todo
                        }
                    }
                    else if (fieldInfo.FieldType == typeof(Int64))
                    {
                        int size = 8;

                        offset = obj._Pack(offset, size);

                        Int64 val = BytesTool.readBytes(buffer, offset, size, obj.LittleEndian);

                        fieldInfo.SetValue(obj, val);

                        offset += size;
                    }
                    else if (fieldInfo.FieldType == typeof(UInt64))
                    {
                        int size = 8;

                        offset = obj._Pack(offset, size);

                        UInt64 val = (UInt64)BytesTool.readBytes(buffer, offset, size, obj.LittleEndian);

                        fieldInfo.SetValue(obj, val);

                        offset += size;
                    }
                    else if (fieldInfo.FieldType == typeof(Int32))
                    {
                        int size = 4;

                        offset = obj._Pack(offset, size);

                        Int32 val = (Int32)BytesTool.readBytes(buffer, offset, size, obj.LittleEndian);

                        fieldInfo.SetValue(obj, val);

                        offset += size;
                    }
                    else if (fieldInfo.FieldType == typeof(UInt32))
                    {
                        int size = 4;

                        offset = obj._Pack(offset, size);

                        UInt32 val = (UInt32)BytesTool.readBytes(buffer, offset, size, obj.LittleEndian);

                        fieldInfo.SetValue(obj, val);

                        offset += size;
                    }
                    else if (fieldInfo.FieldType == typeof(Int16))
                    {
                        int size = 2;

                        offset = obj._Pack(offset, size);

                        Int16 val = (Int16)BytesTool.readBytes(buffer, offset, size, obj.LittleEndian);

                        fieldInfo.SetValue(obj, val);

                        offset += size;
                    }
                    else if (fieldInfo.FieldType == typeof(UInt16))
                    {
                        int size = 2;

                        offset = obj._Pack(offset, size);

                        UInt16 val = (UInt16)BytesTool.readBytes(buffer, offset, size, obj.LittleEndian);

                        fieldInfo.SetValue(obj, val);

                        offset += size;
                    }
                    else if (fieldInfo.FieldType == typeof(SByte))
                    {
                        offset = obj._Pack(offset, 1);

                        SByte val = (SByte)BytesTool.readBytes(buffer, offset, 1, obj.LittleEndian);

                        fieldInfo.SetValue(obj, val);

                        offset++;
                    }
                    else if (fieldInfo.FieldType == typeof(Byte))
                    {
                        offset = obj._Pack(offset, 1);

                        Byte val = (Byte)BytesTool.readBytes(buffer, offset, 1, obj.LittleEndian);

                        fieldInfo.SetValue(obj, val);

                        offset++;
                    }
                    else if (fieldInfo.FieldType == typeof(DataSection))
                    {
                        DataSection innerSection = (DataSection)fieldInfo.GetValue(obj);

                        offset = obj._Pack(offset, 4);

                        int innerRet = innerSection.Read(buffer, offset, length - offset);

                        if (innerRet != 0)
                        {
                            return(innerRet << 1);
                        }

                        offset += innerSection.Size();
                    }
                }
                catch
                {
                    return(2);
                }
            }

            return(0);
        }
Esempio n. 2
0
        public static int WriteObj(DataSection obj, byte[] buffer, int offset, int length)
        {
            if ((obj == null) ||
                (buffer == null) ||
                ((offset + length) > buffer.Length))
            {
                return(1);
            }

            length += offset;

            Type objType = obj.GetType();

            foreach (FieldInfo fieldInfo in objType.GetFields(BindingFlags.Public | BindingFlags.Instance))
            {
                try
                {
                    if (fieldInfo.FieldType == typeof(String))
                    {
                        offset = obj._Pack(offset, 1);

                        FieldInfo fiLen = objType.GetField(fieldInfo.Name + "_Length");

                        String val = (String)fieldInfo.GetValue(obj);

                        if (fiLen != null)
                        {
                            int size = (int)fiLen.GetValue(obj);

                            if (obj.TypeOfString == StringType.FIXED_LENGTH)
                            {
                                BytesTool.emptyBuffer(buffer, offset, size, 0);

                                if (val != null)
                                {
                                    BytesTool.writeString(obj.EncodingOfString, val, buffer, offset, size);
                                }

                                offset += size;
                            }
                            else
                            {
                                if (val == null)
                                {
                                    val = "\0";
                                }

                                int valLength = obj.EncodingOfString.GetByteCount(val);
                                BytesTool.writeString(obj.EncodingOfString, val, buffer, offset, size);

                                offset += valLength < size ? valLength : size;
                            }
                        }
                        else
                        {
                            if (val == null)
                            {
                                val = "\0";
                            }

                            int valLength = obj.EncodingOfString.GetByteCount(val);
                            BytesTool.writeString(obj.EncodingOfString, val, buffer, offset, length - offset);

                            offset += valLength;
                        }
                    }
                    else if (fieldInfo.FieldType.IsArray &&
                             fieldInfo.FieldType.HasElementType)
                    {
                        if (fieldInfo.FieldType.GetElementType() == typeof(String))
                        {
                            offset = obj._Pack(offset, 1);

                            FieldInfo fiLen = objType.GetField(fieldInfo.Name + "_Length");
                            String[]  array = (String[])fieldInfo.GetValue(obj);

                            if (fiLen != null)
                            {
                                int size = (int)fiLen.GetValue(obj);

                                if (obj.TypeOfString == StringType.FIXED_LENGTH)
                                {
                                    for (int i = 0; i < array.Length; i++)
                                    {
                                        BytesTool.emptyBuffer(buffer, offset, size, 0);

                                        if (array[i] != null)
                                        {
                                            BytesTool.writeString(obj.EncodingOfString, array[i], buffer, offset, size);
                                        }

                                        offset += size;
                                    }
                                }
                                else
                                {
                                    for (int i = 0; i < array.Length; i++)
                                    {
                                        string val = array[i];

                                        if (val != null)
                                        {
                                            val = "\0";
                                        }

                                        int valLength = obj.EncodingOfString.GetByteCount(val);
                                        BytesTool.writeString(obj.EncodingOfString, val, buffer, offset, size);

                                        offset += valLength < size ? valLength : size;
                                    }
                                }
                            }
                            else
                            {
                                for (int i = 0; i < array.Length; i++)
                                {
                                    string val = array[i];

                                    if (val == null)
                                    {
                                        val = "\0";
                                    }

                                    int valLength = obj.EncodingOfString.GetByteCount(val);
                                    BytesTool.writeString(obj.EncodingOfString, val, buffer, offset, length - offset);

                                    offset += valLength;
                                }
                            }
                        }
                        else if (fieldInfo.FieldType.GetElementType() == typeof(UInt64))
                        {
                            int size = 8;

                            UInt64[] array = (UInt64[])fieldInfo.GetValue(obj);

                            if (array != null)
                            {
                                offset = obj._Pack(offset, size);

                                for (int i = 0; i < array.Length; i++)
                                {
                                    BytesTool.writeBytes((Int64)array[i], buffer, offset, size, obj.LittleEndian);
                                    offset += size;
                                }
                            }
                        }
                        else if (fieldInfo.FieldType.GetElementType() == typeof(Int64))
                        {
                            int size = 8;

                            Int64[] array = (Int64[])fieldInfo.GetValue(obj);

                            if (array != null)
                            {
                                offset = obj._Pack(offset, size);

                                for (int i = 0; i < array.Length; i++)
                                {
                                    BytesTool.writeBytes(array[i], buffer, offset, size, obj.LittleEndian);
                                    offset += size;
                                }
                            }
                        }
                        else if (fieldInfo.FieldType.GetElementType() == typeof(UInt32))
                        {
                            int size = 4;

                            UInt32[] array = (UInt32[])fieldInfo.GetValue(obj);

                            if (array != null)
                            {
                                offset = obj._Pack(offset, size);

                                for (int i = 0; i < array.Length; i++)
                                {
                                    BytesTool.writeBytes(array[i], buffer, offset, size, obj.LittleEndian);
                                    offset += size;
                                }
                            }
                        }
                        else if (fieldInfo.FieldType.GetElementType() == typeof(Int32))
                        {
                            int size = 4;

                            Int32[] array = (Int32[])fieldInfo.GetValue(obj);

                            if (array != null)
                            {
                                offset = obj._Pack(offset, size);

                                for (int i = 0; i < array.Length; i++)
                                {
                                    BytesTool.writeBytes(array[i], buffer, offset, size, obj.LittleEndian);
                                    offset += size;
                                }
                            }
                        }
                        else if (fieldInfo.FieldType.GetElementType() == typeof(UInt16))
                        {
                            int size = 2;

                            UInt16[] array = (UInt16[])fieldInfo.GetValue(obj);

                            if (array != null)
                            {
                                offset = obj._Pack(offset, size);

                                for (int i = 0; i < array.Length; i++)
                                {
                                    BytesTool.writeBytes(array[i], buffer, offset, size, obj.LittleEndian);
                                    offset += size;
                                }
                            }
                        }
                        else if (fieldInfo.FieldType.GetElementType() == typeof(Int16))
                        {
                            int size = 2;

                            Int16[] array = (Int16[])fieldInfo.GetValue(obj);

                            if (array != null)
                            {
                                offset = obj._Pack(offset, size);

                                for (int i = 0; i < array.Length; i++)
                                {
                                    BytesTool.writeBytes(array[i], buffer, offset, size, obj.LittleEndian);
                                    offset += size;
                                }
                            }
                        }
                        else if (fieldInfo.FieldType.GetElementType() == typeof(Byte))
                        {
                            Byte[] array = (Byte[])fieldInfo.GetValue(obj);

                            if (array != null)
                            {
                                offset = obj._Pack(offset, 1);

                                for (int i = 0; i < array.Length; i++)
                                {
                                    buffer[offset++] = array[i];
                                }
                            }
                        }
                        else if (fieldInfo.FieldType.GetElementType() == typeof(SByte))
                        {
                            SByte[] array = (SByte[])fieldInfo.GetValue(obj);

                            if (array != null)
                            {
                                offset = obj._Pack(offset, 1);

                                for (int i = 0; i < array.Length; i++)
                                {
                                    buffer[offset++] = (Byte)array[i];
                                }
                            }
                        }
                        else if (fieldInfo.FieldType.GetElementType() == typeof(DataSection))
                        {
                            // todo
                        }
                    }
                    else if (fieldInfo.FieldType == typeof(Int64))
                    {
                        int size = 8;

                        offset = obj._Pack(offset, size);

                        Int64 val = (Int64)fieldInfo.GetValue(obj);

                        BytesTool.writeBytes(val, buffer, offset, size, obj.LittleEndian);

                        offset += size;
                    }
                    else if (fieldInfo.FieldType == typeof(UInt64))
                    {
                        int size = 8;

                        offset = obj._Pack(offset, size);

                        UInt64 val = (UInt64)fieldInfo.GetValue(obj);

                        BytesTool.writeBytes((Int64)val, buffer, offset, size, obj.LittleEndian);

                        offset += size;
                    }
                    else if (fieldInfo.FieldType == typeof(Int32))
                    {
                        int size = 4;

                        offset = obj._Pack(offset, size);

                        Int32 val = (Int32)fieldInfo.GetValue(obj);

                        BytesTool.writeBytes(val, buffer, offset, size, obj.LittleEndian);

                        offset += size;
                    }
                    else if (fieldInfo.FieldType == typeof(UInt32))
                    {
                        int size = 4;

                        offset = obj._Pack(offset, size);

                        UInt32 val = (UInt32)fieldInfo.GetValue(obj);

                        BytesTool.writeBytes(val, buffer, offset, size, obj.LittleEndian);

                        offset += size;
                    }
                    else if (fieldInfo.FieldType == typeof(Int16))
                    {
                        int size = 2;

                        offset = obj._Pack(offset, size);

                        Int16 val = (Int16)fieldInfo.GetValue(obj);

                        BytesTool.writeBytes(val, buffer, offset, size, obj.LittleEndian);

                        offset += size;
                    }
                    else if (fieldInfo.FieldType == typeof(UInt16))
                    {
                        int size = 2;

                        offset = obj._Pack(offset, size);

                        UInt16 val = (UInt16)fieldInfo.GetValue(obj);

                        BytesTool.writeBytes(val, buffer, offset, size, obj.LittleEndian);

                        offset += size;
                    }
                    else if (fieldInfo.FieldType == typeof(SByte))
                    {
                        offset = obj._Pack(offset, 1);

                        SByte val = (SByte)fieldInfo.GetValue(obj);

                        BytesTool.writeBytes(val, buffer, offset, 1, obj.LittleEndian);

                        offset++;
                    }
                    else if (fieldInfo.FieldType == typeof(Byte))
                    {
                        offset = obj._Pack(offset, 1);

                        Byte val = (Byte)fieldInfo.GetValue(obj);

                        BytesTool.writeBytes(val, buffer, offset, 1, obj.LittleEndian);

                        offset++;
                    }
                    else if (fieldInfo.FieldType == typeof(DataSection))
                    {
                        DataSection innerSection = (DataSection)fieldInfo.GetValue(obj);

                        offset = obj._Pack(offset, 4);

                        int innerRet = innerSection.Write(buffer, offset, length - offset);

                        if (innerRet != 0)
                        {
                            return(innerRet << 1);
                        }

                        offset += innerSection.Size();
                    }
                }
                catch
                {
                    return(2);
                }
            }

            return(0);
        }