Esempio n. 1
0
        public virtual void AddBufferEntityParser(int gmuId, int appId, IFFParser parser, FFTgtParseBufferHandler action)
        {
            using (ILogMethod method = Log.LogMethod(this.DYN_MODULE_NAME, "AddBufferEntityParser"))
            {
                try
                {
                    FF_FlowInitiation flowDirection = this.FlowInitiation;
                    if (_subParsers == null)
                    {
                        _subParsers = new FFParserDictionary();
                    }
                    if (_parserMappings == null)
                    {
                        _parserMappings = new IntDictionary <int>();
                    }

                    if (parser != null && parser.FlowInitiation != FF_FlowInitiation.Any)
                    {
                        flowDirection = parser.FlowInitiation;
                    }

                    _subParsers.AddParserItem(flowDirection, gmuId, appId, parser, action);
                    if (appId != -1 &&
                        !_parserMappings.ContainsKey(appId))
                    {
                        _parserMappings.Add(appId, gmuId);
                    }
                }
                catch (Exception ex)
                {
                    method.Exception(ex);
                }
            }
        }
Esempio n. 2
0
        public virtual IFFParser GetParserFromAppId(int appId, out int gmuId)
        {
            using (ILogMethod method = Log.LogMethod(this.DYN_MODULE_NAME, "GetParserFromEntityId"))
            {
                gmuId = 0;
                IFFParser result = default(IFFParser);

                try
                {
                    FFParserItem parserItem = null;
                    if (_parserMappings.ContainsKey(appId))
                    {
                        parserItem = _subParsers[_parserMappings[appId]];
                    }
                    else
                    {
                        parserItem = _subParsers[-1];
                    }
                    gmuId  = parserItem.GmuId;
                    result = parserItem[this.FlowInitiation].Parser;
                }
                catch (Exception ex)
                {
                    method.Exception(ex);
                }

                return(result);
            }
        }
Esempio n. 3
0
        private void CheckInternal(string fileName, Action <int> assertAction)
        {
            using (var fs = new FileStream(Path.Combine(TestContext.CurrentContext.TestDirectory, fileName), FileMode.Open)) {
                using (var iffParser = new IFFParser(fs, false)) {
                    var error = 0;
                    while (true)
                    {
                        error = iffParser.Parse(ParseMode.RawStep);
                        if (error == (int)ParserStatus.EndOfContext)
                        {
                            continue;
                        }
                        else if (error != 0)
                        {
                            break;
                        }
                        var top = iffParser.GetCurrentContext();
                        if (top == null)
                        {
                            continue;
                        }
                        for (var parent = iffParser.GetParentContext(top);
                             parent != null;
                             parent = iffParser.GetParentContext(parent))
                        {
                            Console.Write(".");
                        }
                        Console.Write("{0} {1} ", IdUtility.IdToString(top.Id), top.Size);
                        Console.Write("{0}\n", IdUtility.IdToString(top.Type));
                    }

                    assertAction(error);
                }
            }
        }
        internal override IFreeformEntity ParseBufferInternal(ref IFreeformEntity entity, IFreeformEntity rootEntity, int id, byte[] buffer)
        {
            // get and verify the command
            FF_AppId_G2H_Commands command = GetCommand(buffer);

            if (command == FF_AppId_G2H_Commands.None)
            {
                Log.Warning("Invalid message passed (Invalid command)");
                return(null);
            }

            // create the entity and parse
            IFFParser parser = this.GetParserFromAppId((int)command);

            entity = parser.ParseBuffer(rootEntity, buffer);

            // valid message
            return(entity);
        }
Esempio n. 5
0
        public virtual void AddBufferEntityParser(Type gmuIdEnum, Type appIdEnum, IFFParser parser, FFTgtParseBufferHandler action)
        {
            using (ILogMethod method = Log.LogMethod(this.DYN_MODULE_NAME, "AddBufferEntityParser"))
            {
                try
                {
                    //Array gmuIdValues = Enum.GetValues(gmuIdEnum);
                    //Array appIdValues = Enum.GetValues(appIdEnum);

                    //if (gmuIdValues.Length == appIdValues.Length)
                    //{
                    //    for (int i = 0; i < gmuIdValues.Length; i++)
                    //    {
                    //        this.AddBufferEntityParser((int)gmuIdValues.GetValue(i), (int)appIdValues.GetValue(i), parser, action);
                    //    }
                    //}
                    string[] gmuIdNames = Enum.GetNames(gmuIdEnum);
                    foreach (var gmuIdName in gmuIdNames)
                    {
                        try
                        {
                            object gmuIdValue = Enum.Parse(gmuIdEnum, gmuIdName, true);
                            object appIdValue = Enum.Parse(appIdEnum, gmuIdName, true);
                            this.AddBufferEntityParser((int)gmuIdValue, (int)appIdValue, parser, action);
                        }
                        catch (Exception ex)
                        {
                            method.InfoV("::: ENUM ERROR : Unable to convert from ({0}) to ({1})", gmuIdEnum.FullName, appIdEnum.FullName);
                        }
                    }
                }
                catch (Exception ex)
                {
                    method.Exception(ex);
                }
            }
        }
