Use singleton class

This commit is contained in:
offtkp 2024-09-17 17:24:57 +03:00
parent 6cc61fc27d
commit 1cd0616186
4 changed files with 16 additions and 17 deletions

View File

@ -6,14 +6,14 @@
namespace Common {
Decoder::Decoder() {
DecoderImpl::DecoderImpl() {
ZydisDecoderInit(&m_decoder, ZYDIS_MACHINE_MODE_LONG_64, ZYDIS_STACK_WIDTH_64);
ZydisFormatterInit(&m_formatter, ZYDIS_FORMATTER_STYLE_INTEL);
}
Decoder::~Decoder() = default;
DecoderImpl::~DecoderImpl() = default;
void Decoder::printInstruction(void* code, u64 address) {
void DecoderImpl::printInstruction(void* code, u64 address) {
ZydisDecodedInstruction instruction;
ZydisDecodedOperand operands[ZYDIS_MAX_OPERAND_COUNT_VISIBLE];
ZyanStatus status =
@ -25,7 +25,8 @@ void Decoder::printInstruction(void* code, u64 address) {
}
}
void Decoder::printInst(ZydisDecodedInstruction& inst, ZydisDecodedOperand* operands, u64 address) {
void DecoderImpl::printInst(ZydisDecodedInstruction& inst, ZydisDecodedOperand* operands,
u64 address) {
const int bufLen = 256;
char szBuffer[bufLen];
ZydisFormatterFormatInstruction(&m_formatter, &inst, operands, inst.operand_count_visible,
@ -33,8 +34,8 @@ void Decoder::printInst(ZydisDecodedInstruction& inst, ZydisDecodedOperand* oper
fmt::print("instruction: {}\n", szBuffer);
}
ZyanStatus Decoder::decodeInstruction(ZydisDecodedInstruction& inst, ZydisDecodedOperand* operands,
void* data, u64 size) {
ZyanStatus DecoderImpl::decodeInstruction(ZydisDecodedInstruction& inst,
ZydisDecodedOperand* operands, void* data, u64 size) {
return ZydisDecoderDecodeFull(&m_decoder, data, size, &inst, operands);
}

View File

@ -4,28 +4,26 @@
#pragma once
#include <Zydis/Zydis.h>
#include "common/singleton.h"
#include "common/types.h"
namespace Common {
class Decoder {
class DecoderImpl {
public:
Decoder();
~Decoder();
DecoderImpl();
~DecoderImpl();
void printInst(ZydisDecodedInstruction& inst, ZydisDecodedOperand* operands, u64 address);
void printInstruction(void* code, u64 address);
ZyanStatus decodeInstruction(ZydisDecodedInstruction& inst, ZydisDecodedOperand* operands,
void* data, u64 size = 15);
static Decoder& Instance() {
static Decoder instance;
return instance;
}
private:
ZydisDecoder m_decoder;
ZydisFormatter m_formatter;
};
using Decoder = Common::Singleton<DecoderImpl>;
} // namespace Common

View File

@ -663,7 +663,7 @@ static PatchModule* GetModule(const void* ptr) {
static std::pair<bool, u64> TryPatch(u8* code, PatchModule* module) {
ZydisDecodedInstruction instruction;
ZydisDecodedOperand operands[ZYDIS_MAX_OPERAND_COUNT];
const auto status = Common::Decoder::Instance().decodeInstruction(instruction, operands, code,
const auto status = Common::Decoder::Instance()->decodeInstruction(instruction, operands, code,
module->end - code);
if (!ZYAN_SUCCESS(status)) {
return std::make_pair(false, 1);

View File

@ -69,7 +69,7 @@ static std::string DisassembleInstruction(void* code_address) {
ZydisDecodedInstruction instruction;
ZydisDecodedOperand operands[ZYDIS_MAX_OPERAND_COUNT];
const auto status =
Common::Decoder::Instance().decodeInstruction(instruction, operands, code_address);
Common::Decoder::Instance()->decodeInstruction(instruction, operands, code_address);
if (ZYAN_SUCCESS(status)) {
ZydisFormatter formatter;
ZydisFormatterInit(&formatter, ZYDIS_FORMATTER_STYLE_INTEL);