Path: blob/master/dep/zydis/include/Zydis/Internal/FormatterBase.h
4219 views
/***************************************************************************************************12Zyan Disassembler Library (Zydis)34Original Author : Florian Bernd, Joel Hoener56* Permission is hereby granted, free of charge, to any person obtaining a copy7* of this software and associated documentation files (the "Software"), to deal8* in the Software without restriction, including without limitation the rights9* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell10* copies of the Software, and to permit persons to whom the Software is11* furnished to do so, subject to the following conditions:12*13* The above copyright notice and this permission notice shall be included in all14* copies or substantial portions of the Software.15*16* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR17* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,18* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE19* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER20* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,21* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE22* SOFTWARE.2324***************************************************************************************************/2526/**27* @file28* Provides formatter functions that are shared between the different formatters.29*/3031#ifndef ZYDIS_FORMATTER_BASE_H32#define ZYDIS_FORMATTER_BASE_H3334#include <Zydis/Formatter.h>35#include <Zydis/Internal/String.h>3637#ifdef __cplusplus38extern "C" {39#endif4041/* ============================================================================================== */42/* Macros */43/* ============================================================================================== */4445/* ---------------------------------------------------------------------------------------------- */46/* String */47/* ---------------------------------------------------------------------------------------------- */4849/**50* Appends an unsigned numeric value to the given string.51*52* @param formatter A pointer to the `ZydisFormatter` instance.53* @param base The numeric base.54* @param str The destination string.55* @param value The value to append.56* @param padding_length The padding length.57* @param force_leading_number Enable this option to prepend a leading `0` if the first58* character is non-numeric.59*/60#define ZYDIS_STRING_APPEND_NUM_U(formatter, base, str, value, padding_length, \61force_leading_number) \62switch (base) \63{ \64case ZYDIS_NUMERIC_BASE_DEC: \65ZYAN_CHECK(ZydisStringAppendDecU(str, value, padding_length, \66(formatter)->number_format[base][0].string, \67(formatter)->number_format[base][1].string)); \68break; \69case ZYDIS_NUMERIC_BASE_HEX: \70ZYAN_CHECK(ZydisStringAppendHexU(str, value, padding_length, force_leading_number, \71(formatter)->hex_uppercase, \72(formatter)->number_format[base][0].string, \73(formatter)->number_format[base][1].string)); \74break; \75default: \76return ZYAN_STATUS_INVALID_ARGUMENT; \77}7879/**80* Appends a signed numeric value to the given string.81*82* @param formatter A pointer to the `ZydisFormatter` instance.83* @param base The numeric base.84* @param str The destination string.85* @param value The value to append.86* @param padding_length The padding length.87* @param force_leading_number Enable this option to prepend a leading `0`, if the first88* character is non-numeric.89* @param force_sign Enable to print the '+' sign for positive numbers.90*/91#define ZYDIS_STRING_APPEND_NUM_S(formatter, base, str, value, padding_length, \92force_leading_number, force_sign) \93switch (base) \94{ \95case ZYDIS_NUMERIC_BASE_DEC: \96ZYAN_CHECK(ZydisStringAppendDecS(str, value, padding_length, force_sign, \97(formatter)->number_format[base][0].string, \98(formatter)->number_format[base][1].string)); \99break; \100case ZYDIS_NUMERIC_BASE_HEX: \101ZYAN_CHECK(ZydisStringAppendHexS(str, value, padding_length, force_leading_number, \102(formatter)->hex_uppercase, force_sign, \103(formatter)->number_format[base][0].string, \104(formatter)->number_format[base][1].string)); \105break; \106default: \107return ZYAN_STATUS_INVALID_ARGUMENT; \108}109110/* ---------------------------------------------------------------------------------------------- */111/* Buffer */112/* ---------------------------------------------------------------------------------------------- */113114/**115* Invokes the `ZydisFormatterBufferAppend` routine, if tokenization is enabled for the116* current pass.117*118* @param buffer A pointer to the `ZydisFormatterBuffer` struct.119* @param type The token type.120*121* Using this macro instead of direct calls to `ZydisFormatterBufferAppend` greatly improves the122* performance for non-tokenizing passes.123*/124#define ZYDIS_BUFFER_APPEND_TOKEN(buffer, type) \125if ((buffer)->is_token_list) \126{ \127ZYAN_CHECK(ZydisFormatterBufferAppend(buffer, type)); \128}129130/**131* Returns a snapshot of the buffer-state.132*133* @param buffer A pointer to the `ZydisFormatterBuffer` struct.134* @param state Receives a snapshot of the buffer-state.135*136* Using this macro instead of direct calls to `ZydisFormatterBufferRemember` improves the137* performance for non-tokenizing passes.138*/139#define ZYDIS_BUFFER_REMEMBER(buffer, state) \140if ((buffer)->is_token_list) \141{ \142(state) = (ZyanUPointer)(buffer)->string.vector.data; \143} else \144{ \145(state) = (ZyanUPointer)(buffer)->string.vector.size; \146}147148/**149* Appends a string (`STR_`-prefix) or a predefined token-list (`TOK_`-prefix).150*151* @param buffer A pointer to the `ZydisFormatterBuffer` struct.152* @param name The base name (without prefix) of the string- or token.153*/154#define ZYDIS_BUFFER_APPEND(buffer, name) \155if ((buffer)->is_token_list) \156{ \157ZYAN_CHECK(ZydisFormatterBufferAppendPredefined(buffer, TOK_ ## name)); \158} else \159{ \160ZYAN_CHECK(ZydisStringAppendShort(&buffer->string, &STR_ ## name)); \161}162163// TODO: Implement `letter_case` for predefined tokens164165/**166* Appends a string (`STR_`-prefix) or a predefined token-list (`TOK_`-prefix).167*168* @param buffer A pointer to the `ZydisFormatterBuffer` struct.169* @param name The base name (without prefix) of the string- or token.170* @param letter_case The desired letter-case.171*/172#define ZYDIS_BUFFER_APPEND_CASE(buffer, name, letter_case) \173if ((buffer)->is_token_list) \174{ \175ZYAN_CHECK(ZydisFormatterBufferAppendPredefined(buffer, TOK_ ## name)); \176} else \177{ \178ZYAN_CHECK(ZydisStringAppendShortCase(&buffer->string, &STR_ ## name, letter_case)); \179}180181/* ---------------------------------------------------------------------------------------------- */182183/* ============================================================================================== */184/* Helper functions */185/* ============================================================================================== */186187/* ---------------------------------------------------------------------------------------------- */188/* Buffer */189/* ---------------------------------------------------------------------------------------------- */190191// MSVC does not like the C99 flexible-array extension192#ifdef ZYAN_MSVC193# pragma warning(push)194# pragma warning(disable:4200)195#endif196197#pragma pack(push, 1)198199typedef struct ZydisPredefinedToken_200{201ZyanU8 size;202ZyanU8 next;203ZyanU8 data[];204} ZydisPredefinedToken;205206#pragma pack(pop)207208#ifdef ZYAN_MSVC209# pragma warning(pop)210#endif211212/**213* Appends a predefined token-list to the `buffer`.214*215* @param buffer A pointer to the `ZydisFormatterBuffer` struct.216* @param data A pointer to the `ZydisPredefinedToken` struct.217*218* @return A zycore status code.219*220* This function is internally used to improve performance while adding static strings or multiple221* tokens at once.222*/223ZYAN_INLINE ZyanStatus ZydisFormatterBufferAppendPredefined(ZydisFormatterBuffer* buffer,224const ZydisPredefinedToken* data)225{226ZYAN_ASSERT(buffer);227ZYAN_ASSERT(data);228229const ZyanUSize len = buffer->string.vector.size;230ZYAN_ASSERT((len > 0) && (len < 256));231if (buffer->capacity <= len + data->size)232{233return ZYAN_STATUS_INSUFFICIENT_BUFFER_SIZE;234}235236ZydisFormatterToken* const last = (ZydisFormatterToken*)buffer->string.vector.data - 1;237last->next = (ZyanU8)len;238239ZYAN_MEMCPY((ZyanU8*)buffer->string.vector.data + len, &data->data[0], data->size);240241const ZyanUSize delta = len + data->next;242buffer->capacity -= delta;243buffer->string.vector.data = (ZyanU8*)buffer->string.vector.data + delta;244buffer->string.vector.size = data->size - data->next;245buffer->string.vector.capacity = ZYAN_MIN(buffer->capacity, 255);246247return ZYAN_STATUS_SUCCESS;248}249250/* ---------------------------------------------------------------------------------------------- */251/* General */252/* ---------------------------------------------------------------------------------------------- */253254/**255* Returns the size to be used as explicit size suffix (`AT&T`) or explicit typecast256* (`INTEL`), if required.257*258* @param formatter A pointer to the `ZydisFormatter` instance.259* @param context A pointer to the `ZydisFormatterContext` struct.260* @param operand The instructions first memory operand.261*262* @return Returns the explicit size, if required, or `0`, if not needed.263*264* This function always returns a size different to `0`, if the `ZYDIS_FORMATTER_PROP_FORCE_SIZE`265* is set to `ZYAN_TRUE`.266*/267ZyanU32 ZydisFormatterHelperGetExplicitSize(const ZydisFormatter* formatter,268ZydisFormatterContext* context, const ZydisDecodedOperand* operand);269270/* ---------------------------------------------------------------------------------------------- */271272/* ============================================================================================== */273/* Formatter functions */274/* ============================================================================================== */275276/* ---------------------------------------------------------------------------------------------- */277/* Operands */278/* ---------------------------------------------------------------------------------------------- */279280ZyanStatus ZydisFormatterBaseFormatOperandREG(const ZydisFormatter* formatter,281ZydisFormatterBuffer* buffer, ZydisFormatterContext* context);282283ZyanStatus ZydisFormatterBaseFormatOperandPTR(const ZydisFormatter* formatter,284ZydisFormatterBuffer* buffer, ZydisFormatterContext* context);285286ZyanStatus ZydisFormatterBaseFormatOperandIMM(const ZydisFormatter* formatter,287ZydisFormatterBuffer* buffer, ZydisFormatterContext* context);288289/* ---------------------------------------------------------------------------------------------- */290/* Elemental tokens */291/* ---------------------------------------------------------------------------------------------- */292293ZyanStatus ZydisFormatterBasePrintAddressABS(const ZydisFormatter* formatter,294ZydisFormatterBuffer* buffer, ZydisFormatterContext* context);295296ZyanStatus ZydisFormatterBasePrintAddressREL(const ZydisFormatter* formatter,297ZydisFormatterBuffer* buffer, ZydisFormatterContext* context);298299ZyanStatus ZydisFormatterBasePrintIMM(const ZydisFormatter* formatter,300ZydisFormatterBuffer* buffer, ZydisFormatterContext* context);301302/* ---------------------------------------------------------------------------------------------- */303/* Optional tokens */304/* ---------------------------------------------------------------------------------------------- */305306ZyanStatus ZydisFormatterBasePrintSegment(const ZydisFormatter* formatter,307ZydisFormatterBuffer* buffer, ZydisFormatterContext* context);308309ZyanStatus ZydisFormatterBasePrintPrefixes(const ZydisFormatter* formatter,310ZydisFormatterBuffer* buffer, ZydisFormatterContext* context);311312ZyanStatus ZydisFormatterBasePrintDecorator(const ZydisFormatter* formatter,313ZydisFormatterBuffer* buffer, ZydisFormatterContext* context, ZydisDecorator decorator);314315/* ---------------------------------------------------------------------------------------------- */316317/* ============================================================================================== */318319#ifdef __cplusplus320}321#endif322323#endif // ZYDIS_FORMATTER_BASE_H324325326