Zydis  v3.0.0
FormatterBuffer.h
Go to the documentation of this file.
1 /***************************************************************************************************
2 
3  Zyan Disassembler Library (Zydis)
4 
5  Original Author : Florian Bernd
6 
7  * Permission is hereby granted, free of charge, to any person obtaining a copy
8  * of this software and associated documentation files (the "Software"), to deal
9  * in the Software without restriction, including without limitation the rights
10  * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
11  * copies of the Software, and to permit persons to whom the Software is
12  * furnished to do so, subject to the following conditions:
13  *
14  * The above copyright notice and this permission notice shall be included in all
15  * copies or substantial portions of the Software.
16  *
17  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
18  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
20  * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
21  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
22  * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
23  * SOFTWARE.
24 
25 ***************************************************************************************************/
26 
32 #ifndef ZYDIS_FORMATTER_TOKEN_H
33 #define ZYDIS_FORMATTER_TOKEN_H
34 
35 #include <ZydisExportConfig.h>
36 #include <Zycore/String.h>
37 #include <Zycore/Types.h>
38 #include <Zydis/Status.h>
39 
40 #ifdef __cplusplus
41 extern "C" {
42 #endif
43 
44 /* ============================================================================================== */
45 /* Constants */
46 /* ============================================================================================== */
47 
48 /* ---------------------------------------------------------------------------------------------- */
49 /* Token types */
50 /* ---------------------------------------------------------------------------------------------- */
51 
55 typedef ZyanU8 ZydisTokenType;
56 
57 #define ZYDIS_TOKEN_INVALID 0x00
61 #define ZYDIS_TOKEN_WHITESPACE 0x01
65 #define ZYDIS_TOKEN_DELIMITER 0x02
69 #define ZYDIS_TOKEN_PARENTHESIS_OPEN 0x03
73 #define ZYDIS_TOKEN_PARENTHESIS_CLOSE 0x04
77 #define ZYDIS_TOKEN_PREFIX 0x05
81 #define ZYDIS_TOKEN_MNEMONIC 0x06
85 #define ZYDIS_TOKEN_REGISTER 0x07
89 #define ZYDIS_TOKEN_ADDRESS_ABS 0x08
93 #define ZYDIS_TOKEN_ADDRESS_REL 0x09
97 #define ZYDIS_TOKEN_DISPLACEMENT 0x0A
101 #define ZYDIS_TOKEN_IMMEDIATE 0x0B
105 #define ZYDIS_TOKEN_TYPECAST 0x0C
109 #define ZYDIS_TOKEN_DECORATOR 0x0D
113 #define ZYDIS_TOKEN_SYMBOL 0x0E
114 
118 #define ZYDIS_TOKEN_USER 0x80
119 
120 /* ---------------------------------------------------------------------------------------------- */
121 
122 /* ============================================================================================== */
123 /* Enums and types */
124 /* ============================================================================================== */
125 
126 /* ---------------------------------------------------------------------------------------------- */
127 /* Token */
128 /* ---------------------------------------------------------------------------------------------- */
129 
130 #pragma pack(push, 1)
131 
138 typedef struct ZydisFormatterToken_
139 {
147  ZyanU8 next;
149 
150 #pragma pack(pop)
151 
156 
157 /* ---------------------------------------------------------------------------------------------- */
158 /* Buffer */
159 /* ---------------------------------------------------------------------------------------------- */
160 
167 typedef struct ZydisFormatterBuffer_
168 {
173  ZyanBool is_token_list;
177  ZyanUSize capacity;
182  ZyanString string;
184 
185 /* ---------------------------------------------------------------------------------------------- */
186 
187 /* ============================================================================================== */
188 /* Exported functions */
189 /* ============================================================================================== */
190 
191 /* ---------------------------------------------------------------------------------------------- */
192 /* Token */
193 /* ---------------------------------------------------------------------------------------------- */
194 
204 ZYDIS_EXPORT ZyanStatus ZydisFormatterTokenGetValue(const ZydisFormatterToken* token,
205  ZydisTokenType* type, ZyanConstCharPointer* value);
206 
215 ZYDIS_EXPORT ZyanStatus ZydisFormatterTokenNext(ZydisFormatterTokenConst** token);
216 
217 /* ---------------------------------------------------------------------------------------------- */
218 /* Buffer */
219 /* ---------------------------------------------------------------------------------------------- */
220 
232 ZYDIS_EXPORT ZyanStatus ZydisFormatterBufferGetToken(const ZydisFormatterBuffer* buffer,
233  ZydisFormatterTokenConst** token);
234 
250 ZYDIS_EXPORT ZyanStatus ZydisFormatterBufferGetString(ZydisFormatterBuffer* buffer,
251  ZyanString** string);
252 
264 ZYDIS_EXPORT ZyanStatus ZydisFormatterBufferAppend(ZydisFormatterBuffer* buffer,
265  ZydisTokenType type);
266 
278 ZYDIS_EXPORT ZyanStatus ZydisFormatterBufferRemember(const ZydisFormatterBuffer* buffer,
279  ZyanUPointer* state);
280 
295 ZYDIS_EXPORT ZyanStatus ZydisFormatterBufferRestore(ZydisFormatterBuffer* buffer,
296  ZyanUPointer state);
297 
298 /* ---------------------------------------------------------------------------------------------- */
299 
300 /* ============================================================================================== */
301 
302 #ifdef __cplusplus
303 }
304 #endif
305 
306 #endif /* ZYDIS_FORMATTER_TOKEN_H */
ZYDIS_EXPORT ZyanStatus ZydisFormatterBufferRestore(ZydisFormatterBuffer *buffer, ZyanUPointer state)
Restores a previously saved buffer-state.
ZYDIS_EXPORT ZyanStatus ZydisFormatterTokenNext(ZydisFormatterTokenConst **token)
Obtains the next token linked to the passed one.
struct ZydisFormatterBuffer_ ZydisFormatterBuffer
Defines the ZydisFormatterBuffer struct.
ZYDIS_EXPORT ZyanStatus ZydisFormatterTokenGetValue(const ZydisFormatterToken *token, ZydisTokenType *type, ZyanConstCharPointer *value)
Returns the type and the string value of the given token.
ZYDIS_EXPORT ZyanStatus ZydisFormatterBufferGetToken(const ZydisFormatterBuffer *buffer, ZydisFormatterTokenConst **token)
Returns the current (most recently added) token.
ZYDIS_EXPORT ZyanStatus ZydisFormatterBufferRemember(const ZydisFormatterBuffer *buffer, ZyanUPointer *state)
Returns a snapshot of the buffer-state.
ZYDIS_EXPORT ZyanStatus ZydisFormatterBufferAppend(ZydisFormatterBuffer *buffer, ZydisTokenType type)
Appends a new token to the buffer.
struct ZydisFormatterToken_ ZydisFormatterToken
Defines the ZydisFormatterToken struct.
const ZydisFormatterToken ZydisFormatterTokenConst
Defines the ZydisFormatterTokenConst data-type.
Definition: FormatterBuffer.h:155
ZYDIS_EXPORT ZyanStatus ZydisFormatterBufferGetString(ZydisFormatterBuffer *buffer, ZyanString **string)
Returns the ZyanString instance associated with the given buffer.
ZyanU8 ZydisTokenType
@biref Defines the ZydisTokenType data-type.
Definition: FormatterBuffer.h:55
Status code definitions and check macros.
Defines the ZydisFormatterBuffer struct.
Definition: FormatterBuffer.h:168
ZyanString string
The ZyanString instance that refers to the literal value of the most recently added token.
Definition: FormatterBuffer.h:182
ZyanUSize capacity
The remaining capacity of the buffer.
Definition: FormatterBuffer.h:177
ZyanBool is_token_list
ZYAN_TRUE, if the buffer contains a token stream or `ZYAN_FALSE, if it contains a simple string.
Definition: FormatterBuffer.h:173
Defines the ZydisFormatterToken struct.
Definition: FormatterBuffer.h:139
ZyanU8 next
An offset to the next token, or 0.
Definition: FormatterBuffer.h:147
ZydisTokenType type
The token type.
Definition: FormatterBuffer.h:143