Exemplo n.º 1
0
        /// <summary>
        /// Creates an <see cref="AssemblyDef"/> instance from a <see cref="DotNetFile"/>
        /// </summary>
        /// <param name="dnFile">The loaded .NET file</param>
        /// <param name="context">Module context or <c>null</c></param>
        /// <returns>A new <see cref="AssemblyDef"/> instance that now owns <paramref name="dnFile"/></returns>
        /// <exception cref="ArgumentNullException">If <paramref name="dnFile"/> is <c>null</c></exception>
        /// <exception cref="BadImageFormatException">If it's not a .NET assembly (eg. not a .NET file or only a .NET module)</exception>
        public static AssemblyDef Load(DotNetFile dnFile, ModuleContext context)
        {
            if (dnFile == null)
            {
                throw new ArgumentNullException("dnFile");
            }
            ModuleDef module = null;

            try {
                module = ModuleDefMD.Load(dnFile, context);
                var asm = module.Assembly;
                if (asm == null)
                {
                    throw new BadImageFormatException(string.Format("{0} is only a .NET module, not a .NET assembly. Use ModuleDef.Load().", module.ToString()));
                }
                return(asm);
            }
            catch {
                if (module != null)
                {
                    module.Dispose();
                }
                throw;
            }
        }
Exemplo n.º 2
0
 public void DoSetup()
 {
     CreateTempDir();
     mFile = new DotNetFile();
     mPath = Path.Combine(TempDir, "abc.txt");
     File.WriteAllText(mPath, "data");
 }
Exemplo n.º 3
0
        static void DoTest()
        {
            string exe = @"TestApp.dll";
            var    m   = new DotNetFile(exe);

            var decompiler = new IlDecompiler(m.EntryPoint);

            Console.WriteLine("Decompile of Main function:");
            var ilFormater   = new ILFormater(decompiler.Decompile());
            var outputString = ilFormater.Format();

            Console.WriteLine(outputString);
            Console.WriteLine("Running program:");
            DotNetClr clr = new DotNetClr(
                m,
                Path.Combine(Path.GetDirectoryName(System.Reflection.Assembly.GetEntryAssembly().Location),
                             "framework"));

            //Register our internal methods
            clr.RegisterCustomInternalMethod("TestsComplete", TestsComplete);
            clr.RegisterCustomInternalMethod("TestSuccess", TestSuccess);
            clr.RegisterCustomInternalMethod("TestFail", TestFail);

            clr.Start();
        }
Exemplo n.º 4
0
        public void We_Write_a_file()
        {
            fs = new DotNetFileSystem();
            fs.Write(filePath, contents);
            var f = new DotNetFile(FileName.GetFileName(@".\temp.txt"));

            f.CopyTo(FileName.GetFileName(@".\copy.txt"));
        }
        public File GetFileFromServer(Connection connection, string fileToDownload)
        {
            var tempFileName = GetUniqueFileName();

            DownloadFile(connection, fileToDownload, tempFileName);

            var file = new DotNetFile(new RelativeFileName(new RelativePathName(tempFileName)));
            return file;
        }
Exemplo n.º 6
0
        static void Main()
        {
            //This is for debugging purposes
            bool   doil2cpu = false;
            string il2cpu   = @"C:\Users\Misha\AppData\Roaming\Cosmos User Kit\Build\IL2CPU\IL2CPU.dll";
            string exe      = doil2cpu ? il2cpu : "TestApp.dll";


            //Create a new dotnetfile with the path to the EXE
            m = new DotNetFile(exe);

            //This is not needed, but this shows the IL code of the entry point
            var decompiler = new IlDecompiler(m.EntryPoint);

            Console.WriteLine("Decompile of Main function:");
            var ilFormater   = new ILFormater(decompiler.Decompile());
            var outputString = ilFormater.Format();

            Console.WriteLine(outputString);

            //This creates an instance of a CLR, and then runs it
            Console.WriteLine("Running program:");
            clr = new DotNetClr(
                m,
                Path.Combine(Path.GetDirectoryName(System.Reflection.Assembly.GetEntryAssembly().Location),
                             "framework"));

            //Register our internal methods
            clr.RegisterCustomInternalMethod("TestsComplete", TestsComplete);
            clr.RegisterCustomInternalMethod("TestSuccess", TestSuccess);
            clr.RegisterCustomInternalMethod("TestFail", TestFail);
            clr.RegisterCustomInternalMethod("TestsRxObject", TestRxObject);

            //Put arguments in the string array
            clr.Start(new string[] { "testArg" });


            if (NumbOfFailedTests >= 1)
            {
                Environment.Exit(1);
            }
        }