Esempio n. 6
0
        public virtual void AddParserItem(FF_FlowInitiation flowInitiation, int gmuId, int appId, IFFParser parser, FFTgtParseBufferHandler action)
        {
            using (ILogMethod method = Log.LogMethod("FFParserDictionary", "AddTargetParser"))
            {
                try
                {
                    FFParserItem item = null;

                    if (!this.ContainsKey(gmuId))
                    {
                        this.Add(gmuId, (item = new FFParserItem()));
                    }
                    else
                    {
                        item = this[gmuId];
                    }

                    item.GmuId = gmuId;
                    item.AppId = appId;

                    FFParserItemInfo itemInfo = item[flowInitiation];
                    itemInfo.Parser = parser;
                    itemInfo.Action = action;
                }
                catch (Exception ex)
                {
                    method.Exception(ex);
                }
            }
        }
Esempio n. 7
0
        internal override void ParseEntityInternal(IFreeformEntity entity, ref List <byte> buffer)
        {
            IFreeformEntity_MsgTgt tgt  = entity as IFreeformEntity_MsgTgt;
            IFreeformEntity_MsgTgt tgt2 = tgt;

            if (tgt2 != null)
            {
                // has raw data inside the target
                if (tgt2 is IFFTgt_Override)
                {
                    buffer.AddRange(tgt2.ToRawData());
                    return;
                }

                int       gmuParserId   = this.GetTargetId(tgt, out tgt2);
                int       gmuTargetId   = gmuParserId.ExtractCombinedId();
                bool      isSecured     = tgt2.IsSecured;
                bool      hasSubTargets = this.HasSubTargets;
                byte[]    buffer2       = null;
                IFFParser parser        = this;
                IFFParser parserSecured = null;

                FFParserItem parserItem = this.GetParserItem(null, gmuParserId);
                if (parserItem == null)
                {
                    if (tgt2.Parent != null &&
                        tgt2.Parent is IFreeformEntity_Msg)
                    {
                        int id = FreeformHelper.CreateCombinedId(tgt2.Parent.UniqueEntityId.GetGmuIdInt8(), gmuParserId);
                        parserItem = this.GetParserItem(null, id);
                    }
                }

                // found?
                if (parserItem != null)
                {
                    // parser
                    IFFParser parser2 = parserItem.GetParser(this.FlowInitiation);
                    if (parser2 != null)
                    {
                        if (tgt2.Targets != null &&
                            tgt2.Targets.Count > 0)
                        {
                            buffer2 = parser2.ParseTarget(tgt2);
                        }
                        else
                        {
                            parser = parser2;
                        }
                    }

                    // secured parser
                    if (isSecured)
                    {
                        int          securityId  = FreeformHelper.CreateCombinedId(entity.Parent as IFreeformEntity_Msg, SECURITY_ID, false);
                        FFParserItem parserItem2 = this.GetParserItem(null, securityId);
                        if (parserItem2 != null)
                        {
                            parserSecured = parserItem2.GetParser(this.FlowInitiation);
                        }
                    }
                }

                bool skipTargetInfo = parser.SkipTargetInfo;
                if (buffer2 == null)
                {
                    List <byte> data = new List <byte>();
                    parser.GetTargetData(tgt2, ref data);
                    buffer2 = data.ToArray();
                }

                // encrypt the target
                if (isSecured &&
                    parserSecured != null)
                {
                    List <byte> bufferToEncrypt = new List <byte>();
                    this.AddTargetToBuffer(ref bufferToEncrypt, (byte)gmuTargetId, buffer2, true, false);

                    parserSecured.GetTargetData(tgt2, ref bufferToEncrypt);
                    buffer2        = bufferToEncrypt.ToArray();
                    gmuTargetId    = SECURITY_ID;
                    hasSubTargets  = true;
                    skipTargetInfo = false;
                }

                this.AddTargetToBuffer(ref buffer, (byte)gmuTargetId, buffer2, hasSubTargets, skipTargetInfo);
            }
        }
