示例#1
0
        public void DisassembleIt(byte[] Data, ulong Address, OnDisassembly Callback)
        {
            IntPtr DataPtr        = Marshal.UnsafeAddrOfPinnedArrayElement(Data, 0);
            uint   CodeSize       = (uint)Data.Length;
            ulong  CurrentAddress = Address;

            CapstoneInstruction Instruction = new CapstoneInstruction();

            ulong LastAddress = CurrentAddress;

            while (CodeSize > 0)
            {
                if (!CapstoneAPI.cs_disasm_iter(Handle, ref DataPtr, ref CodeSize, ref CurrentAddress, InstrBuffer))
                {
                    break;
                }

                // Fix bug where cs_disasm_iter can get stuck in a loop but return true
                if (CurrentAddress == LastAddress)
                {
                    break;
                }

                LastInstruction.Read(InstrBuffer);

                Instruction.Address     = LastInstruction.address;
                Instruction.Disassembly = string.Format("{0,-12}{1}", LastInstruction.mnemonic, LastInstruction.op_str);
                Instruction.Bytecode    = new byte[LastInstruction.size];
                Array.Copy(LastInstruction.bytes, Instruction.Bytecode, LastInstruction.size);

                Callback(Instruction);

                LastAddress = CurrentAddress;
            }
        }