public void TestPositionAfterMultiParsing()
        {
            // Arrange

            var stream  = new MemoryStream().FromString("\'t\'\'t\'\'t\'\'t\'\'t\'");
            var context = new DefaultParserContext(stream: stream);
            var parser  = new SymbolParser();

            // Act

            var startStreamPos = stream.Position;

            var result = new List <IRule> {
                parser.Parse(context),
                parser.Parse(context),
                parser.Parse(context),
                parser.Parse(context),
                parser.Parse(context)
            };

            var endStreamPosition = stream.Position;


            // Assert

            Assert.AreEqual(startStreamPos + (result.Count * 3), endStreamPosition);
        }
Exemple #2
0
        /// <summary>
        /// Provides the functionality to map a DLL from memory into a process
        /// </summary>
        public LibraryMapper(Process process, ReadOnlyMemory <byte> dllBytes)
        {
            // Validate the arguments

            if (process is null || process.HasExited)
            {
                throw new ArgumentException("The process provided was invalid");
            }

            if (dllBytes.IsEmpty)
            {
                throw new ArgumentException("The DLL bytes provided were invalid");
            }

            EnterDebugMode();

            _dllBytes = new Memory <byte>(new byte[dllBytes.Length]);

            dllBytes.CopyTo(_dllBytes);

            _peImage = new PeImage(dllBytes);

            _processManager = new ProcessManager(process);

            _symbolParser = new SymbolParser(RetrieveNtdllFilePath(process), "RtlInsertInvertedFunctionTable", "RtlRemoveInvertedFunctionTable");
        }
Exemple #3
0
 public void Insert(int index, T instance)
 {
     if (index < 0)
     {
         throw new ArgumentOutOfRangeException("index");
     }
     if (instance == null)
     {
         throw new ArgumentNullException();
     }
     if (index > this.Count)
     {
         throw new ArgumentOutOfRangeException("index");
     }
     if (this.mode == InstanceCollectionMode.PathHierarchy)
     {
         string        str;
         string        str2;
         IList <int[]> list;
         SymbolParser.ArrayIndexType type;
         if (SymbolParser.TryParseParentPath(instance, out str, out str2))
         {
             throw new ArgumentOutOfRangeException("Instance '{0}'is not a root object. Cannot Insert!", instance.InstancePath);
         }
         string indicesStr = null;
         if (SymbolParser.TryParseArrayElement(instance.InstancePath, out str2, out indicesStr, out list, out type))
         {
             throw new ArgumentOutOfRangeException("Instance '{0}'is not a root object. Cannot Insert!", instance.InstancePath);
         }
     }
     this._pathDict.Add(instance.InstancePath, instance);
     this._list.Insert(index, instance);
 }
        public static Expression <T> Parse <T>(string source)
        {
            var parseResult = SymbolParser.Build(source);
            var parser      = new ExpressionParserCore(parseResult, typeof(T), null, null);

            return(parser.ToLambdaExpression <T>());
        }
Exemple #5
0
        /// <summary>
        /// Provides the functionality to map a DLL from disk into a process
        /// </summary>
        public LibraryMapper(Process process, string dllFilePath)
        {
            // Validate the arguments

            if (process is null || process.HasExited)
            {
                throw new ArgumentException("The process provided was invalid");
            }

            if (string.IsNullOrWhiteSpace(dllFilePath))
            {
                throw new ArgumentException("The DLL file path provided was invalid");
            }

            EnterDebugMode();

            var dllBytes = File.ReadAllBytes(dllFilePath);

            _dllBytes = new Memory <byte>(new byte[dllBytes.Length]);

            dllBytes.CopyTo(_dllBytes);

            _peImage = new PeImage(dllBytes);

            _processManager = new ProcessManager(process);

            _symbolParser = new SymbolParser(RetrieveNtdllFilePath(process), "RtlInsertInvertedFunctionTable", "RtlRemoveInvertedFunctionTable");
        }