Esempio n. 8
0
        protected override IFreeformEntity ParseBufferInternal(IFreeformEntity parent, IFreeformEntity rootEntity, ref IFreeformEntity entity, byte[] buffer, int offset, int length)
        {
            if (length == 0)
            {
                return(null);
            }
            int sessionId = -1;

            if (buffer != null)
            {
                if (length < 0)
                {
                    length = buffer.Length;
                }
                bool hasSubTargets = this.HasSubTargets;

                while (offset < length)
                {
                    int    id         = buffer[offset];
                    int    combinedId = id;
                    byte   length2    = 0;
                    byte[] buffer2    = null;

                    if (rootEntity != null &&
                        rootEntity is IFreeformEntity_Msg)
                    {
                        combinedId = rootEntity.CreateCombinedId(id, false);
                    }

                    if (HasSubTargets)
                    {
                        length2 = buffer[++offset]; // we may get zero length buffer
                        if (length2 > 0)
                        {
                            buffer2 = new byte[length2];
                            Buffer.BlockCopy(buffer, ++offset, buffer2, 0, buffer2.Length);
                        }
                        else
                        {
                            offset += 1;
                            continue;
                        }
                    }
                    else
                    {
                        buffer2 = new byte[buffer.Length - 1];
                        Buffer.BlockCopy(buffer, ++offset, buffer2, 0, buffer2.Length);
                    }

                    if (buffer2 != null)
                    {
                        IFreeformEntity_MsgTgt target    = null;
                        bool         isSecured           = false;
                        bool         isResponseRequired  = false;
                        bool         isResponseRequired2 = false;
                        FFParserItem parserItem          = this.GetParserItem(rootEntity, id);
                        int          appId = id;

                        // not found, then try it with combined id
                        if (parserItem == null)
                        {
                            parserItem = this.GetParserItem(rootEntity, combinedId);
                        }

                        // found
                        if (parserItem != null)
                        {
                            // check if response required
                            id = id.GetRequestResponseId(out isResponseRequired);

                            // check if the packet is secured
                            appId = parserItem.AppId.GetRequestResponseId(out isResponseRequired2);

                            FFTgtParseBufferHandler action = parserItem[this.FlowInitiation].Action;
                            if (action != null)
                            {
                                target = action(parent as IFreeformEntity_MsgTgt, id, length2, buffer2);
                            }
                            else
                            {
                                IFFParser parser = parserItem.GetParser(this.FlowInitiation);
                                if (parser != null)
                                {
                                    target = parser.ParseBuffer(rootEntity, appId, buffer2) as IFreeformEntity_MsgTgt;
                                }
                            }
                        }

                        // secured target
                        if (target is FFTgt_B2B_Encrypted)
                        {
                            rootEntity.EncryptedTarget = target;
                            isSecured = true;
                        }

                        entity = target;
                        if (target != null)
                        {
                            target.TargetID           = appId;
                            target.TargetLength       = length2;
                            target.EntityData         = buffer2;
                            target.IsSecured          = isSecured;
                            target.IsResponseRequired = isResponseRequired;
                            if (parent != null)
                            {
                                parent.Targets.Add(target);
                            }
                            if (target is IFreeformEntity_MsgTgt_Primary &&
                                rootEntity != null)
                            {
                                rootEntity.EntityPrimaryTarget = target;
                            }
                        }
                    }

                    if (hasSubTargets)
                    {
                        offset += length2;
                    }
                    else
                    {
                        break;
                    }
                }
            }

            return(entity);
        }
Esempio n. 9
0
        //public void AddBufferEntityParser<G, A>(G gmuIdEnum, A appIdEnum, IFFParser parser)
        //{
        //    this.AddBufferEntityParser(gmuIdEnum, appIdEnum, parser, null);
        //}
        //public void AddBufferEntityParser<G, A>(G gmuIdEnum, A appIdEnum, FFTgtParseBufferHandler action)
        //{
        //    this.AddBufferEntityParser(gmuIdEnum, appIdEnum, null, action);
        //}
        //public void AddBufferEntityParser<G, A>(G gmuIdEnum, A appIdEnum, IFFParser parser, FFTgtParseBufferHandler action)
        //{
        //    using (ILogMethod method = Log.LogMethod(this.DYN_MODULE_NAME, "AddBufferEntityParser"))
        //    {
        //        try
        //        {
        //            //Array gmuIdValues = Enum.GetValues(gmuIdEnum);
        //            //Array appIdValues = Enum.GetValues(appIdEnum);

        //            //if (gmuIdValues.Length == appIdValues.Length)
        //            //{
        //            //    for (int i = 0; i < gmuIdValues.Length; i++)
        //            //    {
        //            //        this.AddBufferEntityParser((int)gmuIdValues.GetValue(i), (int)appIdValues.GetValue(i), parser, action);
        //            //    }
        //            //}
        //            Array gmuIdNames = Enum.GetNames(gmuIdEnum);
        //            foreach (var gmuIdName in gmuIdNames.OfType<string>())
        //            {
        //                int appIdValue = 0;
        //                Enum.TryParse<int>(gmuIdName, out appIdValue);
        //            }
        //        }
        //        catch (Exception ex)
        //        {
        //            method.Exception(ex);
        //        }
        //    }
        //}

        public virtual void AddBufferEntityParser(int gmuId, int appId, IFFParser parser)
        {
            this.AddBufferEntityParser(gmuId, appId, parser, null);
        }
Esempio n. 10
0
 public virtual void AddBufferEntityParser(Type gmuIdEnum, Type appIdEnum, IFFParser parser)
 {
     this.AddBufferEntityParser(gmuIdEnum, appIdEnum, parser, null);
 }