Path: blob/master/dep/zydis/include/Zydis/FormatterBuffer.h
4216 views
/***************************************************************************************************12Zyan Disassembler Library (Zydis)34Original Author : Florian Bernd56* 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* Implements the `ZydisFormatterToken` type and provides functions to use it.29*/3031#ifndef ZYDIS_FORMATTER_TOKEN_H32#define ZYDIS_FORMATTER_TOKEN_H3334#include <Zycore/String.h>35#include <Zycore/Types.h>36#include <Zydis/Defines.h>37#include <Zydis/Status.h>3839#ifdef __cplusplus40extern "C" {41#endif4243/* ============================================================================================== */44/* Constants */45/* ============================================================================================== */4647/* ---------------------------------------------------------------------------------------------- */48/* Token types */49/* ---------------------------------------------------------------------------------------------- */5051/**52* Defines the `ZydisTokenType` data-type.53*/54typedef ZyanU8 ZydisTokenType;5556#define ZYDIS_TOKEN_INVALID 0x0057/**58* A whitespace character.59*/60#define ZYDIS_TOKEN_WHITESPACE 0x0161/**62* A delimiter character (like `','`, `':'`, `'+'`, `'-'`, `'*'`).63*/64#define ZYDIS_TOKEN_DELIMITER 0x0265/**66* An opening parenthesis character (like `'('`, `'['`, `'{'`).67*/68#define ZYDIS_TOKEN_PARENTHESIS_OPEN 0x0369/**70* A closing parenthesis character (like `')'`, `']'`, `'}'`).71*/72#define ZYDIS_TOKEN_PARENTHESIS_CLOSE 0x0473/**74* A prefix literal (like `"LOCK"`, `"REP"`).75*/76#define ZYDIS_TOKEN_PREFIX 0x0577/**78* A mnemonic literal (like `"MOV"`, `"VCMPPSD"`, `"LCALL"`).79*/80#define ZYDIS_TOKEN_MNEMONIC 0x0681/**82* A register literal (like `"RAX"`, `"DS"`, `"%ECX"`).83*/84#define ZYDIS_TOKEN_REGISTER 0x0785/**86* An absolute address literal (like `0x00400000`).87*/88#define ZYDIS_TOKEN_ADDRESS_ABS 0x0889/**90* A relative address literal (like `-0x100`).91*/92#define ZYDIS_TOKEN_ADDRESS_REL 0x0993/**94* A displacement literal (like `0xFFFFFFFF`, `-0x100`, `+0x1234`).95*/96#define ZYDIS_TOKEN_DISPLACEMENT 0x0A97/**98* An immediate literal (like `0xC0`, `-0x1234`, `$0x0000`).99*/100#define ZYDIS_TOKEN_IMMEDIATE 0x0B101/**102* A typecast literal (like `DWORD PTR`).103*/104#define ZYDIS_TOKEN_TYPECAST 0x0C105/**106* A decorator literal (like `"Z"`, `"1TO4"`).107*/108#define ZYDIS_TOKEN_DECORATOR 0x0D109/**110* A symbol literal.111*/112#define ZYDIS_TOKEN_SYMBOL 0x0E113114/**115* The base for user-defined token types.116*/117#define ZYDIS_TOKEN_USER 0x80118119/* ---------------------------------------------------------------------------------------------- */120121/* ============================================================================================== */122/* Enums and types */123/* ============================================================================================== */124125/* ---------------------------------------------------------------------------------------------- */126/* Token */127/* ---------------------------------------------------------------------------------------------- */128129#pragma pack(push, 1)130131/**132* Defines the `ZydisFormatterToken` struct.133*134* All fields in this struct should be considered as "private". Any changes may lead to unexpected135* behavior.136*/137typedef struct ZydisFormatterToken_138{139/**140* The token type.141*/142ZydisTokenType type;143/**144* An offset to the next token, or `0`.145*/146ZyanU8 next;147} ZydisFormatterToken;148149#pragma pack(pop)150151/**152* Defines the `ZydisFormatterTokenConst` data-type.153*/154typedef const ZydisFormatterToken ZydisFormatterTokenConst;155156/* ---------------------------------------------------------------------------------------------- */157/* Buffer */158/* ---------------------------------------------------------------------------------------------- */159160/**161* Defines the `ZydisFormatterBuffer` struct.162*163* All fields in this struct should be considered as "private". Any changes may164* lead to unexpected behavior.165*/166typedef struct ZydisFormatterBuffer_167{168/**169* `ZYAN_TRUE`, if the buffer contains a token stream or `ZYAN_FALSE, if it170* contains a simple string.171*/172ZyanBool is_token_list;173/**174* The remaining capacity of the buffer.175*/176ZyanUSize capacity;177/**178* The `ZyanString` instance that refers to the literal value of the most179* recently added token.180*/181ZyanString string;182} ZydisFormatterBuffer;183184/* ---------------------------------------------------------------------------------------------- */185186/* ============================================================================================== */187/* Exported functions */188/* ============================================================================================== */189190/* ---------------------------------------------------------------------------------------------- */191/* Token */192/* ---------------------------------------------------------------------------------------------- */193194/**195* Returns the `type` and the string `value` of the given `token`.196*197* @param token A pointer to the `ZydisFormatterToken` struct.198* @param type Receives the token type.199* @param value Receives a pointer to the string value of the token.200*201* @return A zyan status code.202*/203ZYDIS_EXPORT ZyanStatus ZydisFormatterTokenGetValue(const ZydisFormatterToken* token,204ZydisTokenType* type, ZyanConstCharPointer* value);205206/**207* Obtains the next `token` linked to the passed one.208*209* @param token Receives a pointer to the next `ZydisFormatterToken` struct210* linked to the passed one.211*212* @return A zyan status code.213*/214ZYDIS_EXPORT ZyanStatus ZydisFormatterTokenNext(ZydisFormatterTokenConst** token);215216/* ---------------------------------------------------------------------------------------------- */217/* Buffer */218/* ---------------------------------------------------------------------------------------------- */219220/**221* Returns the current (most recently added) token.222*223* @param buffer A pointer to the `ZydisFormatterBuffer` struct.224* @param token Receives a pointer to the current token.225*226* @return A zyan status code.227*228* This function returns `ZYAN_STATUS_INVALID_OPERATION`, if the buffer does not contain at least229* one token.230*/231ZYDIS_EXPORT ZyanStatus ZydisFormatterBufferGetToken(const ZydisFormatterBuffer* buffer,232ZydisFormatterTokenConst** token);233234/**235* Returns the `ZyanString` instance associated with the given buffer.236*237* @param buffer A pointer to the `ZydisFormatterBuffer` struct.238* @param string Receives a pointer to the `ZyanString` instance associated with the given239* buffer.240*241* @return A zyan status code.242*243* This function returns `ZYAN_STATUS_INVALID_OPERATION`, if the buffer does not contain at least244* one token.245*246* The returned string always refers to the literal value of the current (most recently added)247* token and will remain valid until the buffer is destroyed.248*/249ZYDIS_EXPORT ZyanStatus ZydisFormatterBufferGetString(ZydisFormatterBuffer* buffer,250ZyanString** string);251252/**253* Appends a new token to the `buffer`.254*255* @param buffer A pointer to the `ZydisFormatterBuffer` struct.256* @param type The type of the new token.257*258* @return A zyan status code.259*260* Note that the `ZyanString` instance returned by `ZydisFormatterBufferGetString` will261* automatically be updated by calling this function.262*/263ZYDIS_EXPORT ZyanStatus ZydisFormatterBufferAppend(ZydisFormatterBuffer* buffer,264ZydisTokenType type);265266/**267* Returns a snapshot of the buffer-state.268*269* @param buffer A pointer to the `ZydisFormatterBuffer` struct.270* @param state Receives a snapshot of the buffer-state.271*272* @return A zyan status code.273*274* Note that the buffer-state is saved inside the buffer itself and thus becomes invalid as soon275* as the buffer gets overwritten or destroyed.276*/277ZYDIS_EXPORT ZyanStatus ZydisFormatterBufferRemember(const ZydisFormatterBuffer* buffer,278ZyanUPointer* state);279280/**281* Restores a previously saved buffer-state.282*283* @param buffer A pointer to the `ZydisFormatterBuffer` struct.284* @param state The buffer-state to restore.285*286* @return A zyan status code.287*288* All tokens added after obtaining the given `state` snapshot will be removed. This function289* does NOT restore any string content.290*291* Note that the `ZyanString` instance returned by `ZydisFormatterBufferGetString` will292* automatically be updated by calling this function.293*/294ZYDIS_EXPORT ZyanStatus ZydisFormatterBufferRestore(ZydisFormatterBuffer* buffer,295ZyanUPointer state);296297/* ---------------------------------------------------------------------------------------------- */298299/* ============================================================================================== */300301#ifdef __cplusplus302}303#endif304305#endif /* ZYDIS_FORMATTER_TOKEN_H */306307308