Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ZydisEncoderOperand similar to ZydisDecoderOperand #544

Open
Nitr0-G opened this issue Dec 17, 2024 · 1 comment
Open

ZydisEncoderOperand similar to ZydisDecoderOperand #544

Nitr0-G opened this issue Dec 17, 2024 · 1 comment

Comments

@Nitr0-G
Copy link

Nitr0-G commented Dec 17, 2024

Hello to all!
Is it possible to make ZydisEncoderOperand and ZydisDecodedOperand as compatible as possible in next releases?

auto& PdEncoder = *(Pd->m_PEncoder).at(EncoderIndex);
for (size_t InstrIndex = 0; InstrIndex < PdEncoder.m_svZInstructions.size(); ++InstrIndex)
{
	auto& ZOperands = PdEncoder.m_svZOperands.at(InstrIndex);
	auto& ZInstruction = PdEncoder.m_svZInstructions.at(InstrIndex);
	auto& MemoryAddress = PdEncoder.m_svMemoryAddresses.at(InstrIndex);

	static const uint8_t NOP = 0x90;
	memset(MemoryAddress, NOP, ZInstruction.length);
	if (ZInstruction.mnemonic == ZYDIS_MNEMONIC_MOV)
	{
		ZydisEncoderRequest PushTranslate;
		memset(&PushTranslate, 0, sizeof(PushTranslate));

		PushTranslate.mnemonic = ZYDIS_MNEMONIC_PUSH;
		PushTranslate.machine_mode = ZYDIS_MACHINE_MODE_LONG_64;
		PushTranslate.operand_count = 1;
		PushTranslate.operands[0].type = ZOperands[1].type;
		switch (PushTranslate.operands[0].type)
		{
		case ZYDIS_OPERAND_TYPE_REGISTER:
			PushTranslate.operands[0].reg.value = ZOperands[1].reg.value;
			break;
		case ZYDIS_OPERAND_TYPE_MEMORY:
			PushTranslate.operands[0].mem.base = ZOperands[1].mem.base;
			PushTranslate.operands[0].mem.displacement = ZOperands[1].mem.disp.value;
			PushTranslate.operands[0].mem.index = ZOperands[1].mem.index;
			PushTranslate.operands[0].mem.scale = ZOperands[1].mem.scale;
			PushTranslate.operands[0].mem.size = ZOperands[1].size;
			break;
		case ZYDIS_OPERAND_TYPE_POINTER:
			PushTranslate.operands[0].ptr.offset = ZOperands[1].ptr.offset;
			PushTranslate.operands[0].ptr.segment = ZOperands[1].ptr.segment;
			break;
		case ZYDIS_OPERAND_TYPE_IMMEDIATE:
			PushTranslate.operands[0].imm.u = ZOperands[1].imm.value.u;
			break;
		}

		ZyanU8 EncodedInstruction[ZYDIS_MAX_INSTRUCTION_LENGTH];
		ZyanUSize EncodedLength = sizeof(EncodedInstruction);

		if (ZYAN_SUCCESS(ZydisEncoderEncodeInstruction(&PushTranslate, EncodedInstruction, &EncodedLength)))
		{
			if (EncodedLength > ZInstruction.length) {
				throw std::runtime_error("EncodedLength > ZInstruction.length");
			}

			memcpy(MemoryAddress, EncodedInstruction, EncodedLength);
		}
	}
}

It would be great to have an auxiliary function or to do just that: PushTranslate.operands[0] = ZOperands[1]

@ZehMatt
Copy link
Contributor

ZehMatt commented Dec 17, 2024

There is ZydisEncoderDecodedInstructionToEncoderRequest

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants