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);
        }
 private static SparcInstruction DisassembleWord(uint instr)
 {
     var bytes = new byte[4];
     new BeImageWriter(bytes).WriteBeUInt32(0, instr);
     var img = new LoadedImage(Address.Ptr32(0x00100000), bytes);
     return Disassemble(img);
 }
 private IntelInstruction Disassemble64(params byte[] bytes)
 {
     var img = new LoadedImage(Address.Ptr64(0x10000), bytes);
     var rdr = img.CreateLeReader(img.BaseAddress);
     var dasm = new X86Disassembler(rdr, PrimitiveType.Word32, PrimitiveType.Word64, true);
     return dasm.First();
 }
Beispiel #4
0
        public ushort ReadBeUInt16()
        {
            ushort u = LoadedImage.ReadBeUInt16(bytes, (uint)off);

            off += 2;
            return(u);
        }
Beispiel #5
0
 public Program(LoadedImage image, ImageMap imageMap, IProcessorArchitecture arch, Platform platform) : this()
 {
     this.Image = image;
     this.ImageMap = imageMap;
     this.Architecture = arch;
     this.Platform = platform;
 }
 private IntelInstruction Disassemble16(params byte[] bytes)
 {
     LoadedImage img = new LoadedImage(Address.SegPtr(0xC00, 0), bytes);
     ImageReader rdr = img.CreateLeReader(img.BaseAddress);
     var dasm = new X86Disassembler(rdr, PrimitiveType.Word16, PrimitiveType.Word16, false);
     return dasm.First();
 }
 private X86Instruction Disassemble32(params byte[] bytes)
 {
     var img = new LoadedImage(Address.Ptr32(0x10000), bytes);
     var rdr = img.CreateLeReader(img.BaseAddress);
     var dasm = new X86Disassembler(ProcessorMode.Protected32, rdr, PrimitiveType.Word32, PrimitiveType.Word32, false);
     return dasm.First();
 }
 private void BuildTest(params ushort[] words)
 {
     var bytes = words
         .SelectMany(u => new byte[] { (byte)u, (byte)(u >> 8), })
         .ToArray();
     image = new LoadedImage(Address.Ptr32(0x00100000), bytes);
 }
Beispiel #9
0
 private void BuildTest(params string[] bitStrings)
 {
     var bytes = bitStrings.Select(bits => base.ParseBitPattern(bits))
         .SelectMany(u => new byte[] { (byte) u, (byte) (u >> 8), (byte) (u >> 16), (byte) (u >> 24) })
         .ToArray();
     image = new LoadedImage(Address.Ptr32(0x00100000), bytes);
 }
        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();
        }
		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);
		}
Beispiel #12
0
		public void UShortFixup()
		{
			var bytes = new byte[] { 0x01, 0x02, 0x03 };
			var img = new LoadedImage(Address.SegPtr(0x0C00, 0), bytes);
			ushort newSeg = img.FixupLeUInt16(1, 0x4444);
			Assert.AreEqual(0x4746, newSeg);
		}
Beispiel #13
0
        public uint ReadBeUInt32()
        {
            uint u = LoadedImage.ReadBeUInt32(bytes, off);

            off += 4;
            return(u);
        }
Beispiel #14
0
 public void ValidateImage()
 {
     Program prog = new Program();
     LoadedImage rawImage = new LoadedImage(Address.SegPtr(0x0C00, 0), CreateMsdosHeader());
     ExeImageLoader exe = new ExeImageLoader(null, "foo.exe", rawImage.Bytes);
     Assert.IsTrue(PkLiteUnpacker.IsCorrectUnpacker(exe, rawImage.Bytes));
 }
		public void Setup()
		{
            mr = new MockRepository();
            form = new MainForm();
            sc = new ServiceContainer();
            loader = mr.StrictMock<ILoader>();
            dec = mr.StrictMock<IDecompiler>();
            sc = new ServiceContainer();
            uiSvc = new FakeShellUiService();
            host = mr.StrictMock<DecompilerHost>();
            memSvc = mr.StrictMock<ILowLevelViewService>();
            var image = new LoadedImage(Address.Ptr32(0x10000), new byte[1000]);
            var imageMap = image.CreateImageMap();
            var arch = mr.StrictMock<IProcessorArchitecture>();
            arch.Stub(a => a.CreateRegisterBitset()).Return(new BitSet(32));
            arch.Replay();
            var platform = mr.StrictMock<Platform>(null, arch);
            arch.BackToRecord();
            program = new Program(image, imageMap, arch, platform);
            project = new Project { Programs = { program } };

            browserSvc = mr.StrictMock<IProjectBrowserService>();

            sc.AddService<IDecompilerUIService>(uiSvc);
            sc.AddService(typeof(IDecompilerShellUiService), uiSvc);
            sc.AddService(typeof(IDecompilerService), new DecompilerService());
            sc.AddService(typeof(IWorkerDialogService), new FakeWorkerDialogService());
            sc.AddService(typeof(DecompilerEventListener), new FakeDecompilerEventListener());
            sc.AddService(typeof(IProjectBrowserService), browserSvc);
            sc.AddService(typeof(ILowLevelViewService), memSvc);
            sc.AddService<ILoader>(loader);

            i = new TestInitialPageInteractor(sc, dec);

		}
Beispiel #16
0
        public ulong ReadBeUInt64()
        {
            ulong u = LoadedImage.ReadBeUInt64(bytes, off);

            off += 8;
            return(u);
        }
        public void HSC_x86_FindCallsToProcedure()
        {
#if OLD
            var image = new LoadedImage(Address.Ptr32(0x001000), new byte[] {
                0xE8, 0x0B, 0x00, 0x00,  0x00, 0xE8, 0x07, 0x00,
                0x00, 0x00, 0xC3, 0x00,  0x00, 0x00, 0x00, 0x00,
                0xC3, 0xC3                                      // 1010, 1011
            });
            prog = new Program
            {
                Image = image,
                ImageMap = image.CreateImageMap(),
                Architecture = new IntelArchitecture(ProcessorMode.Protected32),
            };
#else
            Given_Image32(0x001000, 
                "E8 0B 00 00 00 E8 07 00 " +
                "00 00 C3 00 00 00 00 00 " +
                "C3 C3 ");                                     // 1010, 1011
            Given_x86_32();
#endif
            Given_RewriterHost();
            mr.ReplayAll();

            Assert.AreEqual(18, prog.Image.Length);

            var hsc = new HeuristicScanner(prog, host, eventListener);
            var linAddrs = hsc.FindCallOpcodes(new Address[]{
                Address.Ptr32(0x1010),
                Address.Ptr32(0x1011)}).ToList();

            Assert.AreEqual(2, linAddrs.Count);
            Assert.IsTrue(linAddrs.Contains(Address.Ptr32(0x1000)));
            Assert.IsTrue(linAddrs.Contains(Address.Ptr32(0x1005)));
        }
Beispiel #18
0
 private MachineInstruction RunTest(params byte[] bytes)
 {
     var image = new LoadedImage(Address.Ptr32(0x200), bytes);
     var rdr = new LeImageReader(image, 0);
     var dasm = new Disassembler(rdr);
     return dasm.First();
 }
Beispiel #19
0
 public Program(LoadedImage image, ImageMap imageMap, IProcessorArchitecture arch, Platform platform) : this()
 {
     this.Image        = image;
     this.ImageMap     = imageMap;
     this.Architecture = arch;
     this.Platform     = platform;
 }
 private void RunTest(string sExp, params byte[] bytes)
 {
     var image = new LoadedImage(Address.Ptr32(0x0100000), bytes);
     var dasm = new CilDisassembler(image.CreateLeReader(0)).GetEnumerator();
     Assert.IsTrue(dasm.MoveNext());
     var instr = dasm.Current;
     Assert.AreEqual(sExp, instr.ToString());
 }
Beispiel #21
0
 public void DumpData(LoadedImage image, Address address, int cbBytes, TextWriter stm)
 {
     if (cbBytes < 0)
     {
         throw new ArgumentException("Must be a nonnegative number.", "cbBytes");
     }
     DumpData(image, address, (uint)cbBytes, stm);
 }
Beispiel #22
0
 private void BuildTest(params ushort[] words)
 {
     var bytes = words
         .SelectMany(
             w => new byte[] { (byte) w, (byte) (w >> 8) })
         .ToArray();
     image = new LoadedImage(LoadAddress, bytes);
 }
Beispiel #23
0
 public void ReadLeNegativeInt()
 {
     LoadedImage img = new LoadedImage(Address.Ptr32(0x10000), new byte[] {
         0xFE, 0xFF, 0xFF, 0xFF });
     Constant c = img.ReadLe(0, PrimitiveType.Int32);
     Assert.AreSame(PrimitiveType.Int32, c.DataType);
     Assert.AreEqual("-2", c.ToString());
 }
Beispiel #24
0
 public void ReadLeUShort()
 {
     LoadedImage img = new LoadedImage(Address.Ptr32(0x10000), new byte[] {
         0x78, 0x56, 0x34, 0x12 });
     Constant c = img.ReadLe(2, PrimitiveType.Word16);
     Assert.AreSame(PrimitiveType.Word16, c.DataType);
     Assert.AreEqual("0x1234", c.ToString());
 }
Beispiel #25
0
 public X86Emulator(IntelArchitecture arch, LoadedImage loadedImage, IPlatformEmulator envEmulator)
 {
     this.arch = arch;
     this.img = loadedImage;
     this.Registers = new ulong[40];
     this.Valid = new bool[40];
     this.envEmulator = envEmulator;
 }
 private void CreateDisassembler32(LoadedImage image)
 {
     dasm = new X86Disassembler(
         image.CreateLeReader(image.BaseAddress),
         PrimitiveType.Word32,
         PrimitiveType.Word32,
         false);
 }
Beispiel #27
0
 protected ImageReader(LoadedImage img, ulong off)
 {
     if (img == null)
         throw new ArgumentNullException("img");
     this.image = img;
     this.bytes = img.Bytes;
     this.addrStart = img.BaseAddress + off;
     this.off = offStart = off;
 }
 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 Reko.Arch.X86.X86ArchitectureFlat32();
     var program = new Core.Program(image, imageMap, arch, new DefaultPlatform(null, arch));
     var project = new Project { Programs = { program } };
     pbs.Load(project);
 }
Beispiel #29
0
        public void WriteByteRange(LoadedImage image, Address begin, Address addrEnd, TextWriter writer)
        {
            ImageReader rdr = arch.CreateImageReader(image, begin);

            while (rdr.Address < addrEnd)
            {
                writer.Write("{0:X2} ", rdr.ReadByte());
            }
        }
Beispiel #30
0
 public Program GetImage()
 {
     var image = new LoadedImage(BaseAddress, Emitter.GetBytes());
     return new Program(
         image,
         image.CreateImageMap(),
         arch, 
         new DefaultPlatform(null, arch));
 }
 private void CreateDisassembler16(LoadedImage image)
 {
     dasm = new X86Disassembler(
         ProcessorMode.Real,
         image.CreateLeReader(image.BaseAddress),
         PrimitiveType.Word16,
         PrimitiveType.Word16,
         false);
 }
Beispiel #32
0
 public bool TryReadBeUInt32(out uint ui32)
 {
     if (!LoadedImage.TryReadBeUInt32(this.bytes, (uint)off, out ui32))
     {
         return(false);
     }
     off += 4;
     return(true);
 }
 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 #34
