Beispiel #1
0
        public override Program Load(Address addrLoad)
        {
            BinHexDecoder      dec = new BinHexDecoder(new StringReader(Encoding.ASCII.GetString(RawImage)));
            IEnumerator <byte> stm = dec.GetBytes().GetEnumerator();
            BinHexHeader       hdr = LoadBinHexHeader(stm);

            byte[] dataFork = LoadFork(hdr.DataForkLength, stm);
            byte[] rsrcFork = LoadFork(hdr.ResourceForkLength, stm);

            var arch     = new M68kArchitecture();
            var platform = new MacOSClassic(Services, arch);

            if (hdr.FileType == "PACT")
            {
                Cpt.CompactProArchive        archive = new Cpt.CompactProArchive();
                List <ArchiveDirectoryEntry> items   = archive.Load(new MemoryStream(dataFork));
                IArchiveBrowserService       abSvc   = Services.GetService <IArchiveBrowserService>();
                if (abSvc != null)
                {
                    var selectedFile = abSvc.UserSelectFileFromArchive(items);
                    if (selectedFile != null)
                    {
                        var image = selectedFile.GetBytes();
                        this.rsrcFork = new ResourceFork(image, arch);
                        this.image    = new LoadedImage(addrLoad, image);
                        this.imageMap = new ImageMap(addrLoad, image.Length);
                        return(new Program(this.image, this.imageMap, arch, platform));
                    }
                }
            }

            var li = new LoadedImage(addrLoad, dataFork);

            return(new Program(li, li.CreateImageMap(), arch, platform));
        }
Beispiel #2
0
        public void DecSvc_DecompilerProjectName()
        {
            IDecompilerService svc = new DecompilerService();
            var loader             = mr.StrictMock <ILoader>();
            var host     = mr.StrictMock <DecompilerHost>();
            var arch     = mr.StrictMock <IProcessorArchitecture>();
            var platform = mr.StrictMock <Platform>(sc, arch);
            var fileName = "foo\\bar\\baz.exe";
            var bytes    = new byte[100];
            var image    = new LoadedImage(Address.Ptr32(0x1000), bytes);
            var imageMap = image.CreateImageMap();
            var prog     = new Program(image, imageMap, arch, platform);

            loader.Stub(l => l.LoadImageBytes(fileName, 0)).Return(bytes);
            loader.Stub(l => l.LoadExecutable(fileName, bytes, null)).Return(prog);
            loader.Replay();
            var dec = new DecompilerDriver(loader, host, sc);

            mr.ReplayAll();

            svc.Decompiler = dec;
            svc.Decompiler.Load(fileName);

            Assert.IsNotNull(svc.Decompiler.Project);
            Assert.AreEqual("baz.exe", svc.ProjectName, "Should have project name available.");
            mr.VerifyAll();
        }
        /// <summary>
        /// Create a simulatd program to use in the code /data display.
        /// </summary>
        private void GenerateSimulatedProgram()
        {
            var row   = Enumerable.Range(0, 0x100).Select(b => (byte)b).ToArray();
            var image = new LoadedImage(
                Address.Ptr32(0x0010000),
                Enumerable.Repeat(
                    row,
                    40).SelectMany(r => r).ToArray());
            var imageMap = image.CreateImageMap();
            var addrCode = Address.Ptr32(0x0010008);
            var addrData = Address.Ptr32(0x001001A);

            imageMap.AddItemWithSize(addrCode, new ImageMapBlock {
                Address = addrCode, Size = 0x0E
            });
            imageMap.AddItemWithSize(addrData, new ImageMapItem {
                Address = addrData, DataType = PrimitiveType.Byte, Size = 0x0E
            });
            var arch = dlg.Services.RequireService <IConfigurationService>().GetArchitecture("x86-protected-32");

            this.program = new Program
            {
                Image        = image,
                ImageMap     = imageMap,
                Architecture = arch,
            };
        }
 private void Given_Image(params byte[] bytes)
 {
     image              = new LoadedImage(addrBase, bytes);
     imageMap           = image.CreateImageMap();
     program            = new Program(image, imageMap, arch, null);
     interactor.Program = program;
 }
