Exemple #1
0
        public static void Main(string[] args)
        {
            var services = new ServiceContainer();
            var listener = new CmdLineListener
            {
                Quiet = Console.IsOutputRedirected
            };
            var config = RekoConfigurationService.Load(services);
            var fsSvc  = new FileSystemServiceImpl();
            var dcSvc  = new DecompilerService();

            services.AddService <IDecompilerService>(dcSvc);
            services.AddService <DecompilerEventListener>(listener);
            services.AddService <IConfigurationService>(config);
            services.AddService <ITypeLibraryLoaderService>(new TypeLibraryLoaderServiceImpl(services));
            services.AddService <IFileSystemService>(fsSvc);
            services.AddService <IDecompiledFileService>(new DecompiledFileService(fsSvc, listener));
            services.AddService <IPluginLoaderService>(new PluginLoaderService());
            services.AddService <ITestGenerationService>(new TestGenerationService(services));
            services.AddService <IOutputService>(new CmdOutputService());
            var ldr        = new Loader(services);
            var decompiler = new Decompiler(ldr, services);

            dcSvc.Decompiler = decompiler;
            var driver = new CmdLineDriver(services, ldr, decompiler, listener);

            driver.Execute(args);
        }
Exemple #2
0
        private void DecompileAssembler(string archName, Address loadAddress)
        {
            var sc      = new ServiceContainer();
            var cfg     = RekoConfigurationService.Load(sc);
            var arch    = cfg.GetArchitecture(archName);
            var asm     = arch.CreateAssembler(null);
            var program = asm.AssembleFragment(loadAddress, txtAssembler.Text + Environment.NewLine);
            var loader  = new Loader(sc);
            var decomp  = new Decompiler(loader, sc);
            var proj    = new Project {
                Programs =
                {
                    program
                }
            };

            decomp.Project = proj;
            decomp.ScanPrograms();
            decomp.AnalyzeDataFlow();
            decomp.ReconstructTypes();
            decomp.StructureProgram();
            decomp.WriteDecompilerProducts();

            plcOutput.Text     = host.DisassemblyWriter.ToString();
            plcDecompiled.Text = host.DecompiledCodeWriter.ToString();
        }
Exemple #3
0
        public void Rcfg_LoadArchitectureModel()
        {
            var cfgSvc = new RekoConfigurationService(sc, "reko.config", new RekoConfiguration_v1
            {
                Architectures = new[] {
                    new Architecture_v1
                    {
                        Name   = "fake",
                        Type   = typeof(FakeArchitecture).AssemblyQualifiedName,
                        Models = new[]
                        {
                            new ModelDefinition_v1
                            {
                                Name    = "Fake-2000",
                                Options = new[]
                                {
                                    new ListOption_v1 {
                                        Text = ProcessorOption.WordSize, Value = "32"
                                    }
                                }
                            }
                        }
                    }
                }
            });

            pluginSvc.Setup(p => p.GetType(It.IsAny <string>()))
            .Returns(typeof(FakeArchitecture));

            var arch    = cfgSvc.GetArchitecture("fake", "Fake-2000");
            var options = arch.SaveUserOptions();

            Assert.AreEqual("Fake-2000", options[ProcessorOption.Model]);
            Assert.AreEqual("32", options[ProcessorOption.WordSize]);
        }
Exemple #4
0
        public void Rcfg_LoadArchitecture_UnknownModel()
        {
            var cfgSvc = new RekoConfigurationService(sc, "reko.config", new RekoConfiguration_v1
            {
                Architectures = new[] {
                    new Architecture_v1
                    {
                        Name   = "fake",
                        Type   = typeof(FakeArchitecture).AssemblyQualifiedName,
                        Models = new[]
                        {
                            new ModelDefinition_v1
                            {
                                Name    = "Fake-2000",
                                Options = new[]
                                {
                                    new ListOption_v1 {
                                        Text = ProcessorOption.WordSize, Value = "32"
                                    }
                                }
                            }
                        }
                    }
                }
            });

            pluginSvc.Setup(p => p.GetType(It.IsAny <string>()))
            .Returns(typeof(FakeArchitecture));

            var arch = cfgSvc.GetArchitecture("fake", "Unknown Model");

            Assert.AreEqual("WarningDiagnostic -  - Model 'Unknown Model' is not defined for architecture 'fake'.", listener.LastDiagnostic);
        }
