Added encoder stub, made decoder input const

This commit is contained in:
Joel Höner 2017-01-12 15:12:09 +01:00
parent 67231ccdff
commit 3d2365b6ed
5 changed files with 146 additions and 2 deletions

View File

@ -49,6 +49,7 @@ endif ()
# Library # Library
set(headers set(headers
"include/Zydis/Decoder.h" "include/Zydis/Decoder.h"
"include/Zydis/Encoder.h"
"include/Zydis/Defines.h" "include/Zydis/Defines.h"
"include/Zydis/Formatter.h" "include/Zydis/Formatter.h"
"include/Zydis/InstructionInfo.h" "include/Zydis/InstructionInfo.h"
@ -61,6 +62,7 @@ set(headers
"include/Zydis/Internal/InstructionTable.h") "include/Zydis/Internal/InstructionTable.h")
set(sources set(sources
"src/Decoder.c" "src/Decoder.c"
"src/Encoder.c"
"src/Formatter.c" "src/Formatter.c"
"src/InstructionTable.c" "src/InstructionTable.c"
"src/Mnemonic.c" "src/Mnemonic.c"

View File

@ -76,7 +76,7 @@ typedef struct ZydisInstructionDecoder_
*/ */
struct struct
{ {
uint8_t* buffer; const uint8_t* buffer;
size_t bufferLen; size_t bufferLen;
} input; } input;
// TODO: (Maybe) remove from this struct and pass as argument // TODO: (Maybe) remove from this struct and pass as argument

90
include/Zydis/Encoder.h Normal file
View File

@ -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 <Zydis/Defines.h>
#include <Zydis/Types.h>
#include <Zydis/Status.h>
#include <Zydis/InstructionInfo.h>
#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 */

View File

@ -2158,7 +2158,7 @@ static ZydisStatus ZydisDecodeOpcode(ZydisInstructionDecoder* decoder,
if (info->encoding == ZYDIS_INSTRUCTION_ENCODING_3DNOW) if (info->encoding == ZYDIS_INSTRUCTION_ENCODING_3DNOW)
{ {
// Save input-buffer state and decode dummy operands // 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; size_t bufferLen = decoder->input.bufferLen;
uint8_t length = info->length; uint8_t length = info->length;
ZYDIS_ASSERT(operandCount == 2); ZYDIS_ASSERT(operandCount == 2);

52
src/Encoder.c Normal file
View File

@ -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 <Zydis/Encoder.h>
/* ============================================================================================== */
/* 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;
}
/* ============================================================================================== */