From 02d0d84c68fd9a97402a251e319c2a8c9a451bf8 Mon Sep 17 00:00:00 2001 From: flobernd Date: Thu, 21 May 2015 21:42:06 +0200 Subject: [PATCH] Minor bugfixes --- CMakeLists.txt | 8 ++-- Zydis/{Zydis.rc => VersionInfo.rc} | Bin Zydis/ZydisInstructionDecoder.hpp | 55 ++++++++++++++-------------- Zydis/ZydisInstructionFormatter.cpp | 38 +++++++++---------- Zydis/ZydisOpcodeTable.hpp | 10 ++--- Zydis/ZydisUtils.cpp | 14 +++---- 6 files changed, 63 insertions(+), 62 deletions(-) rename Zydis/{Zydis.rc => VersionInfo.rc} (100%) diff --git a/CMakeLists.txt b/CMakeLists.txt index b1c6b2e..03cf2c0 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -58,10 +58,10 @@ set(sources "Zydis/ZydisSymbolResolver.cpp" "Zydis/ZydisUtils.cpp") - if (BUILD_SHARED_LIBS AND WIN32) - set(sources ${sources} - "Zydis/Zydis.rc") - endif () +if (BUILD_SHARED_LIBS AND WIN32) + set(sources ${sources} + "Zydis/VersionInfo.rc") +endif () if (BUILD_C_BINDINGS) set(headers ${headers} diff --git a/Zydis/Zydis.rc b/Zydis/VersionInfo.rc similarity index 100% rename from Zydis/Zydis.rc rename to Zydis/VersionInfo.rc diff --git a/Zydis/ZydisInstructionDecoder.hpp b/Zydis/ZydisInstructionDecoder.hpp index 84f9355..19a1d07 100644 --- a/Zydis/ZydisInstructionDecoder.hpp +++ b/Zydis/ZydisInstructionDecoder.hpp @@ -45,34 +45,10 @@ namespace Zydis */ class BaseInput { +friend class InstructionDecoder; private: uint8_t m_currentInput; -protected: - /** - * @brief Override this method in your custom data source implementations. - * Reads the next byte from the data source. This method increases the current - * input position by one. - * @return The current input byte. - */ - virtual uint8_t internalInputPeek() = 0; - /** - * @brief Override this method in your custom data source implementations. - * Reads the next byte from the data source. This method does NOT increase the - * current input position. - * @return The current input byte. - */ - virtual uint8_t internalInputNext() = 0; -protected: - /** - * @brief Default constructor. - */ - BaseInput() { }; -public: - /** - * @brief Destructor. - */ - virtual ~BaseInput() { }; -public: +private: /** * @brief Reads the next byte from the data source. This method does NOT increase the * current input position or the @c length field of the @c info parameter. @@ -111,6 +87,31 @@ public: * @return The current input byte. */ uint8_t inputCurrent() const; +protected: + /** + * @brief Override this method in your custom data source implementations. + * Reads the next byte from the data source. This method increases the current + * input position by one. + * @return The current input byte. + */ + virtual uint8_t internalInputPeek() = 0; + /** + * @brief Override this method in your custom data source implementations. + * Reads the next byte from the data source. This method does NOT increase the + * current input position. + * @return The current input byte. + */ + virtual uint8_t internalInputNext() = 0; +protected: + /** + * @brief Default constructor. + */ + BaseInput() { }; +public: + /** + * @brief Destructor. + */ + virtual ~BaseInput() { }; public: /** * @brief Override this method in your custom data source implementations. @@ -175,7 +176,7 @@ inline T BaseInput::inputNext(InstructionInfo& info) for (unsigned i = 0; i < (sizeof(T) / sizeof(uint8_t)); ++i) { T b = inputNext(info); - if (!b&& (info.flags& IF_ERROR_MASK)) + if (!b && (info.flags & IF_ERROR_MASK)) { return 0; } diff --git a/Zydis/ZydisInstructionFormatter.cpp b/Zydis/ZydisInstructionFormatter.cpp index 5866609..fd48e07 100644 --- a/Zydis/ZydisInstructionFormatter.cpp +++ b/Zydis/ZydisInstructionFormatter.cpp @@ -39,7 +39,7 @@ namespace Zydis /* BaseInstructionFormatter ================================================================ */ -const char *BaseInstructionFormatter::m_registerStrings[] = +const char* BaseInstructionFormatter::m_registerStrings[] = { /* 8 bit general purpose registers */ "al", "cl", "dl", "bl", @@ -237,13 +237,13 @@ void BaseInstructionFormatter::outputAppendAddress(const InstructionInfo& info, } } else { - if (info.flags& IF_DISASSEMBLER_MODE_16) + if (info.flags & IF_DISASSEMBLER_MODE_16) { outputAppendFormatted("%.4X", address); - } else if (info.flags& IF_DISASSEMBLER_MODE_32) + } else if (info.flags & IF_DISASSEMBLER_MODE_32) { outputAppendFormatted("%.8lX", address); - } else if (info.flags& IF_DISASSEMBLER_MODE_64) + } else if (info.flags & IF_DISASSEMBLER_MODE_64) { outputAppendFormatted("%.16llX", address); } else @@ -293,7 +293,7 @@ void BaseInstructionFormatter::outputAppendImmediate(const InstructionInfo& info } } uint64_t offset = 0; - const char *name = nullptr; + const char* name = nullptr; if (resolveSymbols) { name = resolveSymbol(info, value, offset); @@ -316,7 +316,7 @@ void BaseInstructionFormatter::outputAppendImmediate(const InstructionInfo& info void BaseInstructionFormatter::outputAppendDisplacement(const OperandInfo& operand) { assert(operand.offset > 0); - if ((operand.base == Register::NONE)&& (operand.index == Register::NONE)) + if ((operand.base == Register::NONE) && (operand.index == Register::NONE)) { // Assume the displacement value is unsigned assert(operand.scale == 0); @@ -408,7 +408,7 @@ void IntelInstructionFormatter::formatOperand(const InstructionInfo& info, outputAppend(registerToString(operand.base)); break; case OperandType::MEMORY: - if (info.flags& IF_PREFIX_SEGMENT) + if (info.flags & IF_PREFIX_SEGMENT) { outputAppendFormatted("%s:", registerToString(info.segment)); } @@ -480,14 +480,14 @@ void IntelInstructionFormatter::formatOperand(const InstructionInfo& info, void IntelInstructionFormatter::internalFormatInstruction(const InstructionInfo& info) { // Append string prefixes - if (info.flags& IF_PREFIX_LOCK) + if (info.flags & IF_PREFIX_LOCK) { outputAppend("lock "); } - if (info.flags& IF_PREFIX_REP) + if (info.flags & IF_PREFIX_REP) { outputAppend("rep "); - } else if (info.flags& IF_PREFIX_REPNE) + } else if (info.flags & IF_PREFIX_REPNE) { outputAppend("repne "); } @@ -506,7 +506,7 @@ void IntelInstructionFormatter::internalFormatInstruction(const InstructionInfo& (info.operand[0].size != info.operand[1].size)) { cast = true; - } else if (info.operand[1].type == OperandType::REGISTER&& + } else if (info.operand[1].type == OperandType::REGISTER && info.operand[1].base == Register::CL) { switch (info.mnemonic) @@ -536,14 +536,14 @@ void IntelInstructionFormatter::internalFormatInstruction(const InstructionInfo& { outputAppend(", "); bool cast = false; - if (info.operand[1].type == OperandType::MEMORY&& - info.operand[0].size != info.operand[1].size&& + if (info.operand[1].type == OperandType::MEMORY && + info.operand[0].size != info.operand[1].size && ((info.operand[0].type != OperandType::REGISTER) || - ((info.operand[0].base != Register::ES)&& - (info.operand[0].base != Register::CS)&& - (info.operand[0].base != Register::SS)&& - (info.operand[0].base != Register::DS)&& - (info.operand[0].base != Register::FS)&& + ((info.operand[0].base != Register::ES) && + (info.operand[0].base != Register::CS) && + (info.operand[0].base != Register::SS) && + (info.operand[0].base != Register::DS) && + (info.operand[0].base != Register::FS) && (info.operand[0].base != Register::GS)))) { cast = true; @@ -559,7 +559,7 @@ void IntelInstructionFormatter::internalFormatInstruction(const InstructionInfo& { outputAppend(", "); bool cast = false; - if (info.operand[2].type == OperandType::MEMORY&& + if (info.operand[2].type == OperandType::MEMORY && (info.operand[2].size != info.operand[1].size)) { cast = true; diff --git a/Zydis/ZydisOpcodeTable.hpp b/Zydis/ZydisOpcodeTable.hpp index 1b1d14a..ae1d101 100644 --- a/Zydis/ZydisOpcodeTable.hpp +++ b/Zydis/ZydisOpcodeTable.hpp @@ -1607,7 +1607,7 @@ extern const char* instrMnemonicStrings[]; */ inline OpcodeTreeNodeType GetOpcodeNodeType(OpcodeTreeNode node) { - return static_cast((node >> 12)& 0x0F); + return static_cast((node >> 12) & 0x0F); } /** @@ -1617,7 +1617,7 @@ inline OpcodeTreeNodeType GetOpcodeNodeType(OpcodeTreeNode node) */ inline uint16_t GetOpcodeNodeValue(OpcodeTreeNode node) { - return (node& 0x0FFF); + return (node & 0x0FFF); } /** @@ -1698,7 +1698,7 @@ inline OpcodeTreeNode GetOpcodeTreeChild(OpcodeTreeNode parent, uint16_t index) inline const InstructionDefinition* GetInstructionDefinition(OpcodeTreeNode node) { assert(GetOpcodeNodeType(node) == OpcodeTreeNodeType::INSTRUCTION_DEFINITION); - return& instrDefinitions[node& 0x0FFF]; + return& instrDefinitions[node & 0x0FFF]; } /** @@ -1735,7 +1735,7 @@ inline uint16_t GetSimpleOperandSize(DefinedOperandSize operandSize) */ inline DefinedOperandSize GetComplexOperandMemSize(DefinedOperandSize operandSize) { - return static_cast(static_cast(operandSize)& 0x0F); + return static_cast(static_cast(operandSize) & 0x0F); } /** @@ -1745,7 +1745,7 @@ inline DefinedOperandSize GetComplexOperandMemSize(DefinedOperandSize operandSiz */ inline DefinedOperandSize GetComplexOperandRegSize(DefinedOperandSize operandSize) { - return static_cast((static_cast(operandSize) >> 4)& 0x0F); + return static_cast((static_cast(operandSize) >> 4) & 0x0F); } } diff --git a/Zydis/ZydisUtils.cpp b/Zydis/ZydisUtils.cpp index 85d6c86..a451a47 100644 --- a/Zydis/ZydisUtils.cpp +++ b/Zydis/ZydisUtils.cpp @@ -37,33 +37,33 @@ namespace Zydis uint64_t CalcAbsoluteTarget(const InstructionInfo& info, const OperandInfo& operand) { assert((operand.type == OperandType::REL_IMMEDIATE) || - ((operand.type == OperandType::MEMORY)&& (operand.base == Register::RIP))); + ((operand.type == OperandType::MEMORY) && (operand.base == Register::RIP))); uint64_t truncMask = 0xFFFFFFFFFFFFFFFFull; - if (!(info.flags& IF_DISASSEMBLER_MODE_64)) + if (!(info.flags & IF_DISASSEMBLER_MODE_64)) { truncMask >>= (64 - info.operand_mode); } uint16_t size = operand.size; - if ((operand.type == OperandType::MEMORY)&& (operand.base == Register::RIP)) + if ((operand.type == OperandType::MEMORY) && (operand.base == Register::RIP)) { size = operand.offset; } switch (size) { case 8: - return (info.instrPointer + operand.lval.sbyte)& truncMask; + return (info.instrPointer + operand.lval.sbyte) & truncMask; case 16: { - uint32_t delta = operand.lval.sword& truncMask; + uint32_t delta = operand.lval.sword & truncMask; if ((info.instrPointer + delta) > 0xFFFF) { - return (info.instrPointer& 0xF0000) + ((info.instrPointer + delta)& 0xFFFF); + return (info.instrPointer& 0xF0000) + ((info.instrPointer + delta) & 0xFFFF); } return info.instrPointer + delta; } case 32: - return (info.instrPointer + operand.lval.sdword)& truncMask; + return (info.instrPointer + operand.lval.sdword) & truncMask; default: assert(0); }