diff --git a/CMakeLists.txt b/CMakeLists.txt index 65f7e18..4dbe067 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -49,6 +49,7 @@ endif () # Library set(headers "include/Zydis/Decoder.h" + "include/Zydis/Encoder.h" "include/Zydis/Defines.h" "include/Zydis/Formatter.h" "include/Zydis/InstructionInfo.h" @@ -61,6 +62,7 @@ set(headers "include/Zydis/Internal/InstructionTable.h") set(sources "src/Decoder.c" + "src/Encoder.c" "src/Formatter.c" "src/InstructionTable.c" "src/Mnemonic.c" diff --git a/include/Zydis/Decoder.h b/include/Zydis/Decoder.h index 4eb729a..af60de4 100644 --- a/include/Zydis/Decoder.h +++ b/include/Zydis/Decoder.h @@ -76,7 +76,7 @@ typedef struct ZydisInstructionDecoder_ */ struct { - uint8_t* buffer; + const uint8_t* buffer; size_t bufferLen; } input; // TODO: (Maybe) remove from this struct and pass as argument diff --git a/include/Zydis/Encoder.h b/include/Zydis/Encoder.h new file mode 100644 index 0000000..a536c4a --- /dev/null +++ b/include/Zydis/Encoder.h @@ -0,0 +1,90 @@ +/*************************************************************************************************** + + Zyan Disassembler Library (Zydis) + + Original Author : Joel Höner + + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in all + * copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE + * SOFTWARE. + +***************************************************************************************************/ + +#ifndef ZYDIS_ENCODER_H +#define ZYDIS_ENCODER_H + +#include +#include +#include +#include + +#ifdef __cplusplus +extern "C" { +#endif + +/* ============================================================================================== */ +/* Types */ +/* ============================================================================================== */ + +/** + * @brief Defines the @c ZydisInstructionEncoder struct. + * + * All fields in this struct are private and may change anytime. + */ +typedef struct ZydisInstructionEncoder_ +{ + /** + * @brief The output buffer. + */ + struct + { + uint8_t* buffer; + size_t bufferLen; + } output; +} ZydisInstructionEncoder; + +/* ============================================================================================== */ +/* Exported functions */ +/* ============================================================================================== */ + +/** + * @brief Initializes the given @c ZydisInstructionEncoder instance. + * @param decoder A pointer to the @c ZydisInstructionEncoder instance. + * @return A zydis status code. + */ +ZYDIS_EXPORT ZydisStatus ZydisEncoderInitInstructionEncoder(ZydisInstructionEncoder* encoder); + +/** + * @brief Encodes the given instruction info to byte-code. + * + * @param encoder A pointer to the @c ZydisInstructionEncoder instance. + * @param buffer A pointer to the output buffer. + * @param bufferLen The length of the output buffer. + * @param info A pointer to the @c ZydisInstructionInfo struct to be encoded. + * + * @return A zydis status code. + */ +ZYDIS_EXPORT ZydisStatus ZydisEncoderEncodeInstruction(ZydisInstructionEncoder* encoder, + const void* buffer, size_t bufferLen, ZydisInstructionInfo* info); + +/* ============================================================================================== */ + +#ifdef __cplusplus +} +#endif + +#endif /* ZYDIS_ENCODER_H */ diff --git a/src/Decoder.c b/src/Decoder.c index bd9a747..e60c9e1 100644 --- a/src/Decoder.c +++ b/src/Decoder.c @@ -2158,7 +2158,7 @@ static ZydisStatus ZydisDecodeOpcode(ZydisInstructionDecoder* decoder, if (info->encoding == ZYDIS_INSTRUCTION_ENCODING_3DNOW) { // Save input-buffer state and decode dummy operands - uint8_t* buffer = decoder->input.buffer; + const uint8_t* buffer = decoder->input.buffer; size_t bufferLen = decoder->input.bufferLen; uint8_t length = info->length; ZYDIS_ASSERT(operandCount == 2); diff --git a/src/Encoder.c b/src/Encoder.c new file mode 100644 index 0000000..aa62bf9 --- /dev/null +++ b/src/Encoder.c @@ -0,0 +1,52 @@ +/*************************************************************************************************** + + Zyan Disassembler Library (Zydis) + + Original Author : Joel Höner + + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in all + * copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE + * SOFTWARE. + +***************************************************************************************************/ + +#include + +/* ============================================================================================== */ +/* Implementation of public functions */ +/* ============================================================================================== */ + +ZydisStatus ZydisEncoderInitInstructionEncoder(ZydisInstructionEncoder* encoder) +{ + // Nothing yet. + (void)encoder; + return ZYDIS_STATUS_SUCCESS; +} + +ZydisStatus ZydisEncoderEncodeInstruction(ZydisInstructionEncoder* encoder, + void* buffer, size_t bufferLen, ZydisInstructionInfo* info) +{ + if (!encoder || !info) return ZYDIS_STATUS_INVALID_PARAMETER; + if (!buffer || !bufferLen) return ZYDIS_STATUS_INSUFFICIENT_BUFFER_SIZE; + + encoder->output.buffer = (uint8_t*)buffer; + encoder->output.bufferLen = bufferLen; + + return ZYDIS_STATUS_SUCCESS; +} + +/* ============================================================================================== */