コード例 #1
0
        // Token: 0x060003F2 RID: 1010 RVA: 0x0000B708 File Offset: 0x0000A708
        private int WriteField(Context context, CharString field, int fieldIndex, ComplexValue[] fieldValues, object fieldValue)
        {
            byte[] buffer = context.Buffer;
            int    num    = field.CharWidthSpecified ? field.CharWidth : context.CharWidth;
            int    num2   = field.LengthSpecified ? field.Length : -1;

            if (field.GetType() == typeof(Ascii))
            {
                num = 1;
            }
            else if (field.GetType() == typeof(Unicode))
            {
                num = 2;
            }
            byte[] array = null;
            if (num2 == -1)
            {
                if (num > 2)
                {
                    if (fieldValue.GetType() != typeof(byte[]))
                    {
                        throw new InvalidDataToWriteException("Field value is not a byte array.");
                    }
                    array = (byte[])fieldValue;
                    num2  = array.Length / num;
                }
                else
                {
                    if (fieldValue.GetType() != typeof(string))
                    {
                        throw new InvalidDataToWriteException("Field value is not a string.");
                    }
                    string text = (string)fieldValue;
                    num2 = text.Length + 1;
                    if (num == 1)
                    {
                        num2 = 1;
                        foreach (char value in text)
                        {
                            num2++;
                            byte[] bytes = BitConverter.GetBytes(value);
                            if (bytes[1] != 0)
                            {
                                num2++;
                            }
                        }
                    }
                }
            }
            if (field.CharCountRef != null)
            {
                this.WriteReference(context, field, fieldIndex, fieldValues, field.CharCountRef, num2);
            }
            if (buffer != null)
            {
                if (array == null)
                {
                    string text3 = (string)fieldValue;
                    array = new byte[num * num2];
                    int num3 = 0;
                    int num4 = 0;
                    while (num4 < text3.Length && num3 < array.Length)
                    {
                        byte[] bytes2 = BitConverter.GetBytes(text3[num4]);
                        array[num3++] = bytes2[0];
                        if (num == 2 || bytes2[1] != 0)
                        {
                            array[num3++] = bytes2[1];
                        }
                        num4++;
                    }
                }
                if (buffer.Length - context.Index < array.Length)
                {
                    throw new InvalidDataToWriteException("Unexpected end of buffer.");
                }
                for (int j = 0; j < array.Length; j++)
                {
                    buffer[context.Index + j] = array[j];
                }
                if (context.BigEndian && num > 1)
                {
                    for (int k = 0; k < array.Length; k += num)
                    {
                        base.SwapBytes(buffer, context.Index + k, num);
                    }
                }
            }
            return(num2 * num);
        }
コード例 #2
0
        /// <summary>
        /// Reads a char string value from the buffer.
        /// </summary>
        private int ReadField(
            Context context,
            CharString field,
            int fieldIndex,
            ArrayList fieldValues,
            out object fieldValue
            )
        {
            fieldValue = null;

            byte[] buffer = context.Buffer;

            // initialize serialization parameters.
            int charWidth = (field.CharWidthSpecified)?(int)field.CharWidth:(int)context.CharWidth;
            int charCount = (field.LengthSpecified)?(int)field.Length:-1;

            // apply defaults for built in types.
            if (field.GetType() == typeof(Opc.Cpx.Ascii))
            {
                charWidth = 1;
            }
            else if (field.GetType() == typeof(Opc.Cpx.Unicode))
            {
                charWidth = 2;
            }

            if (field.CharCountRef != null)
            {
                charCount = ReadReference(context, field, fieldIndex, fieldValues, field.CharCountRef);
            }

            // find null terminator
            if (charCount == -1)
            {
                charCount = 0;

                for (int ii = context.Index; ii < context.Buffer.Length - charWidth + 1; ii += charWidth)
                {
                    charCount++;

                    bool isNull = true;

                    for (int jj = 0; jj < charWidth; jj++)
                    {
                        if (context.Buffer[ii + jj] != 0)
                        {
                            isNull = false;
                            break;
                        }
                    }

                    if (isNull)
                    {
                        break;
                    }
                }
            }

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

            if (charWidth > 2)
            {
                // copy bytes.
                byte[] bytes = new byte[charCount * charWidth];

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

                // swap bytes.
                if (context.BigEndian)
                {
                    for (int ii = 0; ii < bytes.Length; ii += charWidth)
                    {
                        SwapBytes(bytes, 0, charWidth);
                    }
                }

                fieldValue = bytes;
            }
            else
            {
                // copy characters.
                char[] chars = new char[charCount];

                for (int ii = 0; ii < charCount; ii++)
                {
                    if (charWidth == 1)
                    {
                        chars[ii] = System.Convert.ToChar(buffer[context.Index + ii]);
                    }
                    else
                    {
                        byte[] charBytes = new byte[]
                        {
                            buffer[context.Index + 2 * ii],
                            buffer[context.Index + 2 * ii + 1]
                        };

                        if (context.BigEndian)
                        {
                            SwapBytes(charBytes, 0, 2);
                        }

                        chars[ii] = BitConverter.ToChar(charBytes, 0);
                    }
                }

                fieldValue = new string(chars).TrimEnd(new char[] { '\0' });
            }

            return(charCount * charWidth);
        }
