CapstoneDisassembler.hpp 1.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465
  1. #pragma once
  2. #include <capstone/capstone.h>
  3. #include "ExceptionCapstone.hpp"
  4. #include "ResourceWrapper.hpp"
  5. #include "ResourceTraitsCapstone.hpp"
  6. namespace nkg {
  7. struct CapstoneContext {
  8. const void* lpMachineCode;
  9. size_t cbMachineCode;
  10. uint64_t Address;
  11. };
  12. class CapstoneEngine;
  13. class CapstoneDisassembler : private ARL::ResourceWrapper<ARL::ResourceTraits::CapstoneInsn> {
  14. friend class CapstoneEngine;
  15. private:
  16. const CapstoneEngine& m_Engine;
  17. CapstoneContext m_CurrentState;
  18. CapstoneContext m_NextState;
  19. cs_insn* m_lpCurrentInsn;
  20. CapstoneDisassembler(const CapstoneEngine& Engine);
  21. public:
  22. CapstoneDisassembler& SetContext(const CapstoneContext& Ctx) noexcept;
  23. [[nodiscard]]
  24. const CapstoneContext& GetContext() const noexcept;
  25. [[nodiscard]]
  26. bool Next() noexcept;
  27. [[nodiscard]]
  28. const cs_insn* GetInstruction() const noexcept;
  29. [[nodiscard]]
  30. const CapstoneContext& GetInstructionContext() const noexcept;
  31. };
  32. class CapstoneEngine : private ARL::ResourceWrapper<ARL::ResourceTraits::CapstoneHandle> {
  33. friend class CapstoneDisassembler;
  34. public:
  35. CapstoneEngine(cs_arch ArchType, cs_mode Mode);
  36. void Option(cs_opt_type Type, cs_opt_value Value);
  37. const char* GetGroupName(unsigned int group_id) const noexcept;
  38. const char* GetInstructionName(unsigned int instruction_id) const noexcept;
  39. const char* GetRegisterName(unsigned int register_id) const noexcept;
  40. [[nodiscard]]
  41. CapstoneDisassembler CreateDisassembler() const;
  42. };
  43. }