Beispiel #5
0
        /// <summary>
        /// Load a Basic PRG.
        /// </summary>
        /// <param name="imageBytes"></param>
        /// <returns></returns>
        private Program LoadPrg(byte[] imageBytes)
        {
            var    stm = new MemoryStream();
            ushort preferredAddress = LoadedImage.ReadLeUInt16(imageBytes, 0);
            ushort alignedAddress   = (ushort)(preferredAddress & ~0xF);
            int    pad = preferredAddress - alignedAddress;

            while (pad-- > 0)
            {
                stm.WriteByte(0);
            }
            stm.Write(imageBytes, 2, imageBytes.Length - 2);
            var loadedBytes = stm.ToArray();
            var image       = new LoadedImage(
                Address.Ptr16(alignedAddress),
                loadedBytes);
            var rdr  = new C64BasicReader(image, 0x0801);
            var prog = rdr.ToSortedList(line => (ushort)line.Address.ToLinear(), line => line);
            var arch = new C64Basic(prog);

            image = new LoadedImage(
                Address.Ptr16(prog.Keys[0]),
                new byte[0xFFFF]);
            var program = new Program(
                image,
                image.CreateImageMap(),
                arch,
                new C64Platform(Services, null));

            program.EntryPoints.Add(new EntryPoint(image.BaseAddress, arch.CreateProcessorState()));
            return(program);
        }
        public void Setup()
        {
            var image   = new LoadedImage(Address.Ptr32(0x00100000), new byte[1024]);
            var arch    = new FakeArchitecture();
            var program = new Program
            {
                Image        = image,
                Architecture = arch,
                ImageMap     = image.CreateImageMap(),
                Platform     = new DefaultPlatform(null, arch),
            };

            store   = program.TypeStore;
            factory = program.TypeFactory;
            globals = program.Globals;
            store.EnsureExpressionTypeVariable(factory, globals);

            StructureType s = new StructureType(null, 0);

            s.Fields.Add(0x00100000, PrimitiveType.Word32, null);

            TypeVariable     tvGlobals = store.EnsureExpressionTypeVariable(factory, globals);
            EquivalenceClass eqGlobals = new EquivalenceClass(tvGlobals);

            eqGlobals.DataType            = s;
            globals.TypeVariable.DataType = new Pointer(eqGlobals, 4);
            globals.DataType = globals.TypeVariable.DataType;

            tcr = new TypedConstantRewriter(program);
        }
        private void Given_Program(byte[] bytes)
        {
            var addr  = Address.Ptr32(0x1000);
            var image = new LoadedImage(addr, bytes);

            this.imageMap = image.CreateImageMap();
            this.program  = new Program(image, imageMap, arch, new DefaultPlatform(null, arch));
        }
Beispiel #8
0
 public ProgramBuilder(LoadedImage loadedImage)
 {
     Program = new Program
     {
         Image        = loadedImage,
         ImageMap     = loadedImage.CreateImageMap(),
         Architecture = new FakeArchitecture()
     };
 }
Beispiel #9
0
        public void Setup()
        {
            mr = new MockRepository();
            sc = new ServiceContainer();
            var image    = new LoadedImage(Address.SegPtr(0xC00, 0), Enumerable.Range(0x0, 0x100).Select(b => (byte)b).ToArray());
            var imageMap = image.CreateImageMap();
            var arch     = new Mocks.FakeArchitecture();

            this.program = new Program(image, imageMap, arch, new DefaultPlatform(sc, arch));
        }
Beispiel #10
0
        public Program GetImage()
        {
            var image = new LoadedImage(BaseAddress, emitter.GetBytes());

            return(new Program(
                       image,
                       image.CreateImageMap(),
                       arch,
                       new DefaultPlatform(null, arch)));
        }
Beispiel #11
0
        protected void Given_Image32(uint addr, string sBytes)
        {
            var bytes = HexStringToBytes(sBytes);
            var imag  = new LoadedImage(Address.Ptr32(addr), bytes);

            prog = new Program
            {
                Image    = imag,
                ImageMap = imag.CreateImageMap(),
            };
        }
Beispiel #12
0
        internal void Given_ImageSeg(ushort seg, ushort offset, string sBytes)
        {
            var bytes = HexStringToBytes(sBytes);
            var imag  = new LoadedImage(Address.SegPtr(seg, offset), bytes);

            prog = new Program
            {
                Image    = imag,
                ImageMap = imag.CreateImageMap()
            };
        }
Beispiel #13
0
        private void BuildTest32(Address addrBase, params byte[] bytes)
        {
            arch = new M68kArchitecture();
            var image = new LoadedImage(addrBase, bytes);

            program = new Program(
                image,
                image.CreateImageMap(),
                arch,
                new DefaultPlatform(null, arch));
            RunTest(addrBase);
        }
Beispiel #14
0
        public void Setup()
        {
            this.sc = new ServiceContainer();
            this.mr = new MockRepository();
            var image = new LoadedImage(Address.SegPtr(0x0C00, 0), new byte[100]);

            this.program = new Program
            {
                Image    = image,
                ImageMap = image.CreateImageMap()
            };
        }
Beispiel #15
0
        private void btnLoad_Click(object sender, EventArgs e)
        {
            var image    = new LoadedImage(Address.Ptr32(0x12312300), new byte[0x1000]);
            var imageMap = image.CreateImageMap();
            var arch     = new X86ArchitectureFlat32();
            var program  = new Core.Program(image, imageMap, arch, new DefaultPlatform(null, arch));
            var project  = new Project {
                Programs = { program }
            };

            pbs.Load(project);
        }