コード例 #3
0
        /// <summary>
        /// Writes a char string value to the buffer.
        /// </summary>
        private int WriteField(
            Context context,
            CharString field,
            int fieldIndex,
            ComplexValue[] fieldValues,
            object fieldValue
            )
        {
            byte[] buffer = context.Buffer;

            // initialize serialization parameters.
            int charWidth = (field.CharWidthSpecified)?(int)field.CharWidth:(int)context.CharWidth;
            int charCount = (field.LengthSpecified)?(int)field.Length:-1;

            // apply defaults for built in types.
            if (field.GetType() == typeof(Opc.Cpx.Ascii))
            {
                charWidth = 1;
            }
            else if (field.GetType() == typeof(Opc.Cpx.Unicode))
            {
                charWidth = 2;
            }

            byte[] bytes = null;

            if (charCount == -1)
            {
                // extra wide characters stored as byte arrays
                if (charWidth > 2)
                {
                    if (fieldValue.GetType() != typeof(byte[]))
                    {
                        throw new InvalidDataToWriteException("Field value is not a byte array.");
                    }

                    bytes     = (byte[])fieldValue;
                    charCount = bytes.Length / charWidth;
                }

                // convert string to byte array.
                else
                {
                    if (fieldValue.GetType() != typeof(string))
                    {
                        throw new InvalidDataToWriteException("Field value is not a string.");
                    }

                    string stringValue = (string)fieldValue;

                    charCount = stringValue.Length + 1;

                    // calculate length of ascii string by forcing pure unicode characters to two ascii chars.
                    if (charWidth == 1)
                    {
                        charCount = 1;

                        foreach (char unicodeChar in stringValue)
                        {
                            charCount++;

                            byte[] charBytes = BitConverter.GetBytes(unicodeChar);

                            if (charBytes[1] != 0)
                            {
                                charCount++;
                            }
                        }
                    }
                }
            }

            // update the char count reference.
            if (field.CharCountRef != null)
            {
                WriteReference(context, field, fieldIndex, fieldValues, field.CharCountRef, charCount);
            }

            if (buffer != null)
            {
                // copy string to buffer.
                if (bytes == null)
                {
                    string stringValue = (string)fieldValue;

                    bytes = new byte[charWidth * charCount];

                    int index = 0;

                    for (int ii = 0; ii < stringValue.Length; ii++)
                    {
                        if (index >= bytes.Length)
                        {
                            break;
                        }

                        byte[] charBytes = BitConverter.GetBytes(stringValue[ii]);

                        bytes[index++] = charBytes[0];

                        if (charWidth == 2 || charBytes[1] != 0)
                        {
                            bytes[index++] = charBytes[1];
                        }
                    }
                }

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

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

                // swap bytes.
                if (context.BigEndian && charWidth > 1)
                {
                    for (int ii = 0; ii < bytes.Length; ii += charWidth)
                    {
                        SwapBytes(buffer, context.Index + ii, charWidth);
                    }
                }
            }

            return(charCount * charWidth);
        }
コード例 #4
0
ファイル: BinaryReader.cs プロジェクト: ZSYMAX/OpcNetApi
        // Token: 0x0600036E RID: 878 RVA: 0x00009E24 File Offset: 0x00008E24
        private int ReadField(Context context, CharString field, int fieldIndex, ArrayList fieldValues, out object fieldValue)
        {
            fieldValue = null;
            byte[] buffer = context.Buffer;
            int    num    = field.CharWidthSpecified ? field.CharWidth : context.CharWidth;
            int    num2   = field.LengthSpecified ? field.Length : -1;

            if (field.GetType() == typeof(Ascii))
            {
                num = 1;
            }
            else if (field.GetType() == typeof(Unicode))
            {
                num = 2;
            }
            if (field.CharCountRef != null)
            {
                num2 = this.ReadReference(context, field, fieldIndex, fieldValues, field.CharCountRef);
            }
            if (num2 == -1)
            {
                num2 = 0;
                for (int i = context.Index; i < context.Buffer.Length - num + 1; i += num)
                {
                    num2++;
                    bool flag = true;
                    for (int j = 0; j < num; j++)
                    {
                        if (context.Buffer[i + j] != 0)
                        {
                            flag = false;
                            break;
                        }
                    }
                    if (flag)
                    {
                        break;
                    }
                }
            }
            if (buffer.Length - context.Index < num * num2)
            {
                throw new InvalidDataInBufferException("Unexpected end of buffer.");
            }
            if (num > 2)
            {
                byte[] array = new byte[num2 * num];
                for (int k = 0; k < num2 * num; k++)
                {
                    array[k] = buffer[context.Index + k];
                }
                if (context.BigEndian)
                {
                    for (int l = 0; l < array.Length; l += num)
                    {
                        base.SwapBytes(array, 0, num);
                    }
                }
                fieldValue = array;
            }
            else
            {
                char[] array2 = new char[num2];
                for (int m = 0; m < num2; m++)
                {
                    if (num == 1)
                    {
                        array2[m] = System.Convert.ToChar(buffer[context.Index + m]);
                    }
                    else
                    {
                        byte[] array3 = new byte[]
                        {
                            buffer[context.Index + 2 * m],
                            buffer[context.Index + 2 * m + 1]
                        };
                        if (context.BigEndian)
                        {
                            base.SwapBytes(array3, 0, 2);
                        }
                        array2[m] = BitConverter.ToChar(array3, 0);
                    }
                }
                string text      = new string(array2);
                char[] trimChars = new char[1];
                fieldValue = text.TrimEnd(trimChars);
            }
            return(num2 * num);
        }