Exemplo n.º 1
0
        private int ReadField(Context context, CharString field, int fieldIndex, ArrayList fieldValues, out object fieldValue)
        {
            fieldValue = null;
            byte[] buffer = context.Buffer;
            int    length = field.CharWidthSpecified ? field.CharWidth : context.CharWidth;
            int    num2   = field.LengthSpecified ? field.Length : -1;

            if (field.GetType() == typeof(Ascii))
            {
                length = 1;
            }
            else if (field.GetType() == typeof(Unicode))
            {
                length = 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 - length) + 1); i += length)
                {
                    num2++;
                    bool flag = true;
                    for (int j = 0; j < length; j++)
                    {
                        if (context.Buffer[i + j] != 0)
                        {
                            flag = false;
                            break;
                        }
                    }
                    if (flag)
                    {
                        break;
                    }
                }
            }
            if ((buffer.Length - context.Index) < (length * num2))
            {
                throw new InvalidDataInBufferException("Unexpected end of buffer.");
            }
            if (length > 2)
            {
                byte[] bytes = new byte[num2 * length];
                for (int k = 0; k < (num2 * length); k++)
                {
                    bytes[k] = buffer[context.Index + k];
                }
                if (context.BigEndian)
                {
                    for (int m = 0; m < bytes.Length; m += length)
                    {
                        base.SwapBytes(bytes, 0, length);
                    }
                }
                fieldValue = bytes;
            }
            else
            {
                char[] chArray = new char[num2];
                for (int n = 0; n < num2; n++)
                {
                    if (length == 1)
                    {
                        chArray[n] = System.Convert.ToChar(buffer[context.Index + n]);
                    }
                    else
                    {
                        byte[] buffer3 = new byte[] { buffer[context.Index + (2 * n)], buffer[(context.Index + (2 * n)) + 1] };
                        if (context.BigEndian)
                        {
                            base.SwapBytes(buffer3, 0, 2);
                        }
                        chArray[n] = BitConverter.ToChar(buffer3, 0);
                    }
                }
                fieldValue = new string(chArray).TrimEnd(new char[1]);
            }
            return(num2 * length);
        }
Exemplo n.º 2
0
        private int WriteField(Context context, CharString field, int fieldIndex, ComplexValue[] fieldValues, object fieldValue)
        {
            byte[] bytes  = context.Buffer;
            int    length = field.CharWidthSpecified ? field.CharWidth : context.CharWidth;
            int    count  = field.LengthSpecified ? field.Length : -1;

            if (field.GetType() == typeof(Ascii))
            {
                length = 1;
            }
            else if (field.GetType() == typeof(Unicode))
            {
                length = 2;
            }
            byte[] buffer2 = null;
            if (count == -1)
            {
                if (length > 2)
                {
                    if (fieldValue.GetType() != typeof(byte[]))
                    {
                        throw new InvalidDataToWriteException("Field value is not a byte array.");
                    }
                    buffer2 = (byte[])fieldValue;
                    count   = buffer2.Length / length;
                }
                else
                {
                    if (fieldValue.GetType() != typeof(string))
                    {
                        throw new InvalidDataToWriteException("Field value is not a string.");
                    }
                    string str = (string)fieldValue;
                    count = str.Length + 1;
                    if (length == 1)
                    {
                        count = 1;
                        foreach (char ch in str)
                        {
                            count++;
                            if (BitConverter.GetBytes(ch)[1] != 0)
                            {
                                count++;
                            }
                        }
                    }
                }
            }
            if (field.CharCountRef != null)
            {
                this.WriteReference(context, field, fieldIndex, fieldValues, field.CharCountRef, count);
            }
            if (bytes != null)
            {
                if (buffer2 == null)
                {
                    string str2 = (string)fieldValue;
                    buffer2 = new byte[length * count];
                    int num3 = 0;
                    for (int j = 0; j < str2.Length; j++)
                    {
                        if (num3 >= buffer2.Length)
                        {
                            break;
                        }
                        byte[] buffer4 = BitConverter.GetBytes(str2[j]);
                        buffer2[num3++] = buffer4[0];
                        if ((length == 2) || (buffer4[1] != 0))
                        {
                            buffer2[num3++] = buffer4[1];
                        }
                    }
                }
                if ((bytes.Length - context.Index) < buffer2.Length)
                {
                    throw new InvalidDataToWriteException("Unexpected end of buffer.");
                }
                for (int i = 0; i < buffer2.Length; i++)
                {
                    bytes[context.Index + i] = buffer2[i];
                }
                if (context.BigEndian && (length > 1))
                {
                    for (int k = 0; k < buffer2.Length; k += length)
                    {
                        base.SwapBytes(bytes, context.Index + k, length);
                    }
                }
            }
            return(count * length);
        }