Exemple #5
0
        public void Setup()
        {
            mr            = new MockRepository();
            eventListener = new FakeDecompilerEventListener();
            this.services = mr.Stub <IServiceProvider>();
            var tlSvc     = new TypeLibraryLoaderServiceImpl(services);
            var configSvc = mr.StrictMock <IConfigurationService>();
            var win32env  = new OperatingEnvironmentElement
            {
                TypeLibraries =
                {
                    new TypeLibraryElement {
                        Name = "msvcrt.xml"
                    },
                    new TypeLibraryElement {
                        Name = "windows32.xml"
                    },
                }
            };

            configSvc.Stub(c => c.GetEnvironment("win32")).Return(win32env);
            configSvc.Stub(c => c.GetInstallationRelativePath(null)).IgnoreArguments()
            .Do(new Func <string[], string>(s => RekoConfigurationService.MakeInstallationRelativePath(s)));
            services.Stub(s => s.GetService(typeof(ITypeLibraryLoaderService))).Return(tlSvc);
            services.Stub(s => s.GetService(typeof(IConfigurationService))).Return(configSvc);
            services.Stub(s => s.GetService(typeof(DecompilerEventListener))).Return(eventListener);
            services.Stub(s => s.GetService(typeof(CancellationTokenSource))).Return(null);
            services.Stub(s => s.GetService(typeof(IFileSystemService))).Return(new FileSystemServiceImpl());
            services.Stub(s => s.GetService(typeof(IDiagnosticsService))).Return(new FakeDiagnosticsService());
            services.Replay();
            configSvc.Replay();
            arch  = new X86ArchitectureFlat32();
            win32 = new Reko.Environments.Windows.Win32Platform(services, arch);
        }
Exemple #6
0
        public Sifter(string[] args)
        {
            var sc = new ServiceContainer();

            testGen = new TestGenerationService(sc)
            {
                OutputDirectory = Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location)
            };
            fsSvc       = new FileSystemServiceImpl();
            this.cfgSvc = RekoConfigurationService.Load(sc, "reko/reko.config");
            sc.AddService <ITestGenerationService>(testGen);
            sc.AddService <IFileSystemService>(fsSvc);
            sc.AddService <IConfigurationService>(cfgSvc);
            this.processInstr = new Action <byte[], MachineInstruction?>(ProcessInstruction);
            IProcessorArchitecture?arch;

            (arch, this.instrRenderer) = ProcessArgs(args);
            if (arch is null)
            {
                throw new ApplicationException("Unable to load Reko architecture.");
            }
            this.arch        = arch;
            this.baseAddress = Address.Create(arch.PointerType, 0x00000000);    //$TODO allow customization?
            this.progress    = new Progress();
        }
Exemple #7
0
        public void Setup()
        {
            eventListener = new FakeDecompilerEventListener();
            this.services = new ServiceContainer();
            var tlSvc     = new TypeLibraryLoaderServiceImpl(services);
            var configSvc = new Mock <IConfigurationService>();
            var win32env  = new PlatformDefinition
            {
                TypeLibraries =
                {
                    new TypeLibraryDefinition {
                        Name = "msvcrt.xml"
                    },
                    new TypeLibraryDefinition {
                        Name = "windows32.xml"
                    },
                }
            };

            configSvc.Setup(c => c.GetEnvironment("win32")).Returns(win32env);
            configSvc.Setup(c => c.GetInstallationRelativePath(It.IsAny <string>()))
            .Returns((string[] s) => RekoConfigurationService.MakeInstallationRelativePath(s));
            services.AddService(typeof(ITypeLibraryLoaderService), tlSvc);
            services.AddService(typeof(IConfigurationService), configSvc.Object);
            services.AddService(typeof(DecompilerEventListener), eventListener);
            services.AddService(typeof(CancellationTokenSource), new CancellationTokenSource());
            services.AddService(typeof(IFileSystemService), new FileSystemServiceImpl());
            arch  = new X86ArchitectureFlat32(services, "x86-protected-32", new Dictionary <string, object>());
            win32 = new Win32Platform(services, arch);
        }
