mirror of
https://github.com/momo5502/emulator.git
synced 2026-01-20 12:13:57 +00:00
Add capstone disassembler
This commit is contained in:
60
src/analyzer/disassembler.cpp
Normal file
60
src/analyzer/disassembler.cpp
Normal file
@@ -0,0 +1,60 @@
|
||||
#include "std_include.hpp"
|
||||
|
||||
#include "disassembler.hpp"
|
||||
|
||||
disassembler::disassembler()
|
||||
{
|
||||
const auto res = cs_open(CS_ARCH_X86, CS_MODE_64, &this->handle_);
|
||||
if (res != CS_ERR_OK)
|
||||
{
|
||||
throw std::runtime_error("Failed to initialize capstone");
|
||||
}
|
||||
}
|
||||
|
||||
disassembler::~disassembler()
|
||||
{
|
||||
this->release();
|
||||
}
|
||||
|
||||
disassembler::disassembler(disassembler&& obj) noexcept
|
||||
{
|
||||
this->operator=(std::move(obj));
|
||||
}
|
||||
|
||||
disassembler& disassembler::operator=(disassembler&& obj) noexcept
|
||||
{
|
||||
if (this != &obj)
|
||||
{
|
||||
this->release();
|
||||
this->handle_ = obj.handle_;
|
||||
obj.handle_ = 0;
|
||||
}
|
||||
|
||||
return *this;
|
||||
}
|
||||
|
||||
void disassembler::release()
|
||||
{
|
||||
if (this->handle_)
|
||||
{
|
||||
cs_close(&this->handle_);
|
||||
this->handle_ = 0;
|
||||
}
|
||||
}
|
||||
|
||||
instructions disassembler::disassemble(const std::span<const uint8_t> data, const size_t count) const
|
||||
{
|
||||
cs_insn* insts{};
|
||||
const auto inst_count = cs_disasm(this->handle_, data.data(), data.size(), count, 0, &insts);
|
||||
return instructions{std::span(insts, inst_count)};
|
||||
}
|
||||
|
||||
void instructions::release()
|
||||
{
|
||||
if (!this->instructions_.empty())
|
||||
{
|
||||
cs_free(this->instructions_.data(), this->instructions_.size());
|
||||
}
|
||||
|
||||
this->instructions_ = {};
|
||||
}
|
||||
Reference in New Issue
Block a user