keystone_assembler.hpp 1.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758
  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;
  12. private:
  13. resource_wrapper<resource_traits::keystone::keystone_handle> m_keystone_engine;
  14. public:
  15. keystone_assembler(ks_arch architecture, ks_mode mode);
  16. void option(ks_opt_type option_type, size_t option_value);
  17. [[nodiscard]]
  18. std::vector<uint8_t> assemble(std::string_view asm_string, uint64_t asm_address = 0) const;
  19. };
  20. class keystone_assembler::backend_error : public ::nkg::exception {
  21. public:
  22. using error_code_t = ks_err;
  23. private:
  24. error_code_t m_error_code;
  25. std::string m_error_string;
  26. public:
  27. backend_error(std::string_view file, int line, error_code_t keystone_err, std::string_view message) noexcept :
  28. ::nkg::exception(file, line, message), m_error_code(keystone_err), m_error_string(ks_strerror(keystone_err)) {}
  29. [[nodiscard]]
  30. virtual bool error_code_exists() const noexcept override {
  31. return true;
  32. }
  33. [[nodiscard]]
  34. virtual intptr_t error_code() const noexcept override {
  35. return m_error_code;
  36. }
  37. [[nodiscard]]
  38. virtual const std::string& error_string() const noexcept override {
  39. return m_error_string;
  40. }
  41. };
  42. }