Exemple #6
0
        public bool TryGetInstanceByName(string instanceNameWithIndices, out IList <ISymbol> symbols)
        {
            ISymbol item = null;
            string  str;

            SymbolParser.ArrayIndexType type;
            IList <int[]> list;

            symbols = new List <ISymbol>();
            string indicesStr = null;

            if (SymbolParser.TryParseArrayElement(instanceNameWithIndices, out str, out indicesStr, out list, out type) && (StringComparer.OrdinalIgnoreCase.Compare(str, this._arrayInstance.InstanceName) == 0))
            {
                ISymbol    parent    = this._arrayInstance;
                IArrayType arrayType = this._arrayType;
                for (int i = 0; i < list.Count; i++)
                {
                    item      = this._symbolFactory.CreateArrayElement(list[i], parent, arrayType);
                    parent    = item;
                    arrayType = parent.DataType as IArrayType;
                }
            }
            if (item != null)
            {
                symbols.Add(item);
            }
            return(symbols.Count > 0);
        }
        /// <summary>
        /// 将源字符串分析为委托.
        /// </summary>
        /// <typeparam name="T">委托类型</typeparam>
        /// <param name="source">用于分析的源字符串.</param>
        /// <param name="assemblies">可能用到的程序集列表.</param>
        /// <param name="ns">分析过程中可能用到的命名空间列表.</param>
        /// <returns>
        /// 分析委托.
        /// </returns>
        public static T Compile <T>(string source, Assembly[] assemblies, params string[] ns)
        {
            var parseResult = SymbolParser.Build(source);
            var parser      = new ExpressionParserCore(parseResult, typeof(T), ns, assemblies);

            return(parser.ToLambdaExpression <T>().Compile());
        }
Exemple #8
0
 public bool TryGetInstance(string instancePath, out ISymbol symbol)
 {
     symbol = null;
     if (instancePath.StartsWith(this._arrayInstance.InstancePath, StringComparison.OrdinalIgnoreCase))
     {
         SymbolParser.ArrayIndexType type;
         IList <int[]> jaggedIndices = null;
         if (SymbolParser.TryParseIndices(instancePath.Substring(this._arrayInstance.InstancePath.Length), out jaggedIndices, out type))
         {
             if (this.IsOversampled && ArrayIndexConverter.IsOversamplingIndex(jaggedIndices[0], this._arrayType))
             {
                 symbol = ((ISymbolFactoryOversampled)this._symbolFactory).CreateOversamplingElement(this._arrayInstance);
             }
             else
             {
                 ISymbol    parent    = this._arrayInstance;
                 IArrayType arrayType = this._arrayType;
                 for (int i = 0; i < jaggedIndices.Count; i++)
                 {
                     symbol    = this._symbolFactory.CreateArrayElement(jaggedIndices[i], parent, arrayType);
                     parent    = symbol;
                     arrayType = symbol.DataType as IArrayType;
                 }
             }
             return(true);
         }
     }
     return(false);
 }
Exemple #9
0
        public TcAdsSymbolInfo GetSymbol(string name)
        {
            TcAdsSymbolInfo info = null;
            string          str;
            string          str2;
            IList <int[]>   list;
            StringComparer  ordinalIgnoreCase = StringComparer.OrdinalIgnoreCase;

            SymbolParser.ArrayIndexType standard = SymbolParser.ArrayIndexType.Standard;
            bool flag = SymbolParser.TryParseArrayElement(name, out str, out str2, out list, out standard);

            if (!flag)
            {
                str = name;
            }
            foreach (TcAdsSymbolInfo info2 in this)
            {
                if (ordinalIgnoreCase.Compare(info2.ShortName, str) == 0)
                {
                    info = !flag ? info2 : info2.symbolParser.GetSymbol(name);
                    break;
                }
            }
            return(info);
        }
Exemple #10
0
        /// <summary>
        /// Provides the functionality to map a DLL from memory into a remote process
        /// </summary>
        public LibraryMapper(Process process, Memory <byte> dllBytes)
        {
            // Validate the arguments passed into the constructor

            if (process == default || process.HasExited)
            {
                throw new ArgumentException("The process provided was invalid");
            }

            if (dllBytes.IsEmpty)
            {
                throw new ArgumentException("The DLL bytes provided were invalid");
            }

            EnterDebugMode();

            _dllBytes = dllBytes;

            _peImage = new PeImage(dllBytes);

            _processManager = new ProcessManager(process);

            _symbolParser = new SymbolParser(_processManager.Modules.First(module => module.Name.Equals("ntdll.dll")));

            ValidateArchitecture();
        }
Exemple #11
0
        /// <summary>
        /// 将源字符串分析为委托.
        /// </summary>
        /// <typeparam name="T">委托类型</typeparam>
        /// <param name="source">用于分析的源字符串.</param>
        /// <param name="ns">分析过程中可能用到的命名空间列表.</param>
        /// <returns>分析委托.</returns>
        public static Func <T, bool> Compile <T>(string source, params string[] ns)
        {
            var parseResult = SymbolParser.Build(source);
            var parser      = new ExpressionParserCore <T>(parseResult, typeof(T), ns, null);

            return(parser.ToLambdaExpression <T>().Compile());
        }
Exemple #12
0
        /// <summary>
        /// Provides the functionality to map a DLL from disk into a remote process
        /// </summary>
        public LibraryMapper(Process process, string dllPath)
        {
            // Validate the arguments passed into the constructor

            if (process == default || process.HasExited)
            {
                throw new ArgumentException("The process provided was invalid");
            }

            if (string.IsNullOrWhiteSpace(dllPath))
            {
                throw new ArgumentException("The DLL path provided was invalid");
            }

            EnterDebugMode();

            _dllBytes = File.ReadAllBytes(dllPath);

            _peImage = new PeImage(_dllBytes);

            _processManager = new ProcessManager(process);

            _symbolParser = new SymbolParser(_processManager.Modules.First(module => module.Name.Equals("ntdll.dll")));

            ValidateArchitecture();
        }
        public static IList <int[]> StringToIndices(string indices)
        {
            IList <int[]> jaggedIndices = null;

            SymbolParser.ArrayIndexType standard = SymbolParser.ArrayIndexType.Standard;
            SymbolParser.TryParseIndices(indices, out jaggedIndices, out standard);
            return(jaggedIndices);
        }
        public string diffOutput(string startText, string endText)
        {
            var diffs = dmp.diff_main(SymbolParser.SymbolsFromText(startText), SymbolParser.SymbolsFromText(endText));

            dmp.diff_cleanupSemantic(diffs);

            return(diffOutput(diffs));
        }
Exemple #15
0
        public void Symbols_should_not_have_arguments()
        {
            string       lineSrc = "DEX";
            SymbolParser parser  = new SymbolParser();
            Line         line    = parser.ParseAssemblyLine(lineSrc, 1);

            Assert.AreEqual(1, line.Instruction.LineNumber);
            Assert.AreEqual("DEX", line.Instruction.Source);
            Assert.AreEqual(0, line.Arguments.Length);
        }
Exemple #16
0
        public bool ContainsName(string instanceNameWithIndices)
        {
            string str;

            SymbolParser.ArrayIndexType type;
            IList <int[]> list;
            string        indicesStr = null;

            return(SymbolParser.TryParseArrayElement(instanceNameWithIndices, out str, out indicesStr, out list, out type) && ((StringComparer.OrdinalIgnoreCase.Compare(str, this._arrayInstance.InstanceName) == 0) && ArrayIndexConverter.TryCheckIndices(list, this._arrayType)));
        }
        public void ParseInstruction_Returns_LabelInstruction_When_Label_Is_Valid(string input, string expected)
        {
            // arrange
            var parser = new SymbolParser();

            // act
            string result = parser.ParseLabel(input);

            // assert
            Assert.AreEqual(expected, result);
        }
        public void ParseInstruction_Returns_LabelInstruction_When_Label_Is_Valid(string input, string expected)
        {
            // arrange
            var parser = new SymbolParser();

            // act
            string result = parser.ParseLabel(input);

            // assert
            Assert.AreEqual(expected, result);
        }
Exemple #19
0
        public bool Contains(string instancePath)
        {
            SymbolParser.ArrayIndexType type;
            if (!instancePath.StartsWith(this._arrayInstance.InstancePath, StringComparison.OrdinalIgnoreCase))
            {
                return(false);
            }
            IList <int[]> jaggedIndices = null;

            return(SymbolParser.TryParseIndices(instancePath.Substring(this._arrayInstance.InstancePath.Length), out jaggedIndices, out type) && ArrayIndexConverter.TryCheckIndices(jaggedIndices, this._arrayType));
        }
Exemple #20
0
 public bool TryResolveType(string name, out IDataType type)
 {
     if (name == null)
     {
         throw new ArgumentNullException("name");
     }
     if (name == string.Empty)
     {
         throw new ArgumentException();
     }
     return(this._provider.NamespacesInternal.TryGetType(name, out type) || SymbolParser.TryParseType(name, this, out type));
 }
Exemple #21
0
        public void Absolute_should_have_single_argument()
        {
            string       lineSrc = "LDA ($9FA3,X)";
            SymbolParser parser  = new SymbolParser();
            Line         line    = parser.ParseAssemblyLine(lineSrc, 1);

            Assert.AreEqual(1, line.Instruction.LineNumber);
            Assert.AreEqual("LDA", line.Instruction.Source);
            Assert.AreEqual(1, line.Arguments.Length);
            Assert.AreEqual(1, line.Arguments[0].LineNumber);
            Assert.AreEqual("($9FA3,X)", line.Arguments[0].Source);
        }
Exemple #22
0
        public void Should_parse_text_from_the_string()
        {
            var input = new StringText("Hello, World.");

            var parser = new SymbolParser(char.IsLetter, char.IsLetterOrDigit);

            var result = parser.Parse(input, new TextSpan(0, input.Length));

            Assert.IsTrue(result.HasValue);

            var text = input.GetSubText(result.Value);

            Assert.AreEqual("Hello", text.ToString());
        }
Exemple #23
0
        /// <summary>
        /// Provides the functionality to map a DLL from disk into a remote process
        /// </summary>
        public LibraryMapper(Process process, string dllPath)
        {
            EnterDebugMode();

            _dllBytes = File.ReadAllBytes(dllPath);

            _peImage = new PeImage(_dllBytes);

            _processManager = new ProcessManager(process);

            _symbolParser = new SymbolParser(_processManager.Modules.First(module => module.Name.Equals("ntdll.dll")));

            ValidateArchitecture();
        }
Exemple #24
0
        /// <summary>
        /// Provides the functionality to map a DLL from memory into a remote process
        /// </summary>
        public LibraryMapper(Process process, Memory <byte> dllBytes)
        {
            EnterDebugMode();

            _dllBytes = dllBytes;

            _peImage = new PeImage(dllBytes);

            _processManager = new ProcessManager(process);

            _symbolParser = new SymbolParser(_processManager.Modules.First(module => module.Name.Equals("ntdll.dll")));

            ValidateArchitecture();
        }
Exemple #25
0
        public int IndexOf(ISymbol item)
        {
            string instancePath = item.InstancePath;

            if (instancePath.StartsWith(this._arrayInstance.InstancePath, StringComparison.OrdinalIgnoreCase))
            {
                SymbolParser.ArrayIndexType type;
                IList <int[]> jaggedIndices = null;
                if (SymbolParser.TryParseIndices(instancePath.Substring(this._arrayInstance.InstancePath.Length), out jaggedIndices, out type))
                {
                    return(ArrayIndexConverter.IndicesToSubIndex(jaggedIndices[0], this._arrayType));
                }
            }
            throw new ArgumentOutOfRangeException("item");
        }
        public void TestParseRuleFailWithOneSymbol()
        {
            // Arrange

            var stream  = new MemoryStream().FromString("a");
            var context = new DefaultParserContext(stream: stream);
            var parser  = new SymbolParser();

            // Act

            var result = parser.Parse(context);

            // Assert

            Assert.IsNull(result);
        }
        public void TestParseRuleFailWithSymbolParserTooMuchSymbolsException()
        {
            // Arrange

            var stream  = new MemoryStream().FromString("\'asfasdasdasd\'werwer");
            var context = new DefaultParserContext(stream: stream);
            var parser  = new SymbolParser();

            // Act

            var action = new Action(() => { parser.Parse(context); });


            // Assert
            Assert.ThrowsException <SymbolParserTooMuchSymbolsException>(action);
        }
        public void TestParseRuleSuccessWithManySymbols()
        {
            // Arrange

            var stream  = new MemoryStream().FromString("\'t\'sdjhfgjhsdgf");
            var context = new DefaultParserContext(stream: stream);
            var parser  = new SymbolParser();

            // Act

            var result = parser.Parse(context);

            // Assert

            Assert.IsNotNull(result);
        }
Exemple #29
0
        public void MultiArgCommas_should_have_many_arguments()
        {
            string       lineSrc = "DCB $9F,$34,$FA";
            SymbolParser parser  = new SymbolParser();
            Line         line    = parser.ParseAssemblyLine(lineSrc, 1);

            Assert.AreEqual(1, line.Instruction.LineNumber);
            Assert.AreEqual("DCB", line.Instruction.Source);
            Assert.AreEqual(3, line.Arguments.Length);
            Assert.AreEqual(1, line.Arguments[0].LineNumber);
            Assert.AreEqual(1, line.Arguments[1].LineNumber);
            Assert.AreEqual(1, line.Arguments[2].LineNumber);
            Assert.AreEqual("$9F", line.Arguments[0].Source);
            Assert.AreEqual("$34", line.Arguments[1].Source);
            Assert.AreEqual("$FA", line.Arguments[2].Source);
        }
        private bool TryGetSubSymbol(T root, string[] relativePath, int index, out T found)
        {
            string        instanceName = null;
            IList <int[]> list;

            SymbolParser.ArrayIndexType type;
            string          indicesStr = null;
            IList <ISymbol> list2;

            if (!SymbolParser.TryParseArrayElement(relativePath[index], out instanceName, out indicesStr, out list, out type))
            {
                if (root.Category == DataTypeCategory.Struct)
                {
                    return(this.TryGetSubSymbol((IStructInstance)root, relativePath, index, out found));
                }
                found = default(T);
                return(false);
            }
            ((IStructInstance)root).MemberInstances.TryGetInstanceByName(instanceName, out list2);
            IArrayInstance instance2 = (IArrayInstance)list2[0];
            T    local = default(T);
            bool flag  = true;
            int  num   = 0;

            while (true)
            {
                if (num < list.Count)
                {
                    flag &= this.TryGetSubSymbol(instance2, list[num], out local);
                    if (flag)
                    {
                        instance2 = local as IArrayInstance;
                        num++;
                        continue;
                    }
                }
                if (flag)
                {
                    found = local;
                }
                else
                {
                    found = default(T);
                }
                return(flag);
            }
        }
Exemple #31
0
        public static List <Rule> ParseRules(List <string> rulesStrings, List <Symbol> possibleSymbols)
        {
            var rules = new List <Rule>();

            foreach (var ruleString in rulesStrings)
            {
                var ruleStringSplit = ruleString.Split(new[] { "->" }, StringSplitOptions.RemoveEmptyEntries);

                var leftSideSymbol   = new NonTerminalSymbol(ruleStringSplit[0]);
                var rightSideSymbols = SymbolParser.ParseSymbols(ruleStringSplit[1], possibleSymbols);

                var rule = new Rule(leftSideSymbol, rightSideSymbols);
                rules.Add(rule);
            }

            return(rules);
        }
 public static void Main(string[] args)
 {
     CommandLine.parseArgs(args);
     var parser = new SymbolParser();
 }