Exemple #8
0
        public static void Main(string [] args)
        {
            var services = new ServiceContainer();

            if (args.Length == 0)
            {
                services.AddService(typeof(IServiceFactory), new ServiceFactory(services));
                services.AddService(typeof(IDialogFactory), new WindowsFormsDialogFactory(services));
                services.AddService(typeof(IRegistryService), new WindowsFormsRegistryService());
                services.AddService(typeof(ISettingsService), new WindowsFormsSettingsService(services));
                var interactor = new MainFormInteractor(services);
                interactor.Run();
            }
            else
            {
                var listener = NullDecompilerEventListener.Instance;

                services.AddService(typeof(DecompilerEventListener), listener);
                services.AddService(typeof(IRegistryService), new WindowsFormsRegistryService());
                services.AddService(typeof(IConfigurationService), RekoConfigurationService.Load());
                var ldr = new Loader(services);
                var dec = new DecompilerDriver(ldr, services);
                dec.Decompile(args[0]);
            }
        }
Exemple #9
0
        public async Task Zot()
        {
            var sc    = new ServiceContainer();
            var plSvc = new PluginLoaderService();

            sc.AddService <IPluginLoaderService>(plSvc);
            var fsSvc = new FileSystemServiceImpl();

            sc.AddService <IFileSystemService>(fsSvc);
            var cfgSvc = RekoConfigurationService.Load(sc, @"D:\dev\uxmal\reko\extras\parallel\UnitTests\bin\Debug\net5.0\reko\reko.config");

            sc.AddService <IConfigurationService>(cfgSvc);
            var listener = new NullDecompilerEventListener();

            sc.AddService <DecompilerEventListener>(listener);
            var dechost = new Reko.DecompiledFileService(sc, fsSvc, listener);

            sc.AddService <IDecompiledFileService>(dechost);
            var tlSvc = new TypeLibraryLoaderServiceImpl(sc);

            sc.AddService <ITypeLibraryLoaderService>(tlSvc);
            var loader  = new Reko.Loading.Loader(sc);
            var program = (Program)loader.Load(ImageLocation.FromUri(@"D:\dev\uxmal\reko\users\smx-zoo\RELEASE_MIPS\RELEASE"));
            var project = Project.FromSingleProgram(program);
            var reko    = new Reko.Decompiler(project, sc);

            TryFindSegment(program, ".text", out var seg);
            var scanner = new Scanner(seg.MemoryArea);
            var result  = await scanner.ScanAsync(program.EntryPoints.Values);

            Console.Write(result.B.Count);
        }
Exemple #10
0
 private static void DumpEnvironments(RekoConfigurationService config, TextWriter w, string fmtString)
 {
     foreach (var arch in config.GetEnvironments()
              .OfType <OperatingEnvironmentElement>()
              .OrderBy(a => a.Name))
     {
         w.WriteLine(fmtString, arch.Name, arch.Description);
     }
 }
Exemple #11
0
 private static void DumpRawFiles(RekoConfigurationService config, TextWriter w, string fmtString)
 {
     foreach (var raw in config.GetRawFiles()
              .OfType <RawFileElement>()
              .OrderBy(a => a.Name))
     {
         w.WriteLine(fmtString, raw.Name, raw.Description);
     }
 }
Exemple #12
0
        public Sifter(string[] args)
        {
            this.cfgSvc       = RekoConfigurationService.Load("reko/reko.config");
            this.processInstr = new Action <byte[], MachineInstruction?>(ProcessInstruction);
            (this.arch, this.instrRenderer) = ProcessArgs(args);
            var baseAddress = Address.Ptr32(0x00000000);    //$TODO allow customization?

            this.mem      = new MemoryArea(baseAddress, new byte[100]);
            this.rdr      = arch.CreateImageReader(mem, 0);
            this.dasm     = arch.CreateDisassembler(rdr);
            this.progress = new Progress();
        }
