Reworked `ZydisString`

This commit is contained in:
flobernd 2017-12-02 23:46:05 +01:00 committed by Joel Höner
parent 346b7fec10
commit 39facdf5fe
8 changed files with 1877 additions and 1795 deletions

View File

@ -1,103 +0,0 @@
/***************************************************************************************************
Zyan Disassembler Library (Zydis)
Original Author : Florian Bernd
* 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_INTERNAL_INTERNALTYPES_H
#define ZYDIS_INTERNAL_INTERNALTYPES_H
#include <Zydis/CommonTypes.h>
#include <Zydis/Defines.h>
#ifdef __cplusplus
extern "C" {
#endif
/* ============================================================================================== */
/* Generated Strings */
/* ============================================================================================== */
/* ---------------------------------------------------------------------------------------------- */
/* Enums and types */
/* ---------------------------------------------------------------------------------------------- */
#pragma pack(push, 1)
/**
* @brief Defines the `ZydisGeneratedString` struct.
*/
typedef struct ZydisGeneratedString_
{
/**
* @brief Contains the actual string.
*/
char* buffer;
/**
* @brief The length of the string (without 0-termination).
*/
ZydisU8 length;
} ZydisGeneratedString;
#pragma pack(pop)
/* ---------------------------------------------------------------------------------------------- */
/* Helper macros */
/* ---------------------------------------------------------------------------------------------- */
/**
* @brief Creates a `ZydisGeneratedString` from a static C-string.
*/
#define ZYDIS_MAKE_GENERATED_STRING(string) \
{ string, sizeof(string) - 1 }
/* ---------------------------------------------------------------------------------------------- */
/* Helper functions */
/* ---------------------------------------------------------------------------------------------- */
/**
* @brief Initializes a `ZydisString` struct with the values from a `ZydisGeneratedString`.
*
* @param target A pointer to the target string.
* @param source A pointer to the source string.
*/
ZYDIS_NO_EXPORT ZYDIS_INLINE void ZydisStringInitWithGeneratedString(ZydisString* target,
const ZydisGeneratedString* source)
{
ZYDIS_ASSERT(target);
ZYDIS_ASSERT(source);
target->buffer = source->buffer;
target->length = source->length;
target->capacity = source->length;
}
/* ---------------------------------------------------------------------------------------------- */
/* ============================================================================================== */
#ifdef __cplusplus
}
#endif
#endif /* ZYDIS_INTERNAL_INTERNALTYPES_H */

View File

@ -33,7 +33,6 @@
#define ZYDIS_MNEMONIC_H #define ZYDIS_MNEMONIC_H
#include <Zydis/CommonTypes.h> #include <Zydis/CommonTypes.h>
#include <Zydis/Defines.h>
#include <Zydis/String.h> #include <Zydis/String.h>
#ifdef __cplusplus #ifdef __cplusplus
@ -60,7 +59,7 @@ extern "C" {
ZYDIS_EXPORT const char* ZydisMnemonicGetString(ZydisMnemonic mnemonic); ZYDIS_EXPORT const char* ZydisMnemonicGetString(ZydisMnemonic mnemonic);
/** /**
* @brief Returns the specified instruction mnemonic as `ZydisString`. * @brief Returns the specified instruction mnemonic as `ZydisStaticString`.
* *
* @param mnemonic The mnemonic. * @param mnemonic The mnemonic.
* *
@ -68,7 +67,7 @@ ZYDIS_EXPORT const char* ZydisMnemonicGetString(ZydisMnemonic mnemonic);
* *
* The `buffer` of the returned struct is guaranteed to be zero-terminated in this special case. * The `buffer` of the returned struct is guaranteed to be zero-terminated in this special case.
*/ */
ZYDIS_EXPORT const ZydisString* ZydisMnemonicGetStringEx(ZydisMnemonic mnemonic); ZYDIS_EXPORT const ZydisStaticString* ZydisMnemonicGetStaticString(ZydisMnemonic mnemonic);
/* ============================================================================================== */ /* ============================================================================================== */

View File

@ -34,7 +34,7 @@
#include <Zydis/Defines.h> #include <Zydis/Defines.h>
#include <Zydis/CommonTypes.h> #include <Zydis/CommonTypes.h>
#include <Zydis/Status.h> #include <Zydis/String.h>
#ifdef __cplusplus #ifdef __cplusplus
extern "C" { extern "C" {
@ -314,6 +314,17 @@ ZYDIS_EXPORT ZydisRegisterWidth ZydisRegisterGetWidth64(ZydisRegister reg);
*/ */
ZYDIS_EXPORT const char* ZydisRegisterGetString(ZydisRegister reg); ZYDIS_EXPORT const char* ZydisRegisterGetString(ZydisRegister reg);
/**
* @brief Returns the specified register string as `ZydisStaticString`.
*
* @param reg The register.
*
* @return The register string or @c NULL, if an invalid register was passed.
*
* The `buffer` of the returned struct is guaranteed to be zero-terminated in this special case.
*/
ZYDIS_EXPORT const ZydisStaticString* ZydisRegisterGetStaticString(ZydisRegister reg);
/* ============================================================================================== */ /* ============================================================================================== */
#ifdef __cplusplus #ifdef __cplusplus

View File

@ -49,7 +49,7 @@ typedef struct ZydisString_
*/ */
char *buffer; char *buffer;
/** /**
* @brief The length of the string (without any optional 0). * @brief The length of the string (without 0-termination).
*/ */
ZydisUSize length; ZydisUSize length;
/** /**
@ -58,6 +58,31 @@ typedef struct ZydisString_
ZydisUSize capacity; ZydisUSize capacity;
} ZydisString; } ZydisString;
/* ---------------------------------------------------------------------------------------------- */
/* Static string */
/* ---------------------------------------------------------------------------------------------- */
#pragma pack(push, 1)
/**
* @brief Defines the `ZydisStaticString` struct.
*
* This more compact struct is mainly used for internal string-tables to save up some bytes.
*/
typedef struct ZydisStaticString_
{
/**
* @brief The buffer that contains the actual string (0-termination is optional!).
*/
const char* buffer;
/**
* @brief The length of the string (without 0-termination).
*/
ZydisU8 length;
} ZydisStaticString;
#pragma pack(pop)
/* ---------------------------------------------------------------------------------------------- */ /* ---------------------------------------------------------------------------------------------- */
/* Letter Case */ /* Letter Case */
/* ---------------------------------------------------------------------------------------------- */ /* ---------------------------------------------------------------------------------------------- */
@ -109,6 +134,14 @@ enum ZydisLetterCases
#define ZYDIS_MAKE_STRING(string) \ #define ZYDIS_MAKE_STRING(string) \
{ (char*)string, sizeof(string) - 1, sizeof(string) - 1 } { (char*)string, sizeof(string) - 1, sizeof(string) - 1 }
/**
* @brief Creates a `ZydisStaticString` from a static C-string.
*
* @param string The C-string constant.
*/
#define ZYDIS_MAKE_STATIC_STRING(string) \
{ string, sizeof(string) - 1 }
/* ---------------------------------------------------------------------------------------------- */ /* ---------------------------------------------------------------------------------------------- */
/* ============================================================================================== */ /* ============================================================================================== */
@ -134,7 +167,7 @@ ZYDIS_NO_EXPORT ZYDIS_INLINE ZydisStatus ZydisStringInit(ZydisString* string, ch
return ZYDIS_STATUS_INVALID_PARAMETER; return ZYDIS_STATUS_INVALID_PARAMETER;
} }
ZydisUSize length = ZydisStrLen(value); const ZydisUSize length = ZydisStrLen(value);
string->buffer = value; string->buffer = value;
string->length = length; string->length = length;
string->capacity = length; string->capacity = length;
@ -142,6 +175,28 @@ ZYDIS_NO_EXPORT ZYDIS_INLINE ZydisStatus ZydisStringInit(ZydisString* string, ch
return ZYDIS_STATUS_SUCCESS; return ZYDIS_STATUS_SUCCESS;
} }
/**
* @brief Finalizes a `ZydisString` struct by adding a terminating zero byte.
*
* @param string The string to finalize.
*
* @return A zydis status code.
*/
ZYDIS_NO_EXPORT ZYDIS_INLINE ZydisStatus ZydisStringFinalize(ZydisString* string)
{
if (!string)
{
return ZYDIS_STATUS_INVALID_PARAMETER;
}
if (string->length >= string->capacity)
{
return ZYDIS_STATUS_INSUFFICIENT_BUFFER_SIZE;
}
string->buffer[string->length] = 0;
return ZYDIS_STATUS_SUCCESS;
}
/* ---------------------------------------------------------------------------------------------- */ /* ---------------------------------------------------------------------------------------------- */
/** /**
@ -180,6 +235,33 @@ ZYDIS_NO_EXPORT ZYDIS_INLINE ZydisStatus ZydisStringAppendExC(ZydisString* strin
return ZydisStringAppendEx(string, &other, letterCase); return ZydisStringAppendEx(string, &other, letterCase);
} }
/**
* @brief Appends the given 'ZydisStaticString' to a `ZydisString`, converting it to the
* specified letter-case.
*
* @param string The string to append to.
* @param text The static-string to append.
* @param letterCase The letter case to use.
*
* @return @c ZYDIS_STATUS_SUCCESS, if the function succeeded, or
* @c ZYDIS_STATUS_INSUFFICIENT_BUFFER_SIZE, if the size of the buffer was not
* sufficient to append the given @c text.
*/
ZYDIS_NO_EXPORT ZYDIS_INLINE ZydisStatus ZydisStringAppendStaticEx(ZydisString* string,
const ZydisStaticString* text, ZydisLetterCase letterCase)
{
if (!text || !text->buffer)
{
return ZYDIS_STATUS_INVALID_PARAMETER;
}
ZydisString other;
other.buffer = (char*)text->buffer;
other.length = text->length;
return ZydisStringAppendEx(string, &other, letterCase);
}
/** /**
* @brief Appends a `ZydisString` to another `ZydisString`. * @brief Appends a `ZydisString` to another `ZydisString`.
* *
@ -214,6 +296,31 @@ ZYDIS_NO_EXPORT ZYDIS_INLINE ZydisStatus ZydisStringAppendC(ZydisString* string,
return ZydisStringAppendEx(string, &other, ZYDIS_LETTER_CASE_DEFAULT); return ZydisStringAppendEx(string, &other, ZYDIS_LETTER_CASE_DEFAULT);
} }
/**
* @brief Appends the given 'ZydisStaticString' to a `ZydisString`.
*
* @param string The string to append to.
* @param text The static-string to append.
*
* @return @c ZYDIS_STATUS_SUCCESS, if the function succeeded, or
* @c ZYDIS_STATUS_INSUFFICIENT_BUFFER_SIZE, if the size of the buffer was not
* sufficient to append the given @c text.
*/
ZYDIS_NO_EXPORT ZYDIS_INLINE ZydisStatus ZydisStringAppendStatic(ZydisString* string,
const ZydisStaticString* text, ZydisLetterCase letterCase)
{
if (!text || !text->buffer)
{
return ZYDIS_STATUS_INVALID_PARAMETER;
}
ZydisString other;
other.buffer = (char*)text->buffer;
other.length = text->length;
return ZydisStringAppendEx(string, &other, letterCase);
}
/* ---------------------------------------------------------------------------------------------- */ /* ---------------------------------------------------------------------------------------------- */
/* Formatting */ /* Formatting */
/* ---------------------------------------------------------------------------------------------- */ /* ---------------------------------------------------------------------------------------------- */

View File

@ -91,12 +91,12 @@ static ZydisStatus ZydisFormatterPrintMnemonicIntel(const ZydisFormatter* format
return ZYDIS_STATUS_INVALID_PARAMETER; return ZYDIS_STATUS_INVALID_PARAMETER;
} }
const ZydisString* mnemonic = ZydisMnemonicGetStringEx(instruction->mnemonic); const ZydisStaticString* mnemonic = ZydisMnemonicGetStaticString(instruction->mnemonic);
if (!mnemonic) if (!mnemonic)
{ {
return ZydisStringAppendExC(string, "invalid", formatter->letterCase); return ZydisStringAppendExC(string, "invalid", formatter->letterCase);
} }
ZYDIS_CHECK(ZydisStringAppendEx(string, mnemonic, formatter->letterCase)); ZYDIS_CHECK(ZydisStringAppendStaticEx(string, mnemonic, formatter->letterCase));
if (instruction->attributes & ZYDIS_ATTRIB_IS_FAR_BRANCH) if (instruction->attributes & ZYDIS_ATTRIB_IS_FAR_BRANCH)
{ {
@ -124,12 +124,12 @@ static ZydisStatus ZydisFormatterFormatOperandRegIntel(const ZydisFormatter* for
return ZYDIS_STATUS_SUCCESS; return ZYDIS_STATUS_SUCCESS;
} }
const char* reg = ZydisRegisterGetString(operand->reg.value); const ZydisStaticString* reg = ZydisRegisterGetStaticString(operand->reg.value);
if (!reg) if (!reg)
{ {
return ZydisStringAppendExC(string, "invalid", formatter->letterCase); return ZydisStringAppendExC(string, "invalid", formatter->letterCase);
} }
return ZydisStringAppendExC(string, reg, formatter->letterCase); return ZydisStringAppendStaticEx(string, reg, formatter->letterCase);
} }
static ZydisStatus ZydisFormatterFormatOperandMemIntel(const ZydisFormatter* formatter, static ZydisStatus ZydisFormatterFormatOperandMemIntel(const ZydisFormatter* formatter,
@ -159,8 +159,8 @@ static ZydisStatus ZydisFormatterFormatOperandMemIntel(const ZydisFormatter* for
address, userData)); address, userData));
} else } else
{ {
ZYDIS_CHECK(ZydisStringAppendExC(string, ZydisRegisterGetString(operand->mem.base), ZYDIS_CHECK(ZydisStringAppendStaticEx(string,
formatter->letterCase)); ZydisRegisterGetStaticString(operand->mem.base), formatter->letterCase));
ZYDIS_CHECK(formatter->funcPrintDisplacement(formatter, string, instruction, operand, ZYDIS_CHECK(formatter->funcPrintDisplacement(formatter, string, instruction, operand,
userData)); userData));
} }
@ -169,17 +169,17 @@ static ZydisStatus ZydisFormatterFormatOperandMemIntel(const ZydisFormatter* for
// Regular memory operand // Regular memory operand
if (operand->mem.base != ZYDIS_REGISTER_NONE) if (operand->mem.base != ZYDIS_REGISTER_NONE)
{ {
const char* reg = ZydisRegisterGetString(operand->mem.base); const ZydisStaticString* reg = ZydisRegisterGetStaticString(operand->mem.base);
if (!reg) if (!reg)
{ {
return ZYDIS_STATUS_INVALID_PARAMETER; return ZYDIS_STATUS_INVALID_PARAMETER;
} }
ZYDIS_CHECK(ZydisStringAppendExC(string, reg, formatter->letterCase)); ZYDIS_CHECK(ZydisStringAppendStaticEx(string, reg, formatter->letterCase));
} }
if ((operand->mem.index != ZYDIS_REGISTER_NONE) && if ((operand->mem.index != ZYDIS_REGISTER_NONE) &&
(operand->mem.type != ZYDIS_MEMOP_TYPE_MIB)) (operand->mem.type != ZYDIS_MEMOP_TYPE_MIB))
{ {
const char* reg = ZydisRegisterGetString(operand->mem.index); const ZydisStaticString* reg = ZydisRegisterGetStaticString(operand->mem.index);
if (!reg) if (!reg)
{ {
return ZYDIS_STATUS_INVALID_PARAMETER; return ZYDIS_STATUS_INVALID_PARAMETER;
@ -188,7 +188,7 @@ static ZydisStatus ZydisFormatterFormatOperandMemIntel(const ZydisFormatter* for
{ {
ZYDIS_CHECK(ZydisStringAppendC(string, "+")); ZYDIS_CHECK(ZydisStringAppendC(string, "+"));
} }
ZYDIS_CHECK(ZydisStringAppendExC(string, reg, formatter->letterCase)); ZYDIS_CHECK(ZydisStringAppendStaticEx(string, reg, formatter->letterCase));
if (operand->mem.scale) if (operand->mem.scale)
{ {
ZYDIS_CHECK(ZydisStringAppendC(string, "*")); ZYDIS_CHECK(ZydisStringAppendC(string, "*"));
@ -523,15 +523,15 @@ static ZydisStatus ZydisFormatterPrintSegmentIntel(const ZydisFormatter* formatt
case ZYDIS_REGISTER_CS: case ZYDIS_REGISTER_CS:
case ZYDIS_REGISTER_FS: case ZYDIS_REGISTER_FS:
case ZYDIS_REGISTER_GS: case ZYDIS_REGISTER_GS:
ZYDIS_CHECK(ZydisStringAppendExC(string, ZYDIS_CHECK(ZydisStringAppendStaticEx(string,
ZydisRegisterGetString(operand->mem.segment), formatter->letterCase)); ZydisRegisterGetStaticString(operand->mem.segment), formatter->letterCase));
return ZydisStringAppendC(string, ":"); return ZydisStringAppendC(string, ":");
case ZYDIS_REGISTER_SS: case ZYDIS_REGISTER_SS:
if ((formatter->forceSegments) || if ((formatter->forceSegments) ||
(instruction->attributes & ZYDIS_ATTRIB_HAS_SEGMENT_SS)) (instruction->attributes & ZYDIS_ATTRIB_HAS_SEGMENT_SS))
{ {
ZYDIS_CHECK(ZydisStringAppendExC(string, ZYDIS_CHECK(ZydisStringAppendStaticEx(string,
ZydisRegisterGetString(operand->mem.segment), formatter->letterCase)); ZydisRegisterGetStaticString(operand->mem.segment), formatter->letterCase));
return ZydisStringAppendC(string, ":"); return ZydisStringAppendC(string, ":");
} }
break; break;
@ -539,8 +539,8 @@ static ZydisStatus ZydisFormatterPrintSegmentIntel(const ZydisFormatter* formatt
if ((formatter->forceSegments) || if ((formatter->forceSegments) ||
(instruction->attributes & ZYDIS_ATTRIB_HAS_SEGMENT_DS)) (instruction->attributes & ZYDIS_ATTRIB_HAS_SEGMENT_DS))
{ {
ZYDIS_CHECK(ZydisStringAppendExC(string, ZYDIS_CHECK(ZydisStringAppendStaticEx(string,
ZydisRegisterGetString(operand->mem.segment), formatter->letterCase)); ZydisRegisterGetStaticString(operand->mem.segment), formatter->letterCase));
return ZydisStringAppendC(string, ":"); return ZydisStringAppendC(string, ":");
} }
break; break;
@ -568,13 +568,13 @@ static ZydisStatus ZydisFormatterPrintDecoratorIntel(const ZydisFormatter* forma
{ {
if (instruction->avx.mask.reg != ZYDIS_REGISTER_K0) if (instruction->avx.mask.reg != ZYDIS_REGISTER_K0)
{ {
const char* reg = ZydisRegisterGetString(instruction->avx.mask.reg); const ZydisStaticString* reg = ZydisRegisterGetStaticString(instruction->avx.mask.reg);
if (!reg) if (!reg)
{ {
return ZYDIS_STATUS_INVALID_PARAMETER; return ZYDIS_STATUS_INVALID_PARAMETER;
} }
ZYDIS_CHECK(ZydisStringAppendC(string, " {")); ZYDIS_CHECK(ZydisStringAppendC(string, " {"));
ZYDIS_CHECK(ZydisStringAppendExC(string, reg, formatter->letterCase)); ZYDIS_CHECK(ZydisStringAppendStaticEx(string, reg, formatter->letterCase));
ZYDIS_CHECK(ZydisStringAppendC(string, "}")); ZYDIS_CHECK(ZydisStringAppendC(string, "}"));
if (instruction->avx.mask.mode == ZYDIS_MASK_MODE_ZERO) if (instruction->avx.mask.mode == ZYDIS_MASK_MODE_ZERO)
{ {
@ -1132,7 +1132,7 @@ ZydisStatus ZydisFormatterFormatInstructionEx(const ZydisFormatter* formatter,
return formatter->funcPost(formatter, &string, instruction, userData); return formatter->funcPost(formatter, &string, instruction, userData);
} }
buffer[string.length] = 0; buffer[string.length] = 0; // TODO: Should we add 0-termination in error case?
return ZYDIS_STATUS_SUCCESS; return ZYDIS_STATUS_SUCCESS;
} }

File diff suppressed because it is too large Load Diff

View File

@ -25,18 +25,8 @@
***************************************************************************************************/ ***************************************************************************************************/
#include <Zydis/Mnemonic.h> #include <Zydis/Mnemonic.h>
#include <Zydis/Internal/InternalTypes.h>
#include <Generated/EnumMnemonic.inc> #include <Generated/EnumMnemonic.inc>
/**
* @brief Caches all strings that were accessed by `ZydisMnemonicGetStringEx`.
*
* We could store `ZydisString` structs instead of `ZydisGeneratedString` ones in the
* `zydisMnemonicStrings` array, but this would significantly increase the table-size.
*/
static ZydisString zydisMnemonicStringCache[ZYDIS_MNEMONIC_MAX_VALUE + 1];
/* ============================================================================================== */ /* ============================================================================================== */
/* Exported functions */ /* Exported functions */
/* ============================================================================================== */ /* ============================================================================================== */
@ -50,18 +40,13 @@ const char* ZydisMnemonicGetString(ZydisMnemonic mnemonic)
return (const char*)zydisMnemonicStrings[mnemonic].buffer; return (const char*)zydisMnemonicStrings[mnemonic].buffer;
} }
const ZydisString* ZydisMnemonicGetStringEx(ZydisMnemonic mnemonic) const ZydisStaticString* ZydisMnemonicGetStaticString(ZydisMnemonic mnemonic)
{ {
if (mnemonic > ZYDIS_ARRAY_SIZE(zydisMnemonicStrings) - 1) if (mnemonic > ZYDIS_ARRAY_SIZE(zydisMnemonicStrings) - 1)
{ {
return ZYDIS_NULL; return ZYDIS_NULL;
} }
if (!zydisMnemonicStringCache[mnemonic].buffer) return &zydisMnemonicStrings[mnemonic];
{
ZydisStringInitWithGeneratedString(&zydisMnemonicStringCache[mnemonic],
&zydisMnemonicStrings[mnemonic]);
}
return &zydisMnemonicStringCache[mnemonic];
} }
/* ============================================================================================== */ /* ============================================================================================== */

View File

@ -30,91 +30,165 @@
/* Register strings */ /* Register strings */
/* ============================================================================================== */ /* ============================================================================================== */
const char* registerStrings[] = static const ZydisStaticString registerStrings[] =
{ {
"none", ZYDIS_MAKE_STATIC_STRING("none"),
// General purpose registers 8-bit // General purpose registers 8-bit
"al", "cl", "dl", "bl", ZYDIS_MAKE_STATIC_STRING("al"), ZYDIS_MAKE_STATIC_STRING("cl"),
"ah", "ch", "dh", "bh", ZYDIS_MAKE_STATIC_STRING("dl"), ZYDIS_MAKE_STATIC_STRING("bl"),
"spl", "bpl", "sil", "dil", ZYDIS_MAKE_STATIC_STRING("ah"), ZYDIS_MAKE_STATIC_STRING("ch"),
"r8b", "r9b", "r10b", "r11b", ZYDIS_MAKE_STATIC_STRING("dh"), ZYDIS_MAKE_STATIC_STRING("bh"),
"r12b", "r13b", "r14b", "r15b", ZYDIS_MAKE_STATIC_STRING("spl"), ZYDIS_MAKE_STATIC_STRING("bpl"),
ZYDIS_MAKE_STATIC_STRING("sil"), ZYDIS_MAKE_STATIC_STRING("dil"),
ZYDIS_MAKE_STATIC_STRING("r8b"), ZYDIS_MAKE_STATIC_STRING("r9b"),
ZYDIS_MAKE_STATIC_STRING("r10b"), ZYDIS_MAKE_STATIC_STRING("r11b"),
ZYDIS_MAKE_STATIC_STRING("r12b"), ZYDIS_MAKE_STATIC_STRING("r13b"),
ZYDIS_MAKE_STATIC_STRING("r14b"), ZYDIS_MAKE_STATIC_STRING("r15b"),
// General purpose registers 16-bit // General purpose registers 16-bit
"ax", "cx", "dx", "bx", ZYDIS_MAKE_STATIC_STRING("ax"), ZYDIS_MAKE_STATIC_STRING("cx"),
"sp", "bp", "si", "di", ZYDIS_MAKE_STATIC_STRING("dx"), ZYDIS_MAKE_STATIC_STRING("bx"),
"r8w", "r9w", "r10w", "r11w", ZYDIS_MAKE_STATIC_STRING("sp"), ZYDIS_MAKE_STATIC_STRING("bp"),
"r12w", "r13w", "r14w", "r15w", ZYDIS_MAKE_STATIC_STRING("si"), ZYDIS_MAKE_STATIC_STRING("di"),
ZYDIS_MAKE_STATIC_STRING("r8d"), ZYDIS_MAKE_STATIC_STRING("r9d"),
ZYDIS_MAKE_STATIC_STRING("r10d"), ZYDIS_MAKE_STATIC_STRING("r11d"),
ZYDIS_MAKE_STATIC_STRING("r12d"), ZYDIS_MAKE_STATIC_STRING("r13d"),
ZYDIS_MAKE_STATIC_STRING("r14d"), ZYDIS_MAKE_STATIC_STRING("r15d"),
// General purpose registers 32-bit // General purpose registers 32-bit
"eax", "ecx", "edx", "ebx", ZYDIS_MAKE_STATIC_STRING("rax"), ZYDIS_MAKE_STATIC_STRING("rcx"),
"esp", "ebp", "esi", "edi", ZYDIS_MAKE_STATIC_STRING("rdx"), ZYDIS_MAKE_STATIC_STRING("rbx"),
"r8d", "r9d", "r10d", "r11d", ZYDIS_MAKE_STATIC_STRING("rsp"), ZYDIS_MAKE_STATIC_STRING("rbp"),
"r12d", "r13d", "r14d", "r15d", ZYDIS_MAKE_STATIC_STRING("rsi"), ZYDIS_MAKE_STATIC_STRING("rdi"),
ZYDIS_MAKE_STATIC_STRING("r8"), ZYDIS_MAKE_STATIC_STRING("r9"),
ZYDIS_MAKE_STATIC_STRING("r10"), ZYDIS_MAKE_STATIC_STRING("r11"),
ZYDIS_MAKE_STATIC_STRING("r12"), ZYDIS_MAKE_STATIC_STRING("r13"),
ZYDIS_MAKE_STATIC_STRING("r14"), ZYDIS_MAKE_STATIC_STRING("r15"),
// General purpose registers 64-bit // General purpose registers 64-bit
"rax", "rcx", "rdx", "rbx", ZYDIS_MAKE_STATIC_STRING("rax"), ZYDIS_MAKE_STATIC_STRING("rcx"),
"rsp", "rbp", "rsi", "rdi", ZYDIS_MAKE_STATIC_STRING("rdx"), ZYDIS_MAKE_STATIC_STRING("rbx"),
"r8", "r9", "r10", "r11", ZYDIS_MAKE_STATIC_STRING("rsp"), ZYDIS_MAKE_STATIC_STRING("rbp"),
"r12", "r13", "r14", "r15", ZYDIS_MAKE_STATIC_STRING("rsi"), ZYDIS_MAKE_STATIC_STRING("rdi"),
ZYDIS_MAKE_STATIC_STRING("r8"), ZYDIS_MAKE_STATIC_STRING("r9"),
ZYDIS_MAKE_STATIC_STRING("r10"), ZYDIS_MAKE_STATIC_STRING("r11"),
ZYDIS_MAKE_STATIC_STRING("r12"), ZYDIS_MAKE_STATIC_STRING("r13"),
ZYDIS_MAKE_STATIC_STRING("r14"), ZYDIS_MAKE_STATIC_STRING("r15"),
// Address-size scaling general purpose pseudo-registers
ZYDIS_MAKE_STATIC_STRING("aszax"), ZYDIS_MAKE_STATIC_STRING("aszcx"),
ZYDIS_MAKE_STATIC_STRING("aszdx"), ZYDIS_MAKE_STATIC_STRING("aszbx"),
ZYDIS_MAKE_STATIC_STRING("aszsp"), ZYDIS_MAKE_STATIC_STRING("aszbp"),
ZYDIS_MAKE_STATIC_STRING("aszsi"), ZYDIS_MAKE_STATIC_STRING("aszdi"),
// Stack-size scaling general purpose pseudo-registers
ZYDIS_MAKE_STATIC_STRING("sszax"), ZYDIS_MAKE_STATIC_STRING("sszcx"),
ZYDIS_MAKE_STATIC_STRING("sszdx"), ZYDIS_MAKE_STATIC_STRING("sszbx"),
ZYDIS_MAKE_STATIC_STRING("sszsp"), ZYDIS_MAKE_STATIC_STRING("sszbp"),
ZYDIS_MAKE_STATIC_STRING("sszsi"), ZYDIS_MAKE_STATIC_STRING("sszdi"),
// Floating point legacy registers // Floating point legacy registers
"st0", "st1", "st2", "st3", ZYDIS_MAKE_STATIC_STRING("st0"), ZYDIS_MAKE_STATIC_STRING("st1"),
"st4", "st5", "st6", "st7", ZYDIS_MAKE_STATIC_STRING("st2"), ZYDIS_MAKE_STATIC_STRING("st3"),
ZYDIS_MAKE_STATIC_STRING("st4"), ZYDIS_MAKE_STATIC_STRING("st5"),
ZYDIS_MAKE_STATIC_STRING("st6"), ZYDIS_MAKE_STATIC_STRING("st7"),
// Floating point multimedia registers // Floating point multimedia registers
"mm0", "mm1", "mm2", "mm3", ZYDIS_MAKE_STATIC_STRING("mm0"), ZYDIS_MAKE_STATIC_STRING("mm1"),
"mm4", "mm5", "mm6", "mm7", ZYDIS_MAKE_STATIC_STRING("mm2"), ZYDIS_MAKE_STATIC_STRING("mm3"),
// Floating point vector registers 512-bit ZYDIS_MAKE_STATIC_STRING("mm4"), ZYDIS_MAKE_STATIC_STRING("mm5"),
"zmm0", "zmm1", "zmm2", "zmm3", ZYDIS_MAKE_STATIC_STRING("mm6"), ZYDIS_MAKE_STATIC_STRING("mm7"),
"zmm4", "zmm5", "zmm6", "zmm7",
"zmm8", "zmm9", "zmm10", "zmm11",
"zmm12", "zmm13", "zmm14", "zmm15",
"zmm16", "zmm17", "zmm18", "zmm19",
"zmm20", "zmm21", "zmm22", "zmm23",
"zmm24", "zmm25", "zmm26", "zmm27",
"zmm28", "zmm29", "zmm30", "zmm31",
// Floating point vector registers 256-bit
"ymm0", "ymm1", "ymm2", "ymm3",
"ymm4", "ymm5", "ymm6", "ymm7",
"ymm8", "ymm9", "ymm10", "ymm11",
"ymm12", "ymm13", "ymm14", "ymm15",
"ymm16", "ymm17", "ymm18", "ymm19",
"ymm20", "ymm21", "ymm22", "ymm23",
"ymm24", "ymm25", "ymm26", "ymm27",
"ymm28", "ymm29", "ymm30", "ymm31",
// Floating point vector registers 128-bit // Floating point vector registers 128-bit
"xmm0", "xmm1", "xmm2", "xmm3", ZYDIS_MAKE_STATIC_STRING("xmm0"), ZYDIS_MAKE_STATIC_STRING("xmm1"),
"xmm4", "xmm5", "xmm6", "xmm7", ZYDIS_MAKE_STATIC_STRING("xmm2"), ZYDIS_MAKE_STATIC_STRING("xmm3"),
"xmm8", "xmm9", "xmm10", "xmm11", ZYDIS_MAKE_STATIC_STRING("xmm4"), ZYDIS_MAKE_STATIC_STRING("xmm5"),
"xmm12", "xmm13", "xmm14", "xmm15", ZYDIS_MAKE_STATIC_STRING("xmm6"), ZYDIS_MAKE_STATIC_STRING("xmm7"),
"xmm16", "xmm17", "xmm18", "xmm19", ZYDIS_MAKE_STATIC_STRING("xmm8"), ZYDIS_MAKE_STATIC_STRING("xmm9"),
"xmm20", "xmm21", "xmm22", "xmm23", ZYDIS_MAKE_STATIC_STRING("xmm10"), ZYDIS_MAKE_STATIC_STRING("xmm11"),
"xmm24", "xmm25", "xmm26", "xmm27", ZYDIS_MAKE_STATIC_STRING("xmm12"), ZYDIS_MAKE_STATIC_STRING("xmm13"),
"xmm28", "xmm29", "xmm30", "xmm31", ZYDIS_MAKE_STATIC_STRING("xmm14"), ZYDIS_MAKE_STATIC_STRING("xmm15"),
// Special registers ZYDIS_MAKE_STATIC_STRING("xmm16"), ZYDIS_MAKE_STATIC_STRING("xmm17"),
"rflags", "eflags", "flags", "rip", ZYDIS_MAKE_STATIC_STRING("xmm18"), ZYDIS_MAKE_STATIC_STRING("xmm19"),
"eip", "ip", "mxcsr", "pkru", ZYDIS_MAKE_STATIC_STRING("xmm20"), ZYDIS_MAKE_STATIC_STRING("xmm21"),
"xcr0", ZYDIS_MAKE_STATIC_STRING("xmm22"), ZYDIS_MAKE_STATIC_STRING("xmm23"),
ZYDIS_MAKE_STATIC_STRING("xmm24"), ZYDIS_MAKE_STATIC_STRING("xmm25"),
ZYDIS_MAKE_STATIC_STRING("xmm26"), ZYDIS_MAKE_STATIC_STRING("xmm27"),
ZYDIS_MAKE_STATIC_STRING("xmm28"), ZYDIS_MAKE_STATIC_STRING("xmm29"),
ZYDIS_MAKE_STATIC_STRING("xmm30"), ZYDIS_MAKE_STATIC_STRING("xmm31"),
// Floating point vector registers 256-bit
ZYDIS_MAKE_STATIC_STRING("ymm0"), ZYDIS_MAKE_STATIC_STRING("ymm1"),
ZYDIS_MAKE_STATIC_STRING("ymm2"), ZYDIS_MAKE_STATIC_STRING("ymm3"),
ZYDIS_MAKE_STATIC_STRING("ymm4"), ZYDIS_MAKE_STATIC_STRING("ymm5"),
ZYDIS_MAKE_STATIC_STRING("ymm6"), ZYDIS_MAKE_STATIC_STRING("ymm7"),
ZYDIS_MAKE_STATIC_STRING("ymm8"), ZYDIS_MAKE_STATIC_STRING("ymm9"),
ZYDIS_MAKE_STATIC_STRING("ymm10"), ZYDIS_MAKE_STATIC_STRING("ymm11"),
ZYDIS_MAKE_STATIC_STRING("ymm12"), ZYDIS_MAKE_STATIC_STRING("ymm13"),
ZYDIS_MAKE_STATIC_STRING("ymm14"), ZYDIS_MAKE_STATIC_STRING("ymm15"),
ZYDIS_MAKE_STATIC_STRING("ymm16"), ZYDIS_MAKE_STATIC_STRING("ymm17"),
ZYDIS_MAKE_STATIC_STRING("ymm18"), ZYDIS_MAKE_STATIC_STRING("ymm19"),
ZYDIS_MAKE_STATIC_STRING("ymm20"), ZYDIS_MAKE_STATIC_STRING("ymm21"),
ZYDIS_MAKE_STATIC_STRING("ymm22"), ZYDIS_MAKE_STATIC_STRING("ymm23"),
ZYDIS_MAKE_STATIC_STRING("ymm24"), ZYDIS_MAKE_STATIC_STRING("ymm25"),
ZYDIS_MAKE_STATIC_STRING("ymm26"), ZYDIS_MAKE_STATIC_STRING("ymm27"),
ZYDIS_MAKE_STATIC_STRING("ymm28"), ZYDIS_MAKE_STATIC_STRING("ymm29"),
ZYDIS_MAKE_STATIC_STRING("ymm30"), ZYDIS_MAKE_STATIC_STRING("ymm31"),
// Floating point vector registers 512-bit
ZYDIS_MAKE_STATIC_STRING("zmm0"), ZYDIS_MAKE_STATIC_STRING("zmm1"),
ZYDIS_MAKE_STATIC_STRING("zmm2"), ZYDIS_MAKE_STATIC_STRING("zmm3"),
ZYDIS_MAKE_STATIC_STRING("zmm4"), ZYDIS_MAKE_STATIC_STRING("zmm5"),
ZYDIS_MAKE_STATIC_STRING("zmm6"), ZYDIS_MAKE_STATIC_STRING("zmm7"),
ZYDIS_MAKE_STATIC_STRING("zmm8"), ZYDIS_MAKE_STATIC_STRING("zmm9"),
ZYDIS_MAKE_STATIC_STRING("zmm10"), ZYDIS_MAKE_STATIC_STRING("zmm11"),
ZYDIS_MAKE_STATIC_STRING("zmm12"), ZYDIS_MAKE_STATIC_STRING("zmm13"),
ZYDIS_MAKE_STATIC_STRING("zmm14"), ZYDIS_MAKE_STATIC_STRING("zmm15"),
ZYDIS_MAKE_STATIC_STRING("zmm16"), ZYDIS_MAKE_STATIC_STRING("zmm17"),
ZYDIS_MAKE_STATIC_STRING("zmm18"), ZYDIS_MAKE_STATIC_STRING("zmm19"),
ZYDIS_MAKE_STATIC_STRING("zmm20"), ZYDIS_MAKE_STATIC_STRING("zmm21"),
ZYDIS_MAKE_STATIC_STRING("zmm22"), ZYDIS_MAKE_STATIC_STRING("zmm23"),
ZYDIS_MAKE_STATIC_STRING("zmm24"), ZYDIS_MAKE_STATIC_STRING("zmm25"),
ZYDIS_MAKE_STATIC_STRING("zmm26"), ZYDIS_MAKE_STATIC_STRING("zmm27"),
ZYDIS_MAKE_STATIC_STRING("zmm28"), ZYDIS_MAKE_STATIC_STRING("zmm29"),
ZYDIS_MAKE_STATIC_STRING("zmm30"), ZYDIS_MAKE_STATIC_STRING("zmm31"),
// Flags registers
ZYDIS_MAKE_STATIC_STRING("flags"), ZYDIS_MAKE_STATIC_STRING("eflags"),
ZYDIS_MAKE_STATIC_STRING("rflags"),
// Instruction-pointer registers
ZYDIS_MAKE_STATIC_STRING("ip"), ZYDIS_MAKE_STATIC_STRING("eip"),
ZYDIS_MAKE_STATIC_STRING("rip"),
// Segment registers // Segment registers
"es", "cs", "ss", "ds", ZYDIS_MAKE_STATIC_STRING("es"), ZYDIS_MAKE_STATIC_STRING("ss"),
"fs", "gs", ZYDIS_MAKE_STATIC_STRING("cs"), ZYDIS_MAKE_STATIC_STRING("ds"),
// Table registers ZYDIS_MAKE_STATIC_STRING("fs"), ZYDIS_MAKE_STATIC_STRING("gs"),
"gdtr", "ldtr", "idtr", "tr",
// Test registers // Test registers
"tr0", "tr1", "tr2", "tr3", ZYDIS_MAKE_STATIC_STRING("tr0"), ZYDIS_MAKE_STATIC_STRING("tr1"),
"tr4", "tr5", "tr6", "tr7", ZYDIS_MAKE_STATIC_STRING("tr2"), ZYDIS_MAKE_STATIC_STRING("tr3"),
ZYDIS_MAKE_STATIC_STRING("tr4"), ZYDIS_MAKE_STATIC_STRING("tr5"),
ZYDIS_MAKE_STATIC_STRING("tr6"), ZYDIS_MAKE_STATIC_STRING("tr7"),
// Control registers // Control registers
"cr0", "cr1", "cr2", "cr3", ZYDIS_MAKE_STATIC_STRING("cr0"), ZYDIS_MAKE_STATIC_STRING("cr1"),
"cr4", "cr5", "cr6", "cr7", ZYDIS_MAKE_STATIC_STRING("cr2"), ZYDIS_MAKE_STATIC_STRING("cr3"),
"cr8", "cr9", "cr10", "cr11", ZYDIS_MAKE_STATIC_STRING("cr4"), ZYDIS_MAKE_STATIC_STRING("cr5"),
"cr12", "cr13", "cr14", "cr15", ZYDIS_MAKE_STATIC_STRING("cr6"), ZYDIS_MAKE_STATIC_STRING("cr7"),
ZYDIS_MAKE_STATIC_STRING("cr8"), ZYDIS_MAKE_STATIC_STRING("cr9"),
ZYDIS_MAKE_STATIC_STRING("cr10"), ZYDIS_MAKE_STATIC_STRING("cr11"),
ZYDIS_MAKE_STATIC_STRING("cr12"), ZYDIS_MAKE_STATIC_STRING("cr13"),
ZYDIS_MAKE_STATIC_STRING("cr14"), ZYDIS_MAKE_STATIC_STRING("cr15"),
// Debug registers // Debug registers
"dr0", "dr1", "dr2", "dr3", ZYDIS_MAKE_STATIC_STRING("dr0"), ZYDIS_MAKE_STATIC_STRING("dr1"),
"dr4", "dr5", "dr6", "dr7", ZYDIS_MAKE_STATIC_STRING("dr2"), ZYDIS_MAKE_STATIC_STRING("dr3"),
"dr8", "dr9", "dr10", "dr11", ZYDIS_MAKE_STATIC_STRING("dr4"), ZYDIS_MAKE_STATIC_STRING("dr5"),
"dr12", "dr13", "dr14", "dr15", ZYDIS_MAKE_STATIC_STRING("dr6"), ZYDIS_MAKE_STATIC_STRING("dr7"),
ZYDIS_MAKE_STATIC_STRING("dr8"), ZYDIS_MAKE_STATIC_STRING("dr9"),
ZYDIS_MAKE_STATIC_STRING("dr10"), ZYDIS_MAKE_STATIC_STRING("dr11"),
ZYDIS_MAKE_STATIC_STRING("dr12"), ZYDIS_MAKE_STATIC_STRING("dr13"),
ZYDIS_MAKE_STATIC_STRING("dr14"), ZYDIS_MAKE_STATIC_STRING("dr15"),
// Mask registers // Mask registers
"k0", "k1", "k2", "k3", ZYDIS_MAKE_STATIC_STRING("k0"), ZYDIS_MAKE_STATIC_STRING("k1"),
"k4", "k5", "k6", "k7", ZYDIS_MAKE_STATIC_STRING("k2"), ZYDIS_MAKE_STATIC_STRING("k3"),
// Bounds registers ZYDIS_MAKE_STATIC_STRING("k4"), ZYDIS_MAKE_STATIC_STRING("k5"),
"bnd0", "bnd1", "bnd2", "bnd3", ZYDIS_MAKE_STATIC_STRING("k6"), ZYDIS_MAKE_STATIC_STRING("k7"),
"bndcfg", "bndstatus" // Bound registers
ZYDIS_MAKE_STATIC_STRING("bnd0"), ZYDIS_MAKE_STATIC_STRING("bnd1"),
ZYDIS_MAKE_STATIC_STRING("bnd2"), ZYDIS_MAKE_STATIC_STRING("bnd3"),
// Misc registers
ZYDIS_MAKE_STATIC_STRING("mxcsr"), ZYDIS_MAKE_STATIC_STRING("pkru"),
ZYDIS_MAKE_STATIC_STRING("xcr0"), ZYDIS_MAKE_STATIC_STRING("gdtr"),
ZYDIS_MAKE_STATIC_STRING("ldtr"), ZYDIS_MAKE_STATIC_STRING("idtr"),
ZYDIS_MAKE_STATIC_STRING("tr"), ZYDIS_MAKE_STATIC_STRING("bndcfg"),
ZYDIS_MAKE_STATIC_STRING("bndstatus")
}; };
/* ============================================================================================== */ /* ============================================================================================== */
@ -272,7 +346,16 @@ const char* ZydisRegisterGetString(ZydisRegister reg)
{ {
return ZYDIS_NULL; return ZYDIS_NULL;
} }
return registerStrings[reg]; return registerStrings[reg].buffer;
}
const ZydisStaticString* ZydisRegisterGetStaticString(ZydisRegister reg)
{
if (reg > (sizeof(registerStrings) / sizeof(registerStrings[0])) - 1)
{
return ZYDIS_NULL;
}
return &registerStrings[reg];
} }
/* ============================================================================================== */ /* ============================================================================================== */