Exemplo n.º 7
0
        public void Dispose()
        {
            if (ownPeImage)
            {
                if (dnFile != null)
                {
                    dnFile.Dispose();
                }
                if (peImage != null)
                {
                    peImage.Dispose();
                }
            }
            if (peStream != null)
            {
                peStream.Dispose();
            }

            dnFile   = null;
            peImage  = null;
            peStream = null;
        }
Exemplo n.º 8
0
        protected override void BeforeRun()
        {
            CosmosVFS fs = new Sys.FileSystem.CosmosVFS();

            Sys.FileSystem.VFS.VFSManager.RegisterVFS(fs);
            Console.Clear();
            Console.WriteLine("Cosmos booted successfully.");

            try
            {
                var fl = new DotNetFile(TestApp.file);
                var vm = new DotNetVirtualMachine(fl);
                vm.Start();

                Console.WriteLine("Program exited.");
                Console.ReadLine();
            }
            catch (Exception x)
            {
                Console.WriteLine("Caught: " + x.Message);
            }
        }
Exemplo n.º 9
0
        protected override void BeforeRun()
        {
            //Init the file system
            var fs = new CosmosVFS();

            VFSManager.RegisterVFS(fs);
            Console.Clear();

            //Find the location where we booted from
            string boot  = "";
            bool   frame = false;

            foreach (var item in fs.GetVolumes())
            {
                Console.WriteLine("Found volume: " + item.mFullPath);
                foreach (var d in Directory.GetDirectories(item.mFullPath))
                {
                    Console.WriteLine("dir: " + d);
                }
                foreach (var d in Directory.GetFiles(item.mFullPath))
                {
                    Console.WriteLine("file: " + d);
                }
                if (File.Exists(item.mFullPath + "TESTAPP.DLL") | File.Exists(item.mFullPath + "dotnetparser.exe"))
                {
                    Console.WriteLine("Found boot volume: " + item.mFullPath);
                    boot = item.mFullPath;
                }
                if (Directory.Exists(item.mFullPath + "framework"))
                {
                    frame = true;
                }
                else if (Directory.Exists(item.mFullPath + "FRAMEW"))
                {
                    frame = false;
                }
            }
            if (boot == "")
            {
                Console.WriteLine("Cannot find the media that we booted from.");
                return;
            }
            try
            {
                byte[] fi = TestApp.file;
                if (File.Exists(boot + @"TESTAPP.DLL"))
                {
                    fi = File.ReadAllBytes(boot + @"TESTAPP.DLL");
                }
                else if (File.Exists(boot + @"DotNetparserTester.exe"))
                {
                    fi = File.ReadAllBytes(boot + @"DotNetparserTester.exe");
                }
                var fl  = new DotNetFile(fi);
                var clr = new DotNetClr(fl, frame ? boot + @"framework" : boot + @"FRAMEW");

                //Register our internal methods
                clr.RegisterCustomInternalMethod("TestsComplete", TestsComplete);
                clr.RegisterCustomInternalMethod("TestSuccess", TestSuccess);
                clr.RegisterCustomInternalMethod("TestFail", TestFail);

                clr.Start();
                Console.WriteLine("Program exec complete.");
            }
            catch (Exception x)
            {
                Console.WriteLine("Caught error: " + x.Message);
            }
        }
Exemplo n.º 10
0
 /// <summary>
 /// Creates an <see cref="AssemblyDef"/> instance from a <see cref="DotNetFile"/>
 /// </summary>
 /// <param name="dnFile">The loaded .NET file</param>
 /// <returns>A new <see cref="AssemblyDef"/> instance that now owns <paramref name="dnFile"/></returns>
 /// <exception cref="ArgumentNullException">If <paramref name="dnFile"/> is <c>null</c></exception>
 /// <exception cref="BadImageFormatException">If it's not a .NET assembly (eg. not a .NET file or only a .NET module)</exception>
 public static AssemblyDef Load(DotNetFile dnFile)
 {
     return(Load(dnFile, null));
 }