Exemple #13
0
        public static void Main(string[] args)
        {
            var services      = new ServiceContainer();
            var listener      = new CmdLineListener();
            var config        = RekoConfigurationService.Load();
            var diagnosticSvc = new CmdLineDiagnosticsService(Console.Out);

            services.AddService <DecompilerEventListener>(listener);
            services.AddService <IConfigurationService>(config);
            services.AddService <ITypeLibraryLoaderService>(new TypeLibraryLoaderServiceImpl(services));
            services.AddService <IDiagnosticsService>(diagnosticSvc);
            services.AddService <IFileSystemService>(new FileSystemServiceImpl());
            services.AddService <DecompilerHost>(new CmdLineHost());
            var driver = new CmdLineDriver(services, config);

            driver.Execute(args);
        }
Exemple #14
0
        private void DecompileC()
        {
            string tmpName = Guid.NewGuid().ToString();
            string tmpDir  = Server.MapPath("tmp");
            string cFile   = Path.Combine(tmpDir, tmpName + ".c");
            string asmFile = Path.Combine(tmpDir, tmpName + ".asm");

            try
            {
                CopyCSourceToTempFile(txtAssembler.Text, cFile);
                if (CompileCFile(tmpDir, cFile))
                {
                    var sc       = new ServiceContainer();
                    var ldr      = new Loader(sc);
                    var cfg      = RekoConfigurationService.Load(sc);
                    var arch     = cfg.GetArchitecture("x86-protected-32");
                    var env      = cfg.GetEnvironment("win32");
                    var platform = env.Load(sc, arch);
                    var asm      = arch.CreateAssembler(null);
                    var program  = asm.AssembleFragment(Address.Ptr32(0x10000000), txtAssembler.Text + Environment.NewLine);
                    program.Platform = platform;
                    var decomp  = new Decompiler(ldr, sc);
                    var project = new Project
                    {
                        Programs = { program }
                    };
                    decomp.Project = project;
                    decomp.ScanPrograms();
                    decomp.AnalyzeDataFlow();
                    decomp.ReconstructTypes();
                    decomp.StructureProgram();
                    decomp.WriteDecompilerProducts();

                    plcOutput.Text     = host.DisassemblyWriter.ToString();
                    plcDecompiled.Text = host.DecompiledCodeWriter.ToString();
                }
            }
            finally
            {
                if (File.Exists(asmFile))
                {
                    File.Delete(asmFile);
                }
            }
        }