Beispiel #16
0
        public override Program Load(Address addrLoad)
        {
            int iImageStart = (exe.e_cparHeader * 0x10);
            int cbImageSize = exe.e_cpImage * ExeImageLoader.CbPageSize - iImageStart;

            byte[] bytes  = new byte[cbImageSize];
            int    cbCopy = Math.Min(cbImageSize, RawImage.Length - iImageStart);

            Array.Copy(RawImage, iImageStart, bytes, 0, cbCopy);
            imgLoaded    = new LoadedImage(addrLoad, bytes);
            imgLoadedMap = imgLoaded.CreateImageMap();
            return(new Program(imgLoaded, imgLoadedMap, arch, platform));
        }
Beispiel #17
0
        private void Given_Program(Address address)
        {
            var image    = new LoadedImage(address, new byte[1]);
            var imageMap = image.CreateImageMap();

            this.program = new Program
            {
                Architecture = arch,
                Image        = image,
                ImageMap     = imageMap,
                Platform     = new FakePlatform(null, arch)
            };
        }
Beispiel #18
0
        public override Program Load(Address addrLoad)
        {
            if (addrLoad == null)
            {
                addrLoad = PreferredBaseAddress;
            }
            var image = new LoadedImage(addrLoad, imageBytes);

            return(new Program(
                       image,
                       image.CreateImageMap(),
                       Architecture,
                       Platform ?? new DefaultPlatform(Services, Architecture)));
        }
Beispiel #19
0
        private void Given_Image(params byte[] bytes)
        {
            var image = new LoadedImage(Address.Ptr32(0x00400000), bytes);

            arch.Stub(a => a.CreateImageReader(image, null))
            .IgnoreArguments()
            .Return(new LeImageReader(image, image.BaseAddress));
            this.program = new Program
            {
                Image        = image,
                ImageMap     = image.CreateImageMap(),
                Architecture = arch
            };
        }
Beispiel #20
0
        public override Program Load(Address addrLoad)
        {
            if (sections > 0)
            {
                sectionMap = LoadSections(addrLoad, rvaSectionTable, sections);
                imgLoaded  = LoadSectionBytes(addrLoad, sectionMap);
                imageMap   = imgLoaded.CreateImageMap();
            }
            imgLoaded.BaseAddress = addrLoad;
            var program = new Program(imgLoaded, imageMap, arch, platform);

            this.importReferences = program.ImportReferences;
            return(program);
        }
        private void Given_ProgramWithOneSegment()
        {
            var image    = new LoadedImage(Address.Ptr32(0x12340000), new byte[0x1000]);
            var imageMap = image.CreateImageMap();

            imageMap.AddSegment(Address.Ptr32(0x12340000), ".text", AccessMode.Execute);
            var arch     = mr.StrictMock <IProcessorArchitecture>();
            var platform = new DefaultPlatform(sc, arch);

            this.program          = new Program(image, imageMap, arch, platform);
            this.program.Name     = "foo.exe";
            this.program.Filename = @"c:\test\foo.exe";
            project.Programs.Add(program);
        }
Beispiel #22
0
        private TestScanner CreateScanner(uint startAddress, int imageSize)
        {
            var image = new LoadedImage(Address.Ptr32(startAddress), new byte[imageSize]);

            program = new Program(
                image,
                image.CreateImageMap(),
                arch,
                new FakePlatform(null, arch));
            return(new TestScanner(
                       program,
                       callSigs,
                       importResolver));
        }
Beispiel #23
0
        /// <summary>
        /// Loads a program into memory, but performs no relocations.
        /// </summary>
        /// <param name="fileName"></param>
        /// <param name="arch"></param>
        /// <param name="platform"></param>
        public void LoadRawImage(string fileName, IProcessorArchitecture arch, Platform platform, Address addrBase)
        {
            eventListener.ShowStatus("Loading raw bytes.");
            byte[] image       = loader.LoadImageBytes(fileName, 0);
            var    loadedImage = new LoadedImage(addrBase, image);
            var    program     = new Program(
                loadedImage,
                loadedImage.CreateImageMap(),
                arch,
                platform);

            program.Name = Path.GetFileName(fileName);
            Project      = CreateDefaultProject(fileName, program);
            eventListener.ShowStatus("Raw bytes loaded.");
        }
Beispiel #24
0
        void LoadSegments(uint offset)
        {
            this.segments = ReadSegmentTable(offset, cSeg);
            var segFirst = segments[0];
            var segLast  = segments[segments.Length - 1];

            this.image = new LoadedImage(
                PreferredBaseAddress,
                new byte[segLast.LinearAddress + segLast.DataLength]);
            this.imageMap = image.CreateImageMap();
            foreach (var segment in segments)
            {
                LoadSegment(segment, image, imageMap);
            }
        }
Beispiel #25
0
        public override Program Load(Address addrLoad)
        {
            ldr = CreateParser();
            uint ncmds = ldr.ParseHeader(addrLoad);

            ldr.ParseLoadCommands(ncmds);
            var image = new LoadedImage(addrLoad, RawImage);

            return(new Program {
                Architecture = ldr.arch,
                Image = image,
                ImageMap = image.CreateImageMap(),
                Platform = new DefaultPlatform(Services, ldr.arch)
            });
        }
Beispiel #26
0
        public override Program Load(Address addr)
        {
            byte[] abC = RawImage;
            byte[] abU = new byte[cpUncompressed * 0x10U + ExeImageLoader.CbPsp];
            Array.Copy(abC, exeHdrSize, abU, ExeImageLoader.CbPsp, abC.Length - exeHdrSize);
            imgU = new LoadedImage(addr, abU);

            uint SI = hdrOffset - 1;

            while (abC[SI] == 0xFF)
            {
                --SI;
            }

            int  DI = abU.Length - 1;
            byte op;

            do
            {
                op = abC[SI];
                int cx = LoadedImage.ReadLeUInt16(abC, SI - 2);
                SI -= 3;
                if ((op & 0xFE) == 0xB0)
                {
                    byte b = abC[SI--];
                    while (cx != 0)
                    {
                        abU[DI--] = b;
                        --cx;
                    }
                }
                else
                {
                    if ((op & 0xFE) != 0xB2)
                    {
                        throw new ApplicationException("Packed file is corrupt.");
                    }
                    while (cx != 0)
                    {
                        abU[DI--] = abC[SI--];
                        --cx;
                    }
                }
            } while ((op & 1) == 0);
            imageMap = imgU.CreateImageMap();
            return(new Program(imgU, imageMap, new X86ArchitectureReal(), platform));
        }
Beispiel #27
0
        public override Program Load(Address addrLoad)
        {
            arch = new M68kArchitecture();
            var imgReader = new BeImageReader(RawImage, 0);
            var parse     = new HunkFileParser(imgReader, false);

            this.hunkFile = parse.Parse();
            BuildSegments();
            this.firstCodeHunk = parse.FindFirstCodeHunk();
            var image = new LoadedImage(addrLoad, RelocateBytes(addrLoad));

            return(new Program(
                       image,
                       image.CreateImageMap(),
                       arch,
                       new AmigaOSPlatform(Services, arch)));
        }
        public void Setup()
        {
            var image = new LoadedImage(Address.Ptr32(0x00400000), new byte[1024]);
            var arch  = new FakeArchitecture();

            program = new Program
            {
                Architecture = arch,
                Image        = image,
                ImageMap     = image.CreateImageMap(),
                Platform     = new DefaultPlatform(null, arch)
            };
            store   = program.TypeStore;
            factory = program.TypeFactory;
            point   = new StructureType(null, 0);
            point.Fields.Add(0, PrimitiveType.Word32, null);
            point.Fields.Add(4, PrimitiveType.Word32, null);
        }
Beispiel #29
0
        public override Program Load(Address addrLoad)
        {
            var cfgSvc = Services.RequireService <IConfigurationService>();

            arch = (M68kArchitecture)cfgSvc.GetArchitecture("m68k");
            var imgReader = new BeImageReader(RawImage, 0);
            var parse     = new HunkFileParser(imgReader, false);

            this.hunkFile = parse.Parse();
            BuildSegments();
            this.firstCodeHunk = parse.FindFirstCodeHunk();
            var image = new LoadedImage(addrLoad, RelocateBytes(addrLoad));

            return(new Program(
                       image,
                       image.CreateImageMap(),
                       arch,
                       cfgSvc.GetEnvironment("amigaOS").Load(Services, arch)));
        }
Beispiel #30
0
        public override Program Load(Address addrLoad)
        {
            if (sections > 0)
            {
                sectionMap = LoadSections(addrLoad, rvaSectionTable, sections);
                imgLoaded = LoadSectionBytes(addrLoad, sectionMap);
                ImageMap = imgLoaded.CreateImageMap();
            }
            imgLoaded.BaseAddress = addrLoad;
            this.program = new Program(imgLoaded, ImageMap, arch, platform);
            this.importReferences = program.ImportReferences;

            var rsrcLoader = new PeResourceLoader(this.imgLoaded, rvaResources);
            List<ProgramResource> items = rsrcLoader.Load();
            program.Resources.Resources.AddRange(items);
            program.Resources.Name = "PE resources";

            return program;
        }