Example #1
0
        public override void DecompileMethod(Mono.Cecil.MethodDefinition method, ICSharpCode.Decompiler.ITextOutput output, DecompilationOptions options)
        {
            var declaringType = method.DeclaringType;
            var assembly      = declaringType.Module.Assembly;

            if ((compiler == null) || (compiler.Assembly != assembly))
            {
                compiler = null;
                var c = new AssemblyCompiler(assembly, new NamespaceConverter("pkg.name", ""));
                c.Compile();
                compiler = c;
            }

            var cmethod = compiler.GetMethod(method);

            if ((cmethod != null) && (cmethod.RLBody != null))
            {
                var body        = cmethod.RLBody;
                var basicBlocks = BasicBlock.Find(body);

                foreach (var block in basicBlocks)
                {
                    output.Write(string.Format("D_{0:X4}:", block.Entry.Index));
                    output.WriteLine();
                    output.Indent();
                    foreach (var ins in block.Instructions)
                    {
                        output.Write(ins.ToString());
                        output.WriteLine();
                    }
                    output.Unindent();
                }

                if (body.Exceptions.Any())
                {
                    output.WriteLine();
                    output.Write("Exception handlers:");
                    output.WriteLine();
                    output.Indent();
                    foreach (var handler in body.Exceptions)
                    {
                        output.Write(string.Format("{0:x4}-{1:x4}", handler.TryStart.Index, handler.TryEnd.Index));
                        output.WriteLine();
                        output.Indent();
                        foreach (var c in handler.Catches)
                        {
                            output.Write(string.Format("{0} => {1:x4}", c.Type, c.Instruction.Index));
                            output.WriteLine();
                        }
                        if (handler.CatchAll != null)
                        {
                            output.Write(string.Format("{0} => {1:x4}", "<any>", handler.CatchAll.Index));
                            output.WriteLine();
                        }
                        output.Unindent();
                    }
                    output.Unindent();
                }
            }
            else
            {
                output.Write("Method not found in dex");
                output.WriteLine();
            }
        }
Example #2
0
        public override void DecompileMethod(Mono.Cecil.MethodDefinition method, ICSharpCode.Decompiler.ITextOutput output, DecompilationOptions options)
        {
            var declaringType = method.DeclaringType;
            var assembly      = declaringType.Module.Assembly;

            if ((compiler == null) || (compiler.Assembly != assembly))
            {
                compiler = null;
                var c = new AssemblyCompiler(assembly, new NamespaceConverter("pkg.name", ""));
                c.Compile();
                compiler = c;
            }

            var cmethod = compiler.GetMethod(method);

            if ((cmethod != null) && (cmethod.DexMethod != null))
            {
                var body = cmethod.DexMethod.Body;
                body.UpdateInstructionOffsets();
                var targetInstructions = body.Instructions.Select(x => x.Operand).OfType <Instruction>().ToList();
                targetInstructions.AddRange(body.Exceptions.Select(x => x.TryStart));
                targetInstructions.AddRange(body.Exceptions.Select(x => x.TryEnd));
                targetInstructions.AddRange(body.Exceptions.SelectMany(x => x.Catches, (h, y) => y.Instruction));
                targetInstructions.AddRange(body.Exceptions.Select(x => x.CatchAll));

                foreach (var ins in body.Instructions)
                {
                    if (targetInstructions.Contains(ins) || (ins.Offset == 0))
                    {
                        output.Write(string.Format("D_{0:X4}:", ins.Offset));
                        output.WriteLine();
                    }
                    output.Indent();
                    output.Write(ins.ToString());
                    output.WriteLine();
                    output.Unindent();
                }

                if (body.Exceptions.Any())
                {
                    output.WriteLine();
                    output.Write("Exception handlers:");
                    output.WriteLine();
                    output.Indent();
                    foreach (var handler in body.Exceptions)
                    {
                        output.Write(string.Format("{0:x4}-{1:x4}", handler.TryStart.Offset, handler.TryEnd.Offset));
                        output.WriteLine();
                        output.Indent();
                        foreach (var c in handler.Catches)
                        {
                            output.Write(string.Format("{0} => {1:x4}", c.Type, c.Instruction.Offset));
                            output.WriteLine();
                        }
                        if (handler.CatchAll != null)
                        {
                            output.Write(string.Format("{0} => {1:x4}", "<any>", handler.CatchAll.Offset));
                            output.WriteLine();
                        }
                        output.Unindent();
                    }
                    output.Unindent();
                }
            }
            else
            {
                output.Write("Method not found in dex");
                output.WriteLine();
            }
        }
Example #3
0
 public void Write(char ch)
 {
     output.Write(ch);
 }