Exemple #15
0
        private static RekoConfigurationService MakeServices()
        {
            var services = new ServiceContainer();
            var cfg      = RekoConfigurationService.Load(services, "reko/reko.config");

            services.AddService <IFileSystemService>(new FileSystemServiceImpl());
            var testSvc = new TestGenerationService(services)
            {
                OutputDirectory = Path.GetDirectoryName(typeof(Program).Assembly.Location !)
            };
            var mutable = new MutableTestGenerationService(testSvc)
            {
                IsMuted = true
            };

            services.AddService <ITestGenerationService>(mutable);
            return(cfg);
        }
        private Reko.Decompiler CreateRekoInstance(CefV8Context context)
        {
            var fsSvc    = new FileSystemServiceImpl();
            var listener = new ListenerService(context, eventListeners);
            var dfSvc    = new DecompiledFileService(fsSvc, listener);

            services.AddService(typeof(IFileSystemService), fsSvc);
            services.AddService(typeof(DecompilerEventListener), listener);
            var configSvc = RekoConfigurationService.Load(services, "reko/reko.config");

            services.AddService(typeof(IConfigurationService), configSvc);
            services.AddService(typeof(IDecompiledFileService), dfSvc);
            services.AddService(typeof(ITypeLibraryLoaderService), new TypeLibraryLoaderServiceImpl(services));
            services.AddService(typeof(IPluginLoaderService), new PluginLoaderService());
            var loader = new Reko.Loading.Loader(services);

            return(new Reko.Decompiler(loader, services));
        }
        public void Rcfg_LoadLoader()
        {
            var cfgSvc = new RekoConfigurationService(sc, "reko.config", new RekoConfiguration_v1
            {
                Loaders = new[]
                {
                    new RekoLoader
                    {
                        MagicNumber = "425325",
                        Offset      = "0x4242",
                        Extension   = ".foo",
                        Argument    = "fnord",
                        Label       = "lay-bul",
                        Type        = "foo.Loader,foo",
                    },
                    new RekoLoader
                    {
                        MagicNumber = "444444",
                        Offset      = "123",
                        Extension   = ".bar",
                        Argument    = "bok",
                        Label       = "bullet",
                        Type        = "bar.Loader,bar",
                    }
                }
            });

            var ldrs = cfgSvc.GetImageLoaders().ToArray();

            Assert.AreEqual("425325", ldrs[0].MagicNumber);
            Assert.AreEqual(0x4242, ldrs[0].Offset);
            Assert.AreEqual(".foo", ldrs[0].Extension);
            Assert.AreEqual("fnord", ldrs[0].Argument);
            Assert.AreEqual("lay-bul", ldrs[0].Label);
            Assert.AreEqual("foo.Loader,foo", ldrs[0].TypeName);

            Assert.AreEqual("444444", ldrs[1].MagicNumber);
            Assert.AreEqual(123, ldrs[1].Offset);
            Assert.AreEqual(".bar", ldrs[1].Extension);
            Assert.AreEqual("bok", ldrs[1].Argument);
            Assert.AreEqual("bullet", ldrs[1].Label);
            Assert.AreEqual("bar.Loader,bar", ldrs[1].TypeName);
        }
Exemple #18
0
        public void Rcfg_LoadOperatingEnvironment()
        {
            var cfgSvc = new RekoConfigurationService(new RekoConfiguration_v1
            {
                Environments = new []
                {
                    new Environment_v1
                    {
                        Name        = "testOS",
                        Description = "Test OS",
                        Heuristics  = new PlatformHeuristics_v1
                        {
                            ProcedurePrologs = new []
                            {
                                new BytePattern_v1
                                {
                                    Bytes = "55 8B EC",
                                    Mask  = "FF FF FF",
                                },
                                new BytePattern_v1
                                {
                                    Bytes = "55 ?? 30"
                                }
                            }
                        }
                    }
                }
            });

            var env = cfgSvc.GetEnvironment("testOS");

            Assert.AreEqual(2, env.Heuristics.ProcedurePrologs.Length);
            var pattern0 = env.Heuristics.ProcedurePrologs[0];

            Assert.AreEqual("55 8B EC", pattern0.Bytes);
            Assert.AreEqual("FF FF FF", pattern0.Mask);

            var pattern1 = env.Heuristics.ProcedurePrologs[1];

            Assert.AreEqual("55 ?? 30", pattern1.Bytes);
            Assert.IsNull(pattern1.Mask);
        }
