/// <summary> /// Disassemble a byte array, triggering Callback on each instruction /// </summary> /// <param name="Data">Assembly instructions</param> /// <param name="Address">Address of the first instruction</param> /// /// <param name="Callback">Delegate function called per-instruction</param> public static void DisassembleAll(byte[] Data, ulong Address, OnDisassembly Callback) { using (Capstone Engine = CreateEngine()) { Engine.DisassembleIt(Data, Address, Callback); } }
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; } }
/// <summary> /// Disassemble a byte array, triggering Callback on each instruction /// </summary> /// <param name="Data">Assembly instructions</param> /// <param name="Address">Address of the first instruction</param> /// /// <param name="Callback">Delegate function called per-instruction</param> public void DisassembleIt(byte[] Data, ulong Address, OnDisassembly Callback) { Wrapper.StartDisassembly(); Wrapper.DisassembleIt(Data, Address, Callback); Wrapper.EndDisassembly(); }