Microsoft.Protocols.TestSuites.MS_OXCMAPIHTTP.AddressBookPropertyValue.Parse C# (CSharp) Method

Parse() public method

Parse bytes in context into a PropertyValueNode
public Parse ( Context context ) : void
context Context The value of Context
return void
        public override void Parse(Context context)
        {
            // Current processing property's Type
            PropertyType type = context.CurProperty.Type;
            int strBytesLen = 0;
            bool isFound = false;

            switch (type)
            {
                // 1 Byte
                case PropertyType.PtypBoolean:
                    if (context.AvailBytes() < sizeof(byte))
                    {
                        throw new ParseException("Not well formed PtypBoolean");
                    }
                    else
                    {
                        this.value = new byte[sizeof(byte)];
                        Array.Copy(context.PropertyBytes, context.CurIndex, this.value, 0, sizeof(byte));
                        context.CurIndex += sizeof(byte);
                    }

                    break;
                case PropertyType.PtypInteger16:
                    if (context.AvailBytes() < sizeof(short))
                    {
                        throw new ParseException("Not well formed PtypInteger16");
                    }
                    else
                    {
                        this.value = new byte[sizeof(short)];
                        Array.Copy(context.PropertyBytes, context.CurIndex, this.value, 0, sizeof(short));
                        context.CurIndex += sizeof(short);
                    }

                    break;
                case PropertyType.PtypInteger32:
                case PropertyType.PtypFloating32:
                case PropertyType.PtypErrorCode:
                    if (context.AvailBytes() < sizeof(int))
                    {
                        throw new ParseException("Not well formed PtypInteger32");
                    }
                    else
                    {
                        // Assign value of int to property's value
                        this.value = new byte[sizeof(int)];
                        Array.Copy(context.PropertyBytes, context.CurIndex, this.value, 0, sizeof(int));
                        context.CurIndex += sizeof(int);
                    }

                    break;
                case PropertyType.PtypFloating64:
                case PropertyType.PtypCurrency:
                case PropertyType.PtypFloatingTime:
                case PropertyType.PtypInteger64:
                case PropertyType.PtypTime:
                    if (context.AvailBytes() < sizeof(long))
                    {
                        throw new ParseException("Not well formed PtypInteger64");
                    }
                    else
                    {
                        // Assign value of Int64 to property's value
                        this.value = new byte[sizeof(long)];
                        Array.Copy(context.PropertyBytes, context.CurIndex, this.value, 0, sizeof(long));
                        context.CurIndex += sizeof(long);
                    }

                    break;
                case PropertyType.PtypGuid:
                    if (context.AvailBytes() < sizeof(byte) * 16)
                    {
                        throw new ParseException("Not well formed 16 PtyGuid");
                    }
                    else
                    {
                        // Assign value of Int64 to property's value
                        this.value = new byte[sizeof(byte) * 16];
                        Array.Copy(context.PropertyBytes, context.CurIndex, this.value, 0, sizeof(byte) * 16);
                        context.CurIndex += sizeof(byte) * 16;
                    }

                    break;
                case PropertyType.PtypBinary:
                    if (context.AvailBytes() < sizeof(short))
                    {
                        throw new ParseException("Not well formed PtypBinary");
                    }
                    else
                    {
                        this.hasValue = context.PropertyBytes[context.CurIndex];
                        context.CurIndex++;
                        // Property start with "FF"
                        if (context.PropertyBytes[context.CurIndex] == (byte)0xFF)
                        {
                            context.CurIndex++;
                        }
                        if (this.hasValue == 0xff)
                        {
                            // First parse the count of the binary bytes
                            int bytesCount = BitConverter.ToInt32(context.PropertyBytes, context.CurIndex);
                            this.value = new byte[sizeof(int) + bytesCount];
                            Array.Copy(context.PropertyBytes, context.CurIndex, this.value, 0, sizeof(int));
                            context.CurIndex += sizeof(int);

                            // Then parse the binary bytes
                            if (bytesCount == 0)
                            {
                                this.value = null;
                            }
                            else
                            {
                                Array.Copy(context.PropertyBytes, context.CurIndex, this.value, sizeof(int), bytesCount);
                                context.CurIndex += bytesCount;
                            }
                        }
                    }

                    break;
                case PropertyType.PtypMultipleInteger16:
                    if (context.AvailBytes() < sizeof(short))
                    {
                        throw new ParseException("Not well formed PtypMultipleInterger");
                    }
                    else
                    {
                        short bytesCount = BitConverter.ToInt16(context.PropertyBytes, context.CurIndex);
                        this.value = new byte[sizeof(short) + bytesCount * sizeof(short)];
                        Array.Copy(context.PropertyBytes, context.CurIndex, this.value, 0, sizeof(short));
                        context.CurIndex += sizeof(short);
                        if (bytesCount == 0)
                        {
                            this.value = null;
                        }
                        else
                        {
                            Array.Copy(context.PropertyBytes, context.CurIndex, this.value, sizeof(short), bytesCount * sizeof(short));
                            context.CurIndex += bytesCount * sizeof(short);
                        }
                    }

                    break;
                case PropertyType.PtypMultipleInteger32:
                case PropertyType.PtypMultipleFloating32:
                    if (context.AvailBytes() < sizeof(short))
                    {
                        throw new ParseException("Not well formed " + type.ToString("g"));
                    }
                    else
                    {
                        short bytesCount = BitConverter.ToInt16(context.PropertyBytes, context.CurIndex);
                        this.value = new byte[sizeof(short) + bytesCount * sizeof(int)];
                        Array.Copy(context.PropertyBytes, context.CurIndex, this.value, 0, sizeof(short));
                        context.CurIndex += sizeof(short);
                        if (bytesCount == 0)
                        {
                            this.value = null;
                        }
                        else
                        {
                            Array.Copy(context.PropertyBytes, context.CurIndex, this.value, sizeof(short), bytesCount * sizeof(int));
                            context.CurIndex += bytesCount * sizeof(int);
                        }
                    }

                    break;
                case PropertyType.PtypMultipleFloating64:
                case PropertyType.PtypMultipleCurrency:
                case PropertyType.PtypMultipleFloatingTime:
                case PropertyType.PtypMultipleInteger64:
                case PropertyType.PtypMultipleTime:
                    if (context.AvailBytes() < sizeof(short))
                    {
                        throw new ParseException("Not well formed " + type.ToString("g"));
                    }
                    else
                    {
                        short bytesCount = BitConverter.ToInt16(context.PropertyBytes, context.CurIndex);
                        this.value = new byte[sizeof(short) + bytesCount * sizeof(long)];
                        Array.Copy(context.PropertyBytes, context.CurIndex, this.value, 0, sizeof(short));
                        context.CurIndex += sizeof(short);
                        if (bytesCount == 0)
                        {
                            this.value = null;
                        }
                        else
                        {
                            Array.Copy(context.PropertyBytes, context.CurIndex, this.value, sizeof(short), bytesCount * sizeof(long));
                            context.CurIndex += bytesCount * sizeof(long);
                        }
                    }

                    break;
                case PropertyType.PtypMultipleGuid:
                    if (context.AvailBytes() < sizeof(short))
                    {
                        throw new ParseException("Not well formed PtypMultipleGuid.");
                    }
                    else
                    {
                        short bytesCount = BitConverter.ToInt16(context.PropertyBytes, context.CurIndex);
                        this.value = new byte[sizeof(short) + bytesCount * 16];
                        Array.Copy(context.PropertyBytes, context.CurIndex, this.value, 0, sizeof(short));
                        context.CurIndex += sizeof(short);
                        if (bytesCount == 0)
                        {
                            this.value = null;
                        }
                        else
                        {
                            Array.Copy(context.PropertyBytes, context.CurIndex, this.value, sizeof(short), bytesCount * 16);
                            context.CurIndex += bytesCount * 16;
                        }
                    }

                    break;
                case PropertyType.PtypString8:
                    // The length in bytes of the unicode string to parse
                    strBytesLen = 0;
                    isFound = false;
                    this.hasValue = context.PropertyBytes[context.CurIndex];
                    context.CurIndex++;
                    if (this.hasValue == (byte)0xFF)
                    {
                        // Find the string with '\0' end
                        for (int i = context.CurIndex; i < context.PropertyBytes.Length; i++)
                        {
                            strBytesLen++;
                            if (context.PropertyBytes[i] == 0)
                            {
                                isFound = true;
                                break;
                            }
                        }

                        if (!isFound)
                        {
                            throw new ParseException("String too long or not found");
                        }
                        else
                        {
                            this.value = new byte[strBytesLen];
                            Array.Copy(context.PropertyBytes, context.CurIndex, this.value, 0, strBytesLen);
                            context.CurIndex += strBytesLen;
                        }
                    }
                    break;
                case PropertyType.PtypString:
                    // The length in bytes of the unicode string to parse
                    strBytesLen = 0;
                    isFound = false;
                    this.hasValue = context.PropertyBytes[context.CurIndex];
                    context.CurIndex++;

                    if (this.hasValue == (byte)0xFF)
                    {
                        // Find the string with '\0''\0' end
                        for (int i = context.CurIndex; i < context.PropertyBytes.Length; i += 2)
                        {
                            strBytesLen += 2;
                            if ((context.PropertyBytes[i] == 0) && (context.PropertyBytes[i + 1] == 0))
                            {
                                isFound = true;
                                break;
                            }
                        }

                        if (!isFound)
                        {
                            throw new ParseException("String too long or not found");
                        }
                        else
                        {
                            this.value = new byte[strBytesLen];
                            Array.Copy(context.PropertyBytes, context.CurIndex, this.value, 0, strBytesLen);
                            context.CurIndex += strBytesLen;
                        }
                    }
                    break;
                case PropertyType.PtypMultipleString:
                    if (context.AvailBytes() < sizeof(int))
                    {
                        throw new FormatException("Not well formed PtypMultipleString");
                    }
                    else
                    {
                        strBytesLen = 0;
                        isFound = false;

                        this.hasValue = context.PropertyBytes[context.CurIndex];
                        context.CurIndex++;
                        if (this.hasValue == (byte)0xFF)
                        {
                            int stringCount = BitConverter.ToInt32(context.PropertyBytes, context.CurIndex);
                            context.CurIndex += sizeof(int);
                            if (stringCount == 0)
                            {
                                value = null;
                                break;
                            }

                            for (int i = context.CurIndex; i < context.PropertyBytes.Length; i += 2)
                            {
                                strBytesLen += 2;
                                if ((context.PropertyBytes[i] == 0) && (context.PropertyBytes[i + 1] == 0))
                                {
                                    stringCount--;
                                }

                                if (stringCount == 0)
                                {
                                    isFound = true;
                                    break;
                                }
                            }

                            if (!isFound)
                            {
                                throw new FormatException("String too long or not found");
                            }
                            else
                            {
                                value = new byte[strBytesLen];
                                Array.Copy(context.PropertyBytes, context.CurIndex, value, 0, strBytesLen);
                                context.CurIndex += strBytesLen;
                            }
                        }
                    }
                    break;
                case PropertyType.PtypMultipleString8:
                    if (context.AvailBytes() < sizeof(int))
                    {
                        throw new FormatException("Not well formed PtypMultipleString8");
                    }
                    else
                    {
                        strBytesLen = 0;
                        isFound = false;
                        this.hasValue = context.PropertyBytes[context.CurIndex];
                        context.CurIndex++;
                        if (this.hasValue == (byte)0xFF)
                        {
                            int stringCount = BitConverter.ToInt32(context.PropertyBytes, context.CurIndex);
                            context.CurIndex += sizeof(int);
                            if (stringCount == 0)
                            {
                                value = null;
                                break;
                            }

                            for (int i = context.CurIndex; i < context.PropertyBytes.Length; i++)
                            {
                                strBytesLen++;
                                if (context.PropertyBytes[i] == 0)
                                {
                                    stringCount--;
                                }

                                if (stringCount == 0)
                                {
                                    isFound = true;
                                    break;
                                }
                            }

                            if (!isFound)
                            {
                                throw new FormatException("String too long or not found");
                            }
                            else
                            {
                                value = new byte[strBytesLen];
                                Array.Copy(context.PropertyBytes, context.CurIndex, value, 0, strBytesLen);
                                context.CurIndex += strBytesLen;
                            }
                        }
                    }
                    break;
                case PropertyType.PtypRuleAction:
                    // Length of the property
                    int felength = 0;

                    // Length of the action blocks
                    short actionBolcksLength = BitConverter.ToInt16(context.PropertyBytes, context.CurIndex);
                    felength += 2;
                    short actionBlockLength = 0;
                    for (int i = 0; i < actionBolcksLength; i++)
                    {
                        actionBlockLength = BitConverter.ToInt16(context.PropertyBytes, context.CurIndex + felength);
                        felength += 2 + actionBlockLength;
                    }

                    this.value = new byte[felength];
                    Array.Copy(context.PropertyBytes, context.CurIndex, this.value, 0, felength);
                    context.CurIndex += felength;
                    break;
                case PropertyType.PtypServerId:
                    if (context.AvailBytes() < sizeof(short))
                    {
                        throw new ParseException("Not well formed PtypServerId");
                    }
                    else
                    {
                        this.value = new byte[sizeof(short) + 21 * sizeof(byte)];
                        Array.Copy(context.PropertyBytes, context.CurIndex, this.value, 0, sizeof(short) + 21);

                        context.CurIndex += 21 + sizeof(short);
                    }

                    break;
                case PropertyType.PtypMultipleBinary:
                    if (context.AvailBytes() < sizeof(int))
                    {
                        throw new ParseException("Not well formed PtypMultipleBinary");
                    }
                    else
                    {
                        this.hasValue = context.PropertyBytes[context.CurIndex];
                        context.CurIndex++;
                        if (this.hasValue == (byte)0xFF)
                        {
                            int bytesCount = BitConverter.ToInt32(context.PropertyBytes, context.CurIndex);
                            context.CurIndex += sizeof(int);
                            for (int ibin = 0; ibin < bytesCount; ibin++)
                            {
                                // Property start with "FF"
                                if (context.PropertyBytes[context.CurIndex] == (byte)0xFF)
                                {
                                    context.CurIndex++;
                                }

                                int binLength = BitConverter.ToInt32(context.PropertyBytes, context.CurIndex);
                                context.CurIndex += sizeof(int);
                                if (binLength > 0)
                                {
                                    context.CurIndex += sizeof(byte) * binLength;
                                }
                            }
                        }
                    }
                    break;

                default:
                    throw new FormatException("Type " + type.ToString() + " not found or not support.");
            }
        }
    }

Usage Example

コード例 #1
0
        /// <summary>
        /// Parse the AddressBookPropertyRow structure.
        /// </summary>
        /// <param name="rawBuffer">The raw data returned from server.</param>
        /// <param name="propTagArray">The list of property tags.</param>
        ///  <param name="index">The start index.</param>
        /// <returns>Return an instance of AddressBookPropertyRow.</returns>
        public static AddressBookPropertyRow Parse(byte[] rawBuffer, LargePropertyTagArray propTagArray, ref int index)
        {
            AddressBookPropertyRow addressBookPropertyRow = new AddressBookPropertyRow();

            addressBookPropertyRow.Flag = rawBuffer[index];
            index++;
            List <AddressBookPropertyValue> valueArray = new List <AddressBookPropertyValue>();

            Context.Instance.PropertyBytes = rawBuffer;
            Context.Instance.CurIndex      = index;
            Context.Instance.CurProperty   = new Property(PropertyType.PtypUnspecified);

            // If the value of the Flags field is set to 0x00: The array contains either a AddressBookPropertyValue structure, or a AddressBookTypedPropertyValue structure.
            // If the value of the Flags field is set to 0x01: The array contains either a AddressBookFlaggedPropertyValue structure, or a AddressBookFlaggedPropertyValueWithType structure.
            if (addressBookPropertyRow.Flag == 0x00)
            {
                foreach (PropertyTag propertyTag in propTagArray.PropertyTags)
                {
                    if (propertyTag.PropertyType == 0x0000)
                    {
                        // If the value of the Flags field is set to 0x00: The array contains a AddressBookTypedPropertyValue structure, if the type of property is PtyUnspecified.
                        AddressBookTypedPropertyValue typedPropertyValue = new AddressBookTypedPropertyValue();

                        // Parse the AddressBookTypedPropertyValue with the instance of the context which contains the datas and start index.
                        typedPropertyValue.Parse(Context.Instance);
                        valueArray.Add(typedPropertyValue);
                        index = Context.Instance.CurIndex;
                    }
                    else
                    {
                        // If the value of the Flags field is set to 0x00: The array contains a AddressBookPropertyValue structure, if the type of property is specified.
                        Context.Instance.CurProperty.Type = (PropertyType)propertyTag.PropertyType;
                        AddressBookPropertyValue propertyValue = new AddressBookPropertyValue();

                        // Parse the AddressBookTypedPropertyValue with the instance of the context which contains the datas and start index.
                        propertyValue.Parse(Context.Instance);
                        valueArray.Add(propertyValue);
                        index = Context.Instance.CurIndex;
                    }
                }
            }
            else if (addressBookPropertyRow.Flag == 0x01)
            {
                foreach (PropertyTag propertyTag in propTagArray.PropertyTags)
                {
                    if (propertyTag.PropertyType == 0x0000)
                    {
                        // If the value of the Flags field is set to 0x01: The array contains a AddressBookFlaggedPropertyValueWithType structure, if the type of property is PtyUnspecified.
                        AddressBookFlaggedPropertyValueWithType flaggedPropertyValue = new AddressBookFlaggedPropertyValueWithType();

                        // Parse the AddressBookTypedPropertyValue with the instance of the context which contains the datas and start index.
                        flaggedPropertyValue.Parse(Context.Instance);
                        valueArray.Add(flaggedPropertyValue);
                        index = Context.Instance.CurIndex;
                    }
                    else
                    {
                        // If the value of the Flags field is set to 0x01: The array contains a AddressBookFlaggedPropertyValue structure, if the type of property is specified.
                        Context.Instance.CurProperty.Type = (PropertyType)propertyTag.PropertyType;
                        AddressBookFlaggedPropertyValue propertyValue = new AddressBookFlaggedPropertyValue();

                        // Parse the AddressBookTypedPropertyValue with the instance of the context which contains the datas and start index.
                        propertyValue.Parse(Context.Instance);
                        valueArray.Add(propertyValue);
                        index = Context.Instance.CurIndex;
                    }
                }
            }

            addressBookPropertyRow.ValueArray = valueArray.ToArray();

            return(addressBookPropertyRow);
        }
AddressBookPropertyValue