Exemple #19
0
        private static void TestArchitecture(RekoConfigurationService cfg, ArchitectureDefinition archDef)
        {
            Console.Out.WriteLine("= Testing {0} ============", archDef.Description);
            var work = MakeWorkUnit(cfg, archDef, 42);

            if (work is null)
            {
                Console.Out.WriteLine("*** Failed to load {0}", archDef.Name);
                return;
            }
            var factories = new RewriterTaskFactory[] {
                new LinearTaskFactory(),
                new ShingleTaskFactory(),
                new LinearShingleTaskFactory()
            };

            foreach (var factory in factories)
            {
                CollectStatistics(work, factory);
            }
        }
        public void Rcfg_LoadOperatingEnvironment()
        {
            var cfgSvc = new RekoConfigurationService(new RekoConfiguration_v1
            {
                Environments = new []
                {
                    new Environment_v1
                    {
                        Name = "testOS",
                        Description = "Test OS",
                        Heuristics = new PlatformHeuristics_v1
                        {
                            ProcedurePrologs = new []
                            {
                                new BytePattern_v1
                                {
                                    Bytes = "55 8B EC",
                                    Mask = "FF FF FF",
                                },
                                new BytePattern_v1
                                {
                                    Bytes= "55 ?? 30"
                                }
                            }
                        }
                    }
                }
            });

            var env = cfgSvc.GetEnvironment("testOS");
            Assert.AreEqual(2, env.Heuristics.ProcedurePrologs.Length);
            var pattern0 = env.Heuristics.ProcedurePrologs[0];
            Assert.AreEqual("55 8B EC", pattern0.Bytes);
            Assert.AreEqual("FF FF FF", pattern0.Mask);

            var pattern1 = env.Heuristics.ProcedurePrologs[1];
            Assert.AreEqual("55 ?? 30", pattern1.Bytes);
            Assert.IsNull(pattern1.Mask);
        }
 public IConfigurationService CreateDecompilerConfiguration()
 {
     return(RekoConfigurationService.Load());
 }
        public void Rcfg_LoadOperatingEnvironment()
        {
            var sc     = new ServiceContainer();
            var cfgSvc = new RekoConfigurationService(sc, "reko.config", new RekoConfiguration_v1
            {
                Environments = new []
                {
                    new Environment_v1
                    {
                        Name        = "testOS",
                        Description = "Test OS",
                        Heuristics  = new PlatformHeuristics_v1
                        {
                            ProcedurePrologs = new []
                            {
                                new BytePattern_v1
                                {
                                    Bytes = "55 8B EC",
                                    Mask  = "FF FF FF",
                                },
                                new BytePattern_v1
                                {
                                    Bytes = "55 ?? 30"
                                }
                            }
                        },
                        Architectures = new[]
                        {
                            new PlatformArchitecture_v1
                            {
                                Name          = "testCPU",
                                TypeLibraries = new[]
                                {
                                    new TypeLibraryReference_v1
                                    {
                                        Name = "lp32.xml",
                                    }
                                }
                            }
                        }
                    }
                }
            });

            var env = cfgSvc.GetEnvironment("testOS");

            Assert.AreEqual(2, env.Heuristics.ProcedurePrologs.Length);
            var pattern0 = env.Heuristics.ProcedurePrologs[0];

            Assert.AreEqual("55 8B EC", pattern0.Bytes);
            Assert.AreEqual("FF FF FF", pattern0.Mask);

            var pattern1 = env.Heuristics.ProcedurePrologs[1];

            Assert.AreEqual("55 ?? 30", pattern1.Bytes);
            Assert.IsNull(pattern1.Mask);

            var archs = env.Architectures;

            Assert.AreEqual(1, archs.Count);
            Assert.AreEqual("lp32.xml", archs[0].TypeLibraries[0].Name);
        }
Exemple #23
0
 public CmdLineDriver(IServiceProvider services, RekoConfigurationService config)
 {
     this.services = services;
     this.config = config;
 }
Exemple #24
0
 private static void DumpRawFiles(RekoConfigurationService config, TextWriter w, string fmtString)
 {
     foreach (var raw in config.GetRawFiles()
         .OfType<RawFileElement>()
         .OrderBy(a => a.Name))
     {
         w.WriteLine(fmtString, raw.Name, raw.Description);
     }
 }
Exemple #25
0
 private static void DumpEnvironments(RekoConfigurationService config, TextWriter w, string fmtString)
 {
     foreach (var arch in config.GetEnvironments()
         .OfType<OperatingEnvironmentElement>()
         .OrderBy(a => a.Name))
     {
         w.WriteLine(fmtString, arch.Name, arch.Description);
     }
 }