0
 public ProgramBuilder(LoadedImage loadedImage)
 {
     Program = new Program
     {
         Image = loadedImage,
         ImageMap = loadedImage.CreateImageMap(),
         Architecture = new FakeArchitecture()
     };
 }
Beispiel #35
0
 public bool TryReadBeUInt16(out ushort us)
 {
     if (!LoadedImage.TryReadBeUInt16(bytes, (uint)off, out us))
     {
         return(false);
     }
     off += 2;
     return(true);
 }
Beispiel #36
0
 public bool TryReadBeUInt64(out ulong value)
 {
     if (!LoadedImage.TryReadBeUInt64(this.bytes, off, out value))
     {
         return(false);
     }
     off += 8;
     return(true);
 }
Beispiel #37
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(),
     };
 }
 protected override IEnumerable<RtlInstructionCluster> GetInstructionStream(Frame frame, IRewriterHost host)
 {
     var addr = Address.Ptr16(10);
     var image = new LoadedImage(addr, new byte[1]);
     return arch.CreateRewriter(
         arch.CreateImageReader(image, addr),
         arch.CreateProcessorState(),
         frame,
         host);
 }
Beispiel #39
0
 private void Rewrite(params ushort[] opcodes)
 {
     byte[] bytes = new byte[opcodes.Length * 2];
     var writer = new BeImageWriter(bytes);
     foreach (ushort opcode in opcodes)
     {
         writer.WriteBeUInt16(opcode);
     }
     image = new LoadedImage(addrBase, bytes);
 }
Beispiel #40
0
 protected ImageReader(LoadedImage img, ulong off)
 {
     if (img == null)
     {
         throw new ArgumentNullException("img");
     }
     this.image     = img;
     this.bytes     = img.Bytes;
     this.addrStart = img.BaseAddress + off;
     this.off       = offStart = off;
 }
Beispiel #41
0
        /// <summary>
        /// Reads a chunk of bytes and interprets it in Little-Endian mode.
        /// </summary>
        /// <param name="type">Enough bytes read </param>
        /// <returns>The read value as a <see cref="Constant"/>.</returns>
        public Constant ReadLe(PrimitiveType type)
        {
            Constant c;

            if (image != null)
            {
                c = image.ReadLe(off, type);
            }
            else
            {
                c = LoadedImage.ReadLe(bytes, Offset, type);
            }
            off += (uint)type.Size;
            return(c);
        }
Beispiel #42
0
        public void DumpData(LoadedImage image, Address address, long cbBytes, TextWriter stm)
        {
            ulong       cSkip = address.ToLinear() & 0x0F;
            ImageReader rdr   = arch.CreateImageReader(image, address);

            while (cbBytes > 0)
            {
                StringBuilder sb = new StringBuilder(0x12);
                try
                {
                    stm.Write("{0} ", rdr.Address);
                    for (int i = 0; i < 16; ++i)
                    {
                        if (cbBytes > 0 && cSkip == 0)
                        {
                            byte b = rdr.ReadByte();
                            stm.Write("{0:X2} ", b);
                            sb.Append(0x20 <= b && b < 0x7F
                                                                ? (char)b
                                                                : '.');
                            --cbBytes;
                        }
                        else
                        {
                            stm.Write("   ");
                            if (cSkip > 0)
                            {
                                sb.Append(' ');
                            }
                            --cSkip;
                        }
                    }
                }
                catch
                {
                    stm.WriteLine();
                    stm.WriteLine("...end of image");
                    return;
                }
                stm.WriteLine(sb.ToString());
            }
        }
Beispiel #43
0
        public bool DumpAssemblerLine(LoadedImage image, MachineInstruction instr, TextWriter writer)
        {
            Address addrBegin = instr.Address;

            if (ShowAddresses)
            {
                writer.Write("{0} ", addrBegin);
            }
            if (ShowCodeBytes)
            {
                StringWriter sw = new StringWriter();
                WriteByteRange(image, instr.Address, instr.Address + instr.Length, sw);
                writer.WriteLine("{0,-16}\t{1}", sw.ToString(), instr);
            }
            else
            {
                writer.WriteLine("\t{0}", instr.ToString());
            }
            return(true);
        }
Beispiel #44
0
        protected ImageReader(LoadedImage img, Address addr)
        {
            if (img == null)
            {
                throw new ArgumentNullException("img");
            }
            if (addr == null)
            {
                throw new ArgumentNullException("addr");
            }
            long o = addr - img.BaseAddress;

            if (o < 0 || o >= img.Length)
            {
                throw new ArgumentOutOfRangeException("addr", "Address is outside of image.");
            }
            this.image     = img;
            this.bytes     = img.Bytes;
            this.addrStart = addr;
            this.off       = offStart = (ulong)o;
        }
Beispiel #45
0
        public void DumpAssembler(LoadedImage image, Address addrStart, Address addrLast, TextWriter writer)
        {
            var dasm = arch.CreateDisassembler(arch.CreateImageReader(image, addrStart));

            try
            {
                foreach (var instr in dasm)
                {
                    if (instr.Address >= addrLast)
                    {
                        break;
                    }
                    if (!DumpAssemblerLine(image, instr, writer))
                    {
                        break;
                    }
                }
            }
            catch (Exception ex)
            {
                writer.WriteLine(ex.Message);
                writer.WriteLine();
            }
        }
Beispiel #46
0
 public ushort PeekLeUInt16(uint offset)
 {
     return(LoadedImage.ReadLeUInt16(bytes, offset + (uint)off));
 }
Beispiel #47
0
 public short PeekBeInt16(uint offset)
 {
     return((short)LoadedImage.ReadBeUInt16(bytes, offset + (uint)off));
 }
Beispiel #48
0
 public bool TryPeekByte(uint offset, out byte value)
 {
     return(LoadedImage.TryReadByte(bytes, offset + (uint)off, out value));
 }
Beispiel #49
0
 public abstract ImageReader CreateImageReader(LoadedImage img, Address addr);
Beispiel #50
0
 public abstract ImageReader CreateImageReader(LoadedImage img, ulong off);
Beispiel #51
0
 public void DumpData(LoadedImage image, AddressRange range, TextWriter stm)
 {
     DumpData(image, range.Begin, (long)(range.End - range.Begin), stm);
 }
Beispiel #52
0
 public uint PeekLeUInt32(uint offset)
 {
     return(LoadedImage.ReadLeUInt32(bytes, offset + off));
 }
Beispiel #53
0
 public BeImageReader(LoadedImage image, Address addr) : base(image, addr)
 {
 }
Beispiel #54
0
 public BeImageReader(LoadedImage image, ulong offset) : base(image, offset)
 {
 }
Beispiel #55
0
 public bool TryPeekBeUInt32(uint offset, out uint value)
 {
     return(LoadedImage.TryReadBeUInt32(bytes, offset + (uint)off, out value));
 }
Beispiel #56
0
 public long PeekBeInt64(uint offset)
 {
     return((long)LoadedImage.ReadBeUInt64(bytes, off));
 }
Beispiel #57
0
 public ulong PeekLeUInt64(uint offset)
 {
     return(LoadedImage.ReadLeUInt64(bytes, off));
 }
Beispiel #58
0
 public int PeekBeInt32(uint offset)
 {
     return((int)LoadedImage.ReadBeUInt32(bytes, offset + off));
 }
Beispiel #59
0
 public abstract ImageReader CreateNew(LoadedImage image, Address addr);
Beispiel #60
0
 public override ImageReader CreateNew(LoadedImage image, Address addr)
 {
     return(new BeImageReader(image, (uint)(addr - image.BaseAddress)));
 }