keystone_assembler.hpp 1.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455
  1. #pragma once
  2. #include <string>
  3. #include <vector>
  4. #include <keystone/keystone.h>
  5. #include "resource_wrapper.hpp"
  6. #include "resource_traits/keystone/keystone_handle.hpp"
  7. #include "exception.hpp"
  8. namespace nkg {
  9. class keystone_assembler {
  10. public:
  11. class backend_error : public ::nkg::exception {
  12. public:
  13. using error_code_t = ks_err;
  14. private:
  15. error_code_t m_error_code;
  16. std::string m_error_string;
  17. public:
  18. backend_error(std::string_view file, int line, error_code_t keystone_err, std::string_view message) noexcept :
  19. ::nkg::exception(file, line, message), m_error_code(keystone_err), m_error_string(ks_strerror(keystone_err)) {}
  20. [[nodiscard]]
  21. virtual bool error_code_exists() const noexcept override {
  22. return true;
  23. }
  24. [[nodiscard]]
  25. virtual intptr_t error_code() const noexcept override {
  26. return m_error_code;
  27. }
  28. [[nodiscard]]
  29. virtual const std::string& error_string() const noexcept override {
  30. return m_error_string;
  31. }
  32. };
  33. private:
  34. resource_wrapper<resource_traits::keystone::keystone_handle> m_keystone_engine;
  35. public:
  36. keystone_assembler(ks_arch architecture, ks_mode mode);
  37. void option(ks_opt_type option_type, size_t option_value);
  38. std::vector<uint8_t> assemble(std::string_view asm_string, uint64_t asm_address = 0) const;
  39. };
  40. }