Exemple #26
0
        public int Execute(string [] args)
        {
            TextReader input;
            Stream     output = Console.OpenStandardOutput();
            var        sc     = new ServiceContainer();

            sc.AddService(typeof(IPluginLoaderService), new PluginLoaderService());
            var rekoCfg = RekoConfigurationService.Load(sc);

            sc.AddService <IConfigurationService>(rekoCfg);

            var docopt = new Docopt();
            IDictionary <string, ValueObject> options;

            try {
                options = docopt.Apply(usage, args);
            } catch (Exception ex)
            {
                Console.Error.WriteLine(ex);
                return(1);
            }

            var arch = rekoCfg.GetArchitecture(options["-a"].ToString());

            if (arch == null)
            {
                Console.WriteLine(
                    "c2xml: unknown architecture '{0}'. Check the c2xml config file for supported architectures.",
                    options["-a"]);
                return(-1);
            }

            var envElem = rekoCfg.GetEnvironment(options["-e"].ToString());

            if (envElem == null)
            {
                Console.WriteLine(
                    "c2xml: unknown environment '{0}'. Check the c2xml config file for supported architectures.",
                    options["-e"]);
                return(-1);
            }
            var platform = envElem.Load(sc, arch);

            try
            {
                input = new StreamReader(options["<inputfile>"].ToString());
            }
            catch (Exception ex)
            {
                Console.Error.WriteLine("c2xml: unable to open file {0} for reading. {1}", options["<inputfile>"], ex.Message);
                return(1);
            }

            if (options.ContainsKey("<outputfile>") && options["<outputfile>"] != null)
            {
                try
                {
                    output = new FileStream(options["<outputfile>"].ToString(), FileMode.Create, FileAccess.Write);
                }
                catch (Exception ex)
                {
                    Console.Error.WriteLine("c2xml: unable to open file {0} for writing. {1}", options["<outputfile>"], ex.Message);
                    return(1);
                }
            }
            string dialect = null;

            if (options.TryGetValue("-d", out var optDialect) && optDialect != null)
            {
                dialect = (string)optDialect.Value;
            }
            var xWriter = new XmlTextWriter(output, new UTF8Encoding(false))
            {
                Formatting = Formatting.Indented
            };

            XmlConverter c = new XmlConverter(input, xWriter, platform, dialect);

            c.Convert();
            output.Flush();
            output.Close();
            return(0);
        }
Exemple #27
0
        public int Execute(string [] args)
        {
            TextReader input   = Console.In;
            TextWriter output  = Console.Out;
            var        sc      = new ServiceContainer();
            var        rekoCfg = RekoConfigurationService.Load();

            sc.AddService <IConfigurationService>(rekoCfg);

            var docopt = new Docopt();
            IDictionary <string, ValueObject> options;

            try {
                options = docopt.Apply(usage, args);
            } catch (Exception ex)
            {
                Console.Error.WriteLine(ex);
                return(1);
            }
            var arch = rekoCfg.GetArchitecture(options["-a"].ToString());

            if (arch == null)
            {
                Console.WriteLine(
                    "c2xml: unknown architecture '{0}'. Check the c2xml config file for supported architectures.",
                    options["-a"]);
                return(-1);
            }
            var envElem = rekoCfg.GetEnvironment(options["-e"].ToString());

            if (envElem == null)
            {
                Console.WriteLine(
                    "c2xml: unknown environment '{0}'. Check the c2xml config file for supported architectures.",
                    options["-e"]);
                return(-1);
            }

            var platform = envElem.Load(sc, arch);

            try
            {
                input = new StreamReader(options["<inputfile>"].ToString());
            }
            catch (Exception ex)
            {
                Console.Error.WriteLine("c2xml: unable to open file {0} for reading. {1}", options["<inputfile>"], ex.Message);
                return(1);
            }

            if (options.ContainsKey("<outputfile>") &&
                options["<outputfile>"] != null)
            {
                try
                {
                    output = new StreamWriter(options["<outputfile>"].ToString());
                }
                catch (Exception ex)
                {
                    Console.Error.WriteLine("c2xml: unable to open file {0} for writing. {1}", options["<outputfile>"], ex.Message);
                    return(1);
                }
            }
            var xWriter = new XmlTextWriter(output)
            {
                Formatting = Formatting.Indented
            };

            XmlConverter c = new XmlConverter(input, xWriter, platform);

            c.Convert();
            output.Flush();
            return(0);
        }
Exemple #28
0
 public CmdLineDriver(IServiceProvider services, RekoConfigurationService config)
 {
     this.services = services;
     this.config   = config;
 }