Ejemplo n.º 1
0
        static void Main(string [] args)
        {
            //var method = GetProgramMethod ("Triangle1");
            AssemblyDefinition assembly = AssemblyDefinition.ReadAssembly(args[1]);
            TypeDefinition     type     = assembly.MainModule.GetType("TestCase");
            MethodDefinition   method   = GetMethod(type.Methods, "Main");

            var cfg = ControlFlowGraph.Create(method);

            FormatControlFlowGraph(Console.Out, cfg);

            Console.WriteLine("--------------------");

            var store = AnnotationStore.CreateStore(cfg, BlockOptimization.Detailed);

            PrintAnnotations(method, store);

            var language = CSharp.GetLanguage(CSharpVersion.V1);

            //var body = method.Body.Decompile (language);

            var writer = language.GetWriter(new PlainTextFormatter(Console.Out));

            writer.Write(method);

            Console.ReadKey();
        }
        private static bool TryGetLanguageParam(string[] args, out ILanguage result, ref bool isInvalidLanguage)
        {
            string languageAsString;

            if (TryGetParam(args, "/lang", out languageAsString))
            {
                if (languageAsString == CommandLineManager.CSharpLanguage)
                {
                    result = CSharp.GetLanguage(CSharpVersion.V4);
                    return(true);
                }
                else if (languageAsString == CommandLineManager.VisualBasicLanguage)
                {
                    result = VisualBasic.GetLanguage(VisualBasicVersion.V4);
                    return(true);
                }
                else
                {
                    isInvalidLanguage = true;
                }
            }

            result = null;
            return(false);
        }
        public static string Decompile(DomCecilMethod method, bool markup)
        {
            if (method.MethodDefinition.IsPInvokeImpl)
            {
                return(GettextCatalog.GetString("Method is P/Invoke"));
            }
            if (method.MethodDefinition.Body == null)
            {
                IType type = method.DeclaringType;
                return(type == null ||  type.ClassType == ClassType.Interface ? GettextCatalog.GetString("Interface method") : GettextCatalog.GetString("Abstract method"));
            }

            StringBuilder result = new StringBuilder();

            try {
                //ControlFlowGraph controlFlowGraph = ControlFlowGraph.Create (method.MethodDefinition);
                ILanguage lang = CSharp.GetLanguage(CSharpVersion.V3);
                ColoredCSharpFormatter formatter  = new ColoredCSharpFormatter();
                ILanguageWriter        langWriter = lang.GetWriter(formatter);
                langWriter.Write(method.MethodDefinition);
                result.Append(formatter.Text);
            } catch (Exception e) {
                result.Append("Decompilation failed: \n" + e);
            }
            return(result.ToString());
        }
Ejemplo n.º 4
0
        public static string ToCodeString(this Expression expression)
        {
            var writer   = new StringWriter();
            var language = CSharp.GetLanguage(CSharpVersion.None).GetWriter(
                new PlainTextFormatter(writer));

            language.Write(expression);

            return(writer.ToString());
        }
        private GeneratorProjectInfo(string target, string @out, IProjectGenerationError error)
        {
            this.Target = target;
            this.Out    = @out;

            this.Language                  = CSharp.GetLanguage(CSharpVersion.V4);
            this.VisualStudioVersion       = VisualStudioVersion.VS2013;
            this.frameworkVersion          = FrameworkVersion.v4_5_2;
            this.IsDefaultFrameworkVersion = true;

            this.AddDocumentation     = true;
            this.RenameInvalidMembers = true;

            this.Error = error;
        }
 public string getSourceCode(MethodDefinition method)
 {
     try
     {
         ILanguage    language = CSharp.GetLanguage(CSharpVersion.V1);
         StringWriter writer   = new StringWriter();
         language.GetWriter(new PlainTextFormatter(writer)).Write(method);
         MemoryStream stream  = new MemoryStream();
         StreamWriter writer3 = new StreamWriter(stream);
         language.GetWriter(new PlainTextFormatter(writer3)).Write(method);
         stream.Flush();
         return(writer.ToString());
     }
     catch (Exception exception)
     {
         DI.log.error("in getSourceCode: {0}", new object[] { exception.Message });
         return("Error in creating source code from IL: " + exception.Message);
     }
 }
Ejemplo n.º 7
0
        public static SLE.Expression ConvertMethod(SLE.Expression [] parameters, MethodDefinition method)
        {
            var body = method.Body.Decompile(CSharp.GetLanguage(CSharpVersion.V1));

            if (body.Statements.Count != 1)
            {
                throw new ArgumentException();
            }

            var @return = body.Statements [0] as ReturnStatement;

            if (@return == null)
            {
                throw new ArgumentException();
            }

            var converter = new ExpressionConverter(parameters, method);

            converter.Visit(@return.Expression);
            return(converter.Pop());
        }
Ejemplo n.º 8
0
        static void Main(string [] args)
        {
            var method = GetProgramMethod("Main");

            var cfg = ControlFlowGraph.Create(method);

            FormatControlFlowGraph(Console.Out, cfg);

            Console.WriteLine("--------------------");

            var store = AnnotationStore.CreateStore(cfg, BlockOptimization.Detailed);

            PrintAnnotations(method, store);

            var language = CSharp.GetLanguage(CSharpVersion.V1);

            var body = method.Body.Decompile(language);

            var writer = language.GetWriter(new PlainTextFormatter(Console.Out));

            writer.Write(method);
        }
Ejemplo n.º 9
0
        private static BlockStatement DecompileStateMachine(this MethodBody body, MethodSpecificContext enclosingMethodContext,
                                                            IStateMachineRemoverStep removeStateMachineStep,
                                                            Func <DecompilationContext, IStateMachineData> stateMachineDataSelector,
                                                            out DecompilationContext decompilationContext)
        {
            ILanguage language = CSharp.GetLanguage(CSharpVersion.V4);

            removeStateMachineStep.Language = language;

            DecompilationPipeline thePipeline = GetStateMachineRemovalPipeline(removeStateMachineStep, stateMachineDataSelector);

            decompilationContext = thePipeline.Run(body, language);

            enclosingMethodContext.Variables.AddRange(decompilationContext.MethodContext.Variables);
            enclosingMethodContext.VariableDefinitionToNameMap.AddRange(decompilationContext.MethodContext.VariableDefinitionToNameMap);
            enclosingMethodContext.AddInnerMethodParametersToContext(decompilationContext.MethodContext);
            enclosingMethodContext.VariableAssignmentData.AddRange(decompilationContext.MethodContext.VariableAssignmentData);
            enclosingMethodContext.GotoLabels.AddRange(decompilationContext.MethodContext.GotoLabels);
            enclosingMethodContext.GotoStatements.AddRange(decompilationContext.MethodContext.GotoStatements);
            BlockStatement theBlockStatement = thePipeline.Body;

            return(theBlockStatement);
        }