KeystoneAssembler.cpp 1.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445
  1. #include "KeystoneAssembler.hpp"
  2. #undef NKG_CURRENT_SOURCE_FILE
  3. #undef NKG_CURRENT_SOURCE_LINE
  4. #define NKG_CURRENT_SOURCE_FILE() TEXT(".\\navicat-patcher\\KeystoneAssembler.cpp")
  5. #define NKG_CURRENT_SOURCE_LINE() __LINE__
  6. namespace nkg {
  7. KeystoneAssembler::KeystoneAssembler(const KeystoneEngine& Engine) noexcept :
  8. _Engine(Engine) {}
  9. [[nodiscard]]
  10. std::vector<uint8_t> KeystoneAssembler::GenerateMachineCode(const char* AssemblyCode, uint64_t Address) const {
  11. ResourceOwned pbMachineCode(KeystoneMallocTraits{});
  12. size_t cbMachineCode = 0;
  13. size_t InstructionsProcessed = 0;
  14. if (ks_asm(_Engine, AssemblyCode, Address, pbMachineCode.GetAddressOf(), &cbMachineCode, &InstructionsProcessed) != 0) {
  15. throw KeystoneError(NKG_CURRENT_SOURCE_FILE(), NKG_CURRENT_SOURCE_LINE(), ks_errno(_Engine), TEXT("ks_asm failed."));
  16. }
  17. return std::vector<uint8_t>(pbMachineCode.Get(), pbMachineCode.Get() + cbMachineCode);
  18. }
  19. KeystoneEngine::KeystoneEngine(ks_arch ArchType, ks_mode Mode) {
  20. auto err = ks_open(ArchType, Mode, GetAddressOf());
  21. if (err != KS_ERR_OK) {
  22. throw KeystoneError(NKG_CURRENT_SOURCE_FILE(), NKG_CURRENT_SOURCE_LINE(), err, TEXT("ks_open failed."));
  23. }
  24. }
  25. void KeystoneEngine::Option(ks_opt_type Type, ks_opt_value Value) {
  26. auto err = ks_option(Get(), Type, Value);
  27. if (err != KS_ERR_OK) {
  28. throw KeystoneError(NKG_CURRENT_SOURCE_FILE(), NKG_CURRENT_SOURCE_LINE(), err, TEXT("ks_option failed."));
  29. }
  30. }
  31. KeystoneAssembler KeystoneEngine::CreateAssembler() const {
  32. return KeystoneAssembler(*this);
  33. }
  34. }