diff --git a/include/zasm/core/errors.hpp b/include/zasm/core/errors.hpp index 6d3b1dbf..3462e781 100644 --- a/include/zasm/core/errors.hpp +++ b/include/zasm/core/errors.hpp @@ -33,6 +33,7 @@ namespace zasm // Decoder. InvalidInstruction, OutOfBounds, + InstructionTooLong, // Encoder. ImpossibleInstruction, AddressOutOfRange, diff --git a/include/zasm/decoder/decoder.hpp b/include/zasm/decoder/decoder.hpp index 6b626497..1ef84c76 100644 --- a/include/zasm/decoder/decoder.hpp +++ b/include/zasm/decoder/decoder.hpp @@ -18,6 +18,10 @@ namespace zasm Result decode(const void* data, std::size_t len, std::uint64_t address) noexcept; + MachineMode getMode() const; + + const Error& getLastError() const; + private: ZydisDecoder _decoder{}; MachineMode _mode{}; diff --git a/src/zasm/src/decoder/decoder.cpp b/src/zasm/src/decoder/decoder.cpp index 3dbaab0d..01411977 100644 --- a/src/zasm/src/decoder/decoder.cpp +++ b/src/zasm/src/decoder/decoder.cpp @@ -109,10 +109,19 @@ namespace zasm break; } - if (status != ZYAN_STATUS_SUCCESS) + switch (status) { - // TODO: Translate proper error. - _status = Error{ ErrorCode::NotInitialized }; + case ZYAN_STATUS_SUCCESS: + break; + case ZYAN_STATUS_INVALID_ARGUMENT: + _status = Error{ ErrorCode::InvalidParameter }; + break; + case ZYAN_STATUS_INVALID_OPERATION: + _status = Error{ ErrorCode::InvalidOperation }; + break; + default: + _status = Error{ ErrorCode::NotInitialized }; + break; } } @@ -169,8 +178,20 @@ namespace zasm ZyanStatus status = ZydisDecoderDecodeFull(&_decoder, data, len, &instr, instrOps.data()); if (status != ZYAN_STATUS_SUCCESS) { - // TODO: Translate proper error. - return zasm::makeUnexpected(Error{ ErrorCode::InvalidOperation }); + switch (status) + { + case ZYDIS_STATUS_NO_MORE_DATA: + _status = Error{ ErrorCode::OutOfBounds }; + break; + case ZYDIS_STATUS_INSTRUCTION_TOO_LONG: + _status = Error{ ErrorCode::InstructionTooLong }; + break; + default: + _status = Error{ ErrorCode::InvalidInstruction }; + break; + } + + return zasm::makeUnexpected(_status); } InstructionDetail::CPUFlags flags{}; @@ -207,4 +228,14 @@ namespace zasm return res; } + MachineMode Decoder::getMode() const + { + return _mode; + } + + const Error& Decoder::getLastError() const + { + return _status; + } + } // namespace zasm