Minor refactorings and bugfixes

This commit is contained in:
flobernd 2017-12-02 06:54:47 +01:00
parent 71b21c4301
commit 217d5cc9af
No known key found for this signature in database
GPG Key ID: 9C3AE0ED4A969F10
3 changed files with 104 additions and 33 deletions

View File

@ -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 ()

93
src/InternalTypes.h Normal file
View File

@ -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 <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 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 */

View File

@ -25,39 +25,17 @@
***************************************************************************************************/
#include <Zydis/Mnemonic.h>
/* ============================================================================================== */
/* 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 <InternalTypes.h>
#include <Generated/EnumMnemonic.inc>
/**
* @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];
}
/* ============================================================================================== */