For Rule Action format
Exemple #1
0
        /// <summary>
        /// Generate a sample rule action.
        /// </summary>
        /// <param name="actionType">The action Type</param>
        /// <param name="bytesCount">The length of the bytes count</param>
        /// <param name="actionDataBufferValue">The actionData buffer</param>
        /// <param name="actionFlavor">Action flavor value.</param>
        /// <param name="actionFlag">Action flag value.</param>
        /// <returns>An instance of the RuleAction</returns>
        private static RuleAction GenerateRuleAction(ActionTypes actionType, Count bytesCount, IActionData actionDataBufferValue, uint actionFlavor, uint actionFlag)
        {
            ActionBlock actionBlock = new ActionBlock(bytesCount)
            {
                ActionType      = actionType,
                ActionFlags     = actionFlag,
                ActionDataValue = actionDataBufferValue,
                ActionFlavor    = actionFlavor
            };

            actionBlock.ActionLength = (bytesCount == Count.TwoBytesCount) ? (actionBlock.Size() - 2) : (actionBlock.Size() - 4);
            RuleAction ruleAction = new RuleAction(bytesCount)
            {
                NoOfActions = 0x01,
                Actions     = new ActionBlock[1]
                {
                    actionBlock
                }
            };

            // Only one rule action is generated.
            return(ruleAction);
        }
        /// <summary>
        /// Generate a sample rule action.
        /// </summary>
        /// <param name="actionType">The action Type</param>
        /// <param name="bytesCount">The length of the bytes count</param>
        /// <param name="actionDataBufferValue">The actionData buffer</param>
        /// <param name="actionFlavor">Action flavor value.</param>
        /// <param name="actionFlag">Action flag value.</param>
        /// <returns>An instance of the RuleAction</returns>
        private static RuleAction GenerateRuleAction(ActionTypes actionType, Count bytesCount, IActionData actionDataBufferValue, uint actionFlavor, uint actionFlag)
        {
            ActionBlock actionBlock = new ActionBlock(bytesCount)
            {
                ActionType = actionType,
                ActionFlags = actionFlag,
                ActionDataValue = actionDataBufferValue,
                ActionFlavor = actionFlavor
            };

            actionBlock.ActionLength = (bytesCount == Count.TwoBytesCount) ? (actionBlock.Size() - 2) : (actionBlock.Size() - 4);
            RuleAction ruleAction = new RuleAction(bytesCount)
            {
                NoOfActions = 0x01,
                Actions = new ActionBlock[1]
                {
                    actionBlock
                }
            };

            // Only one rule action is generated.
            return ruleAction;
        }
Exemple #3
0
        /// <summary>
        /// Read a value from buffer for special Type.
        /// </summary>
        /// <param name="type">The Type of value</param>
        /// <param name="buffer">Buffer contains value</param>
        /// <returns>Byte array of the value</returns>
        public static byte[] ReadValueByType(ushort type, byte[] buffer)
        {
            byte[]       value        = null;
            BufferReader bufferReader = new BufferReader(buffer);

            uint length = 0;

            byte[] tmpArray   = null;
            uint   startIndex = 0;
            uint   endIndex   = 0;

            switch (type)
            {
            // 2-byte
            // PtypInteger16
            case 0x0002:
                value = bufferReader.ReadBytes(2);
                break;

            // 4-byte
            // PtypInteger32
            case 0x0003:

            // PtypFloating32
            case 0x0004:

            // PtypErrorCode
            case 0x000A:
                value = bufferReader.ReadBytes(4);
                break;

            // 8-byte
            // PtypFloating64
            case 0x0005:

            // PtypCurrency
            case 0x0006:

            // PtypFloatingTime
            case 0x0007:

            // PtypInteger64
            case 0x0014:

            // PtypTime
            case 0x0040:
                value = bufferReader.ReadBytes(8);
                break;

            // 1 byte
            // PtypBoolean
            case 0x000B:
                value = new byte[1] {
                    bufferReader.ReadByte()
                };
                break;

            // PtypString PT_UNICODE
            case 0x001F:
                value = Encoding.Unicode.GetBytes(bufferReader.ReadUnicodeString());
                break;

            // PtypString8
            case 0x001E:
                value = Encoding.ASCII.GetBytes(bufferReader.ReadASCIIString());
                break;

            // 16-byte
            // PtypGuid  16bytes
            case 0x0048:

            // PtypServerId
            case 0x00FB:
                value = bufferReader.ReadBytes(16);
                break;

            // PtypRestriction
            case 0x00FD:
                tmpArray = bufferReader.ReadToEnd();
                IRestrictions restriction = null;
                restriction = new ContentRestrictions();

                length += restriction.Deserialize(tmpArray);
                value   = bufferReader.ReadBytes(0, length);
                break;

            // PtypRuleAction
            case 0x00FE:
                tmpArray = bufferReader.ReadToEnd();
                RuleAction ruleAction = new RuleAction();
                length       = ruleAction.Deserialize(tmpArray);
                bufferReader = new BufferReader(tmpArray);
                value        = bufferReader.ReadBytes(length);
                break;

            // PtypBinary
            case 0x0102:
                length = (uint)(buffer[bufferReader.Position] + (buffer[bufferReader.Position + 1] << 8) + 2);
                value  = bufferReader.ReadBytes(length);
                break;

            // PtypMultipleInteger16
            case 0x1002:
                length = (uint)(buffer[bufferReader.Position] + (buffer[bufferReader.Position + 1] << 8));
                length = (length * 2) + 2;
                value  = bufferReader.ReadBytes(length);
                break;

            // PtypMultipleInteger32
            case 0x1003:

            // PtypMultipleFloating32
            case 0x1004:
                length = (uint)(buffer[bufferReader.Position] + (buffer[bufferReader.Position + 1] << 8));
                length = (length * 4) + 2;
                value  = bufferReader.ReadBytes(length);
                break;

            // PtypMultipleFloating64
            case 0x1005:

            // PtypMultipleCurrency
            case 0x1006:

            // PtypMultipleFloatingTime
            case 0x1007:

            // PtypMultipleInteger64
            case 0x1014:

            // PtypMultipleTime
            case 0x1040:
                length = (uint)(buffer[bufferReader.Position] + (buffer[bufferReader.Position + 1] << 8));
                length = (length * 8) + 2;
                value  = bufferReader.ReadBytes(length);
                break;

            // PtypMultipleString
            case 0x101F:
                startIndex = bufferReader.Position;
                ushort strCount = bufferReader.ReadUInt16();
                for (int istr = 0; istr < strCount; istr++)
                {
                    bufferReader.ReadUnicodeString();
                }

                endIndex = bufferReader.Position;
                length   = endIndex - startIndex;
                value    = bufferReader.ReadBytes(startIndex, length);
                break;

            // PtypMultipleString8
            case 0x101E:
                startIndex = bufferReader.Position;
                ushort str8Count = bufferReader.ReadUInt16();
                for (int istr8 = 0; istr8 < str8Count; istr8++)
                {
                    bufferReader.ReadASCIIString();
                }

                endIndex = bufferReader.Position;
                length   = endIndex - startIndex;
                value    = bufferReader.ReadBytes(startIndex, length);
                break;

            // PtypMultipleGuid
            case 0x1048:
                length = (uint)(buffer[bufferReader.Position] + (buffer[bufferReader.Position + 1] << 8));
                length = (length * 16) + 2;
                value  = bufferReader.ReadBytes(length);
                break;

            // PtypMultipleBinary
            case 0x1102:
                startIndex = bufferReader.Position;
                ushort binCount = bufferReader.ReadUInt16();
                for (int ibin = 0; ibin < binCount; ibin++)
                {
                    uint binLength = bufferReader.ReadUInt16();
                    bufferReader.ReadBytes(binLength);
                }

                endIndex = bufferReader.Position;
                length   = endIndex - startIndex;
                value    = bufferReader.ReadBytes(startIndex, length);
                break;

            // PtypUnspecified
            case 0x0000:
                throw new NotImplementedException();

            // PtypNull
            case 0x0001:
                value = null;
                break;

            // PtypObject or PtypEmbeddedTable
            case 0x000D:
                throw new NotImplementedException();

            default:
                throw new NotImplementedException();
            }

            return(value);
        }
        /// <summary>
        /// Read a value from buffer for special Type.
        /// </summary>
        /// <param name="type">The Type of value</param>
        /// <param name="buffer">Buffer contains value</param>
        /// <returns>Byte array of the value</returns>
        public static byte[] ReadValueByType(ushort type, byte[] buffer)
        {
            byte[] value = null;
            BufferReader bufferReader = new BufferReader(buffer);

            uint length = 0;
            byte[] tmpArray = null;
            uint startIndex = 0;
            uint endIndex = 0;
            switch (type)
            {
                // 2-byte
                // PtypInteger16
                case 0x0002:
                    value = bufferReader.ReadBytes(2);
                    break;

                // 4-byte
                // PtypInteger32
                case 0x0003:

                // PtypFloating32
                case 0x0004:

                // PtypErrorCode 
                case 0x000A:
                    value = bufferReader.ReadBytes(4);
                    break;

                // 8-byte
                // PtypFloating64
                case 0x0005:

                // PtypCurrency 
                case 0x0006:

                // PtypFloatingTime
                case 0x0007:

                // PtypInteger64
                case 0x0014:

                // PtypTime 
                case 0x0040:
                    value = bufferReader.ReadBytes(8);
                    break;

                // 1 byte
                // PtypBoolean 
                case 0x000B:
                    value = new byte[1] { bufferReader.ReadByte() };
                    break;

                // PtypString PT_UNICODE
                case 0x001F:
                    value = Encoding.Unicode.GetBytes(bufferReader.ReadUnicodeString());
                    break;

                // PtypString8
                case 0x001E:
                    value = Encoding.ASCII.GetBytes(bufferReader.ReadASCIIString());
                    break;

                // 16-byte
                // PtypGuid  16bytes
                case 0x0048:

                // PtypServerId 
                case 0x00FB:
                    value = bufferReader.ReadBytes(16);
                    break;

                // PtypRestriction  
                case 0x00FD:
                    tmpArray = bufferReader.ReadToEnd();
                    IRestrictions restriction = null;
                    restriction = new ContentRestrictions();

                    length += restriction.Deserialize(tmpArray);
                    value = bufferReader.ReadBytes(0, length);
                    break;

                // PtypRuleAction  
                case 0x00FE:
                    tmpArray = bufferReader.ReadToEnd();
                    RuleAction ruleAction = new RuleAction();
                    length = ruleAction.Deserialize(tmpArray);
                    bufferReader = new BufferReader(tmpArray);
                    value = bufferReader.ReadBytes(length);
                    break;

                // PtypBinary  
                case 0x0102:
                    length = (uint)(buffer[bufferReader.Position] + (buffer[bufferReader.Position + 1] << 8) + 2);
                    value = bufferReader.ReadBytes(length);
                    break;

                // PtypMultipleInteger16 
                case 0x1002:
                    length = (uint)(buffer[bufferReader.Position] + (buffer[bufferReader.Position + 1] << 8));
                    length = (length * 2) + 2;
                    value = bufferReader.ReadBytes(length);
                    break;

                // PtypMultipleInteger32  
                case 0x1003:

                // PtypMultipleFloating32  
                case 0x1004:
                    length = (uint)(buffer[bufferReader.Position] + (buffer[bufferReader.Position + 1] << 8));
                    length = (length * 4) + 2;
                    value = bufferReader.ReadBytes(length);
                    break;

                // PtypMultipleFloating64 
                case 0x1005:

                // PtypMultipleCurrency
                case 0x1006:

                // PtypMultipleFloatingTime
                case 0x1007:

                // PtypMultipleInteger64
                case 0x1014:

                // PtypMultipleTime 
                case 0x1040:
                    length = (uint)(buffer[bufferReader.Position] + (buffer[bufferReader.Position + 1] << 8));
                    length = (length * 8) + 2;
                    value = bufferReader.ReadBytes(length);
                    break;

                // PtypMultipleString
                case 0x101F:
                    startIndex = bufferReader.Position;
                    ushort strCount = bufferReader.ReadUInt16();
                    for (int istr = 0; istr < strCount; istr++)
                    {
                        bufferReader.ReadUnicodeString();
                    }

                    endIndex = bufferReader.Position;
                    length = endIndex - startIndex;
                    value = bufferReader.ReadBytes(startIndex, length);
                    break;

                // PtypMultipleString8
                case 0x101E:
                    startIndex = bufferReader.Position;
                    ushort str8Count = bufferReader.ReadUInt16();
                    for (int istr8 = 0; istr8 < str8Count; istr8++)
                    {
                        bufferReader.ReadASCIIString();
                    }

                    endIndex = bufferReader.Position;
                    length = endIndex - startIndex;
                    value = bufferReader.ReadBytes(startIndex, length);
                    break;

                // PtypMultipleGuid
                case 0x1048:
                    length = (uint)(buffer[bufferReader.Position] + (buffer[bufferReader.Position + 1] << 8));
                    length = (length * 16) + 2;
                    value = bufferReader.ReadBytes(length);
                    break;

                // PtypMultipleBinary 
                case 0x1102:
                    startIndex = bufferReader.Position;
                    ushort binCount = bufferReader.ReadUInt16();
                    for (int ibin = 0; ibin < binCount; ibin++)
                    {
                        uint binLength = bufferReader.ReadUInt16();
                        bufferReader.ReadBytes(binLength);
                    }

                    endIndex = bufferReader.Position;
                    length = endIndex - startIndex;
                    value = bufferReader.ReadBytes(startIndex, length);
                    break;

                // PtypUnspecified 
                case 0x0000:
                    throw new NotImplementedException();

                // PtypNull
                case 0x0001:
                    value = null;
                    break;

                // PtypObject or PtypEmbeddedTable 
                case 0x000D:
                    throw new NotImplementedException();

                default:
                    throw new NotImplementedException();
            }

            return value;
        }