Ejemplo n.º 1
0
        // Token: 0x060003F0 RID: 1008 RVA: 0x0000B3BC File Offset: 0x0000A3BC
        private int WriteField(Context context, Integer field, object fieldValue)
        {
            byte[] buffer = context.Buffer;
            int    num    = field.LengthSpecified ? field.Length : 4;
            bool   flag   = field.Signed;

            if (field.GetType() == typeof(Int8))
            {
                num  = 1;
                flag = true;
            }
            else if (field.GetType() == typeof(Int16))
            {
                num  = 2;
                flag = true;
            }
            else if (field.GetType() == typeof(Int32))
            {
                num  = 4;
                flag = true;
            }
            else if (field.GetType() == typeof(Int64))
            {
                num  = 8;
                flag = true;
            }
            else if (field.GetType() == typeof(UInt8))
            {
                num  = 1;
                flag = false;
            }
            else if (field.GetType() == typeof(UInt16))
            {
                num  = 2;
                flag = false;
            }
            else if (field.GetType() == typeof(UInt32))
            {
                num  = 4;
                flag = false;
            }
            else if (field.GetType() == typeof(UInt64))
            {
                num  = 8;
                flag = false;
            }
            if (buffer != null)
            {
                if (buffer.Length - context.Index < num)
                {
                    throw new InvalidDataToWriteException("Unexpected end of buffer.");
                }
                byte[] array;
                if (flag)
                {
                    int num2 = num;
                    switch (num2)
                    {
                    case 1:
                    {
                        array = new byte[1];
                        sbyte b = System.Convert.ToSByte(fieldValue);
                        if (b < 0)
                        {
                            array[0] = (byte)(255 + (int)b + 1);
                            goto IL_205;
                        }
                        array[0] = (byte)b;
                        goto IL_205;
                    }

                    case 2:
                        array = BitConverter.GetBytes(System.Convert.ToInt16(fieldValue));
                        goto IL_205;

                    case 3:
                        break;

                    case 4:
                        array = BitConverter.GetBytes(System.Convert.ToInt32(fieldValue));
                        goto IL_205;

                    default:
                        if (num2 == 8)
                        {
                            array = BitConverter.GetBytes(System.Convert.ToInt64(fieldValue));
                            goto IL_205;
                        }
                        break;
                    }
                    array = (byte[])fieldValue;
                }
                else
                {
                    int num3 = num;
                    switch (num3)
                    {
                    case 1:
                        array = new byte[]
                        {
                            System.Convert.ToByte(fieldValue)
                        };
                        goto IL_205;

                    case 2:
                        array = BitConverter.GetBytes(System.Convert.ToUInt16(fieldValue));
                        goto IL_205;

                    case 3:
                        break;

                    case 4:
                        array = BitConverter.GetBytes(System.Convert.ToUInt32(fieldValue));
                        goto IL_205;

                    default:
                        if (num3 == 8)
                        {
                            array = BitConverter.GetBytes(System.Convert.ToUInt64(fieldValue));
                            goto IL_205;
                        }
                        break;
                    }
                    array = (byte[])fieldValue;
                }
IL_205:
                if (context.BigEndian)
                {
                    base.SwapBytes(array, 0, num);
                }
                for (int i = 0; i < array.Length; i++)
                {
                    buffer[context.Index + i] = array[i];
                }
            }
            return(num);
        }
Ejemplo n.º 2
0
        /// <summary>
        /// Reads a integer value from the buffer.
        /// </summary>
        private int ReadField(Context context, Integer field, out object fieldValue)
        {
            fieldValue = null;

            byte[] buffer = context.Buffer;

            // initialize serialization paramters.
            int  length = (field.LengthSpecified)?(int)field.Length:4;
            bool signed = field.Signed;

            // apply defaults for built in types.
            if (field.GetType() == typeof(Opc.Cpx.Int8))
            {
                length = 1; signed = true;
            }
            else if (field.GetType() == typeof(Opc.Cpx.Int16))
            {
                length = 2; signed = true;
            }
            else if (field.GetType() == typeof(Opc.Cpx.Int32))
            {
                length = 4; signed = true;
            }
            else if (field.GetType() == typeof(Opc.Cpx.Int64))
            {
                length = 8; signed = true;
            }
            else if (field.GetType() == typeof(Opc.Cpx.UInt8))
            {
                length = 1; signed = false;
            }
            else if (field.GetType() == typeof(Opc.Cpx.UInt16))
            {
                length = 2; signed = false;
            }
            else if (field.GetType() == typeof(Opc.Cpx.UInt32))
            {
                length = 4; signed = false;
            }
            else if (field.GetType() == typeof(Opc.Cpx.UInt64))
            {
                length = 8; signed = false;
            }

            // check if there is enough data left.
            if (buffer.Length - context.Index < length)
            {
                throw new InvalidDataInBufferException("Unexpected end of buffer.");
            }

            // copy and swap bytes if required.
            byte[] bytes = new byte[length];

            for (int ii = 0; ii < length; ii++)
            {
                bytes[ii] = buffer[context.Index + ii];
            }

            if (context.BigEndian)
            {
                SwapBytes(bytes, 0, length);
            }

            // convert to object.
            if (signed)
            {
                switch (length)
                {
                case 1:
                {
                    if (bytes[0] < 128)
                    {
                        fieldValue = (sbyte)bytes[0];
                    }
                    else
                    {
                        fieldValue = (sbyte)(0 - bytes[0]);
                    }

                    break;
                }

                case 2:  { fieldValue = BitConverter.ToInt16(bytes, 0);   break; }

                case 4:  { fieldValue = BitConverter.ToInt32(bytes, 0);   break; }

                case 8:  { fieldValue = BitConverter.ToInt64(bytes, 0);   break; }

                default: { fieldValue = bytes;                            break; }
                }
            }
            else
            {
                switch (length)
                {
                case 1:  { fieldValue = bytes[0];                        break; }

                case 2:  { fieldValue = BitConverter.ToUInt16(bytes, 0); break; }

                case 4:  { fieldValue = BitConverter.ToUInt32(bytes, 0); break; }

                case 8:  { fieldValue = BitConverter.ToUInt64(bytes, 0); break; }

                default: { fieldValue = bytes;                           break; }
                }
            }

            return(length);
        }
Ejemplo n.º 3
0
        /// <summary>
        /// Writes a integer value from to the buffer.
        /// </summary>
        private int WriteField(Context context, Integer field, object fieldValue)
        {
            byte[] buffer = context.Buffer;

            // initialize serialization paramters.
            int  length = (field.LengthSpecified)?(int)field.Length:4;
            bool signed = field.Signed;

            // apply defaults for built in types.
            if (field.GetType() == typeof(Opc.Cpx.Int8))
            {
                length = 1; signed = true;
            }
            else if (field.GetType() == typeof(Opc.Cpx.Int16))
            {
                length = 2; signed = true;
            }
            else if (field.GetType() == typeof(Opc.Cpx.Int32))
            {
                length = 4; signed = true;
            }
            else if (field.GetType() == typeof(Opc.Cpx.Int64))
            {
                length = 8; signed = true;
            }
            else if (field.GetType() == typeof(Opc.Cpx.UInt8))
            {
                length = 1; signed = false;
            }
            else if (field.GetType() == typeof(Opc.Cpx.UInt16))
            {
                length = 2; signed = false;
            }
            else if (field.GetType() == typeof(Opc.Cpx.UInt32))
            {
                length = 4; signed = false;
            }
            else if (field.GetType() == typeof(Opc.Cpx.UInt64))
            {
                length = 8; signed = false;
            }

            // only write to the buffer if it has been allocated.
            if (buffer != null)
            {
                // check if there is enough data left.
                if (buffer.Length - context.Index < length)
                {
                    throw new InvalidDataToWriteException("Unexpected end of buffer.");
                }

                // copy and swap bytes if required.
                byte[] bytes = null;

                if (signed)
                {
                    switch (length)
                    {
                    case 1:
                    {
                        bytes = new byte[1];

                        sbyte value = System.Convert.ToSByte(fieldValue);

                        if (value < 0)
                        {
                            bytes[0] = (byte)(Byte.MaxValue + value + 1);
                        }
                        else
                        {
                            bytes[0] = (byte)value;
                        }

                        break;
                    }

                    case 2:  { bytes = BitConverter.GetBytes(System.Convert.ToInt16(fieldValue)); break; }

                    case 4:  { bytes = BitConverter.GetBytes(System.Convert.ToInt32(fieldValue)); break; }

                    case 8:  { bytes = BitConverter.GetBytes(System.Convert.ToInt64(fieldValue)); break; }

                    default: { bytes = (byte[])fieldValue;                                        break; }
                    }
                }
                else
                {
                    switch (length)
                    {
                    case 1:  { bytes = new byte[] { System.Convert.ToByte(fieldValue) };           break; }

                    case 2:  { bytes = BitConverter.GetBytes(System.Convert.ToUInt16(fieldValue)); break; }

                    case 4:  { bytes = BitConverter.GetBytes(System.Convert.ToUInt32(fieldValue)); break; }

                    case 8:  { bytes = BitConverter.GetBytes(System.Convert.ToUInt64(fieldValue)); break; }

                    default: { bytes = (byte[])fieldValue;                                         break; }
                    }
                }

                // copy and swap bytes.
                if (context.BigEndian)
                {
                    SwapBytes(bytes, 0, length);
                }

                // write bytes to buffer.
                for (int ii = 0; ii < bytes.Length; ii++)
                {
                    buffer[context.Index + ii] = bytes[ii];
                }
            }

            return(length);
        }
Ejemplo n.º 4
0
        // Token: 0x0600036C RID: 876 RVA: 0x00009AEC File Offset: 0x00008AEC
        private int ReadField(Context context, Integer field, out object fieldValue)
        {
            fieldValue = null;
            byte[] buffer = context.Buffer;
            int    num    = field.LengthSpecified ? field.Length : 4;
            bool   flag   = field.Signed;

            if (field.GetType() == typeof(Int8))
            {
                num  = 1;
                flag = true;
            }
            else if (field.GetType() == typeof(Int16))
            {
                num  = 2;
                flag = true;
            }
            else if (field.GetType() == typeof(Int32))
            {
                num  = 4;
                flag = true;
            }
            else if (field.GetType() == typeof(Int64))
            {
                num  = 8;
                flag = true;
            }
            else if (field.GetType() == typeof(UInt8))
            {
                num  = 1;
                flag = false;
            }
            else if (field.GetType() == typeof(UInt16))
            {
                num  = 2;
                flag = false;
            }
            else if (field.GetType() == typeof(UInt32))
            {
                num  = 4;
                flag = false;
            }
            else if (field.GetType() == typeof(UInt64))
            {
                num  = 8;
                flag = false;
            }
            if (buffer.Length - context.Index < num)
            {
                throw new InvalidDataInBufferException("Unexpected end of buffer.");
            }
            byte[] array = new byte[num];
            for (int i = 0; i < num; i++)
            {
                array[i] = buffer[context.Index + i];
            }
            if (context.BigEndian)
            {
                base.SwapBytes(array, 0, num);
            }
            if (flag)
            {
                int num2 = num;
                switch (num2)
                {
                case 1:
                    if (array[0] < 128)
                    {
                        fieldValue = (sbyte)array[0];
                        return(num);
                    }
                    fieldValue = (sbyte)(-(sbyte)array[0]);
                    return(num);

                case 2:
                    fieldValue = BitConverter.ToInt16(array, 0);
                    return(num);

                case 3:
                    break;

                case 4:
                    fieldValue = BitConverter.ToInt32(array, 0);
                    return(num);

                default:
                    if (num2 == 8)
                    {
                        fieldValue = BitConverter.ToInt64(array, 0);
                        return(num);
                    }
                    break;
                }
                fieldValue = array;
            }
            else
            {
                int num3 = num;
                switch (num3)
                {
                case 1:
                    fieldValue = array[0];
                    return(num);

                case 2:
                    fieldValue = BitConverter.ToUInt16(array, 0);
                    return(num);

                case 3:
                    break;

                case 4:
                    fieldValue = BitConverter.ToUInt32(array, 0);
                    return(num);

                default:
                    if (num3 == 8)
                    {
                        fieldValue = BitConverter.ToUInt64(array, 0);
                        return(num);
                    }
                    break;
                }
                fieldValue = array;
            }
            return(num);
        }