diff --git a/CMakeLists.txt b/CMakeLists.txt index db6f0f0..a13fa7f 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -108,8 +108,9 @@ target_sources("Zydis" "${CMAKE_CURRENT_LIST_DIR}/include/Zydis/String.h" "${CMAKE_CURRENT_LIST_DIR}/include/Zydis/Utils.h" "${CMAKE_CURRENT_LIST_DIR}/include/Zydis/Zydis.h" + "${CMAKE_CURRENT_LIST_DIR}/include/Zydis/Internal/LibC.h" PRIVATE - "src/LibC.h" + "src/InternalTypes.h" "src/MetaInfo.c" "src/Mnemonic.c" "src/Register.c" @@ -126,8 +127,8 @@ if (ZYDIS_FEATURE_DECODER) "${CMAKE_CURRENT_LIST_DIR}/include/Zydis/DecoderTypes.h" "${CMAKE_CURRENT_LIST_DIR}/include/Zydis/Formatter.h" PRIVATE - "src/DecoderData.h" "src/Decoder.c" + "src/DecoderData.h" "src/DecoderData.c" "src/Formatter.c") endif () diff --git a/src/InternalTypes.h b/src/InternalTypes.h new file mode 100644 index 0000000..159feee --- /dev/null +++ b/src/InternalTypes.h @@ -0,0 +1,93 @@ +/*************************************************************************************************** + + 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_INTERNALTYPES_H +#define ZYDIS_INTERNALTYPES_H + +#include +#include + +#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 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_INTERNALTYPES_H */ diff --git a/src/Mnemonic.c b/src/Mnemonic.c index 26dd743..e07ff42 100644 --- a/src/Mnemonic.c +++ b/src/Mnemonic.c @@ -25,39 +25,17 @@ ***************************************************************************************************/ #include - -/* ============================================================================================== */ -/* Mnemonic strings */ -/* ============================================================================================== */ - -#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) +#include #include /** - * @brief Contains all strings that were accessed by `ZydisMnemonicGetStringEx`. + * @brief Caches all strings that were accessed by `ZydisMnemonicGetStringEx`. * - * We could store `ZydisString` structs instead of `ZydisInternalString` ones in the + * We could store `ZydisString` structs instead of `ZydisGeneratedString` ones in the * `zydisMnemonicStrings` array, but this would significantly increase the table-size. */ -static ZydisString stringTable[ZYDIS_MNEMONIC_MAX_VALUE + 1]; +static ZydisString zydisMnemonicStringCache[ZYDIS_MNEMONIC_MAX_VALUE + 1]; /* ============================================================================================== */ /* Exported functions */ @@ -78,13 +56,12 @@ const ZydisString* ZydisMnemonicGetStringEx(ZydisMnemonic mnemonic) { return ZYDIS_NULL; } - if (!stringTable[mnemonic].buffer) + if (!zydisMnemonicStringCache[mnemonic].buffer) { - stringTable[mnemonic].buffer = zydisMnemonicStrings[mnemonic].buffer; - stringTable[mnemonic].length = zydisMnemonicStrings[mnemonic].length; - stringTable[mnemonic].capacity = zydisMnemonicStrings[mnemonic].length; + ZydisStringInitWithGeneratedString(&zydisMnemonicStringCache[mnemonic], + &zydisMnemonicStrings[mnemonic]); } - return &stringTable[mnemonic]; + return &zydisMnemonicStringCache[mnemonic]; } /* ============================================================================================== */