1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
|
/***************************************************************************************************
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.
***************************************************************************************************/
/**
* @file
* @brief Implements the `ZydisFormatterToken` type and provides functions to use it.
*/
#ifndef ZYDIS_FORMATTER_TOKEN_H
#define ZYDIS_FORMATTER_TOKEN_H
#include "zydis/ZydisExportConfig.h"
#include "zydis/Zycore/String.h"
#include "zydis/Zycore/Types.h"
#include "zydis/Zydis/Status.h"
#ifdef __cplusplus
extern "C" {
#endif
/* ============================================================================================== */
/* Constants */
/* ============================================================================================== */
/* ---------------------------------------------------------------------------------------------- */
/* Token types */
/* ---------------------------------------------------------------------------------------------- */
/**
* @biref Defines the `ZydisTokenType` data-type.
*/
typedef ZyanU8 ZydisTokenType;
#define ZYDIS_TOKEN_INVALID 0x00
/**
* @brief A whitespace character.
*/
#define ZYDIS_TOKEN_WHITESPACE 0x01
/**
* @brief A delimiter character (like `','`, `':'`, `'+'`, `'-'`, `'*'`).
*/
#define ZYDIS_TOKEN_DELIMITER 0x02
/**
* @brief An opening parenthesis character (like `'('`, `'['`, `'{'`).
*/
#define ZYDIS_TOKEN_PARENTHESIS_OPEN 0x03
/**
* @brief A closing parenthesis character (like `')'`, `']'`, `'}'`).
*/
#define ZYDIS_TOKEN_PARENTHESIS_CLOSE 0x04
/**
* @brief A prefix literal (like `"LOCK"`, `"REP"`).
*/
#define ZYDIS_TOKEN_PREFIX 0x05
/**
* @brief A mnemonic literal (like `"MOV"`, `"VCMPPSD"`, `"LCALL"`).
*/
#define ZYDIS_TOKEN_MNEMONIC 0x06
/**
* @brief A register literal (like `"RAX"`, `"DS"`, `"%ECX"`).
*/
#define ZYDIS_TOKEN_REGISTER 0x07
/**
* @brief An absolute address literal (like `0x00400000`).
*/
#define ZYDIS_TOKEN_ADDRESS_ABS 0x08
/**
* @brief A relative address literal (like `-0x100`).
*/
#define ZYDIS_TOKEN_ADDRESS_REL 0x09
/**
* @brief A displacement literal (like `0xFFFFFFFF`, `-0x100`, `+0x1234`).
*/
#define ZYDIS_TOKEN_DISPLACEMENT 0x0A
/**
* @brief An immediate literal (like `0xC0`, `-0x1234`, `$0x0000`).
*/
#define ZYDIS_TOKEN_IMMEDIATE 0x0B
/**
* @brief A typecast literal (like `DWORD PTR`).
*/
#define ZYDIS_TOKEN_TYPECAST 0x0C
/**
* @brief A decorator literal (like `"Z"`, `"1TO4"`).
*/
#define ZYDIS_TOKEN_DECORATOR 0x0D
/**
* @brief A symbol literal.
*/
#define ZYDIS_TOKEN_SYMBOL 0x0E
/**
* @brief The base for user-defined token types.
*/
#define ZYDIS_TOKEN_USER 0x80
/* ---------------------------------------------------------------------------------------------- */
/* ============================================================================================== */
/* Enums and types */
/* ============================================================================================== */
/* ---------------------------------------------------------------------------------------------- */
/* Token */
/* ---------------------------------------------------------------------------------------------- */
#pragma pack(push, 1)
/**
* @brief Defines the `ZydisFormatterToken` struct.
*
* All fields in this struct should be considered as "private". Any changes may lead to unexpected
* behavior.
*/
typedef struct ZydisFormatterToken_
{
/**
* @brief The token type.
*/
ZydisTokenType type;
/**
* @brief An offset to the next token, or `0`.
*/
ZyanU8 next;
} ZydisFormatterToken;
#pragma pack(pop)
/**
* @brief Defines the `ZydisFormatterTokenConst` data-type.
*/
typedef const ZydisFormatterToken ZydisFormatterTokenConst;
/* ---------------------------------------------------------------------------------------------- */
/* Buffer */
/* ---------------------------------------------------------------------------------------------- */
/**
* @brief Defines the `ZydisFormatterBuffer` struct.
*
* All fields in this struct should be considered as "private". Any changes may lead to unexpected
* behavior.
*/
typedef struct ZydisFormatterBuffer_
{
/**
* @brief `ZYAN_TRUE`, if the buffer contains a token stream or `ZYAN_FALSE, if it contains
* a simple string.
*/
ZyanBool is_token_list;
/**
* @brief The remaining capacity of the buffer.
*/
ZyanUSize capacity;
/**
* @brief The `ZyanString` instance that refers to the literal value of the most recently
* added token.
*/
ZyanString string;
} ZydisFormatterBuffer;
/* ---------------------------------------------------------------------------------------------- */
/* ============================================================================================== */
/* Exported functions */
/* ============================================================================================== */
/* ---------------------------------------------------------------------------------------------- */
/* Token */
/* ---------------------------------------------------------------------------------------------- */
/**
* @brief Returns the `type` and the string `value` of the given `token`.
*
* @param token A pointer to the `ZydisFormatterToken` struct.
* @param type Receives the token type.
* @param value Receives a pointer to the string value of the token.
*
* @return A zyan status code.
*/
ZYDIS_EXPORT ZyanStatus ZydisFormatterTokenGetValue(const ZydisFormatterToken* token,
ZydisTokenType* type, ZyanConstCharPointer* value);
/**
* @brief Obtains the next `token` linked to the passed one.
*
* @param token Receives a pointer to the next `ZydisFormatterToken` struct linked to the
* passed one.
*
* @return A zyan status code.
*/
ZYDIS_EXPORT ZyanStatus ZydisFormatterTokenNext(ZydisFormatterTokenConst** token);
/* ---------------------------------------------------------------------------------------------- */
/* Buffer */
/* ---------------------------------------------------------------------------------------------- */
/**
* @brief Returns the current (most recently added) token.
*
* @param buffer A pointer to the `ZydisFormatterBuffer` struct.
* @param token Receives a pointer to the current token.
*
* @return A zyan status code.
*
* This function returns `ZYAN_STATUS_INVALID_OPERATION`, if the buffer does not contain at least
* one token.
*/
ZYDIS_EXPORT ZyanStatus ZydisFormatterBufferGetToken(const ZydisFormatterBuffer* buffer,
ZydisFormatterTokenConst** token);
/**
* @brief Returns the `ZyanString` instance associated with the given buffer.
*
* @param buffer A pointer to the `ZydisFormatterBuffer` struct.
* @param string Receives a pointer to the `ZyanString` instance associated with the given
* buffer.
*
* @return A zyan status code.
*
* This function returns `ZYAN_STATUS_INVALID_OPERATION`, if the buffer does not contain at least
* one token.
*
* The returned string always refers to the literal value of the current (most recently added)
* token and will remain valid until the buffer is destroyed.
*/
ZYDIS_EXPORT ZyanStatus ZydisFormatterBufferGetString(ZydisFormatterBuffer* buffer,
ZyanString** string);
/**
* @brief Appends a new token to the `buffer`.
*
* @param buffer A pointer to the `ZydisFormatterBuffer` struct.
* @param type The type of the new token.
*
* @return A zyan status code.
*
* Note that the `ZyanString` instance returned by `ZydisFormatterBufferGetString` will
* automatically be updated by calling this function.
*/
ZYDIS_EXPORT ZyanStatus ZydisFormatterBufferAppend(ZydisFormatterBuffer* buffer,
ZydisTokenType type);
/**
* @brief Returns a snapshot of the buffer-state.
*
* @param buffer A pointer to the `ZydisFormatterBuffer` struct.
* @param state Receives a snapshot of the buffer-state.
*
* @return A zyan status code.
*
* Note that the buffer-state is saved inside the buffer itself and thus becomes invalid as soon
* as the buffer gets overwritten or destroyed.
*/
ZYDIS_EXPORT ZyanStatus ZydisFormatterBufferRemember(const ZydisFormatterBuffer* buffer,
ZyanUPointer* state);
/**
* @brief Restores a previously saved buffer-state.
*
* @param buffer A pointer to the `ZydisFormatterBuffer` struct.
* @param state The buffer-state to restore.
*
* @return A zyan status code.
*
* All tokens added after obtaining the given `state` snapshot will be removed. This function
* does NOT restore any string content.
*
* Note that the `ZyanString` instance returned by `ZydisFormatterBufferGetString` will
* automatically be updated by calling this function.
*/
ZYDIS_EXPORT ZyanStatus ZydisFormatterBufferRestore(ZydisFormatterBuffer* buffer,
ZyanUPointer state);
/* ---------------------------------------------------------------------------------------------- */
/* ============================================================================================== */
#ifdef __cplusplus
}
#endif
#endif /* ZYDIS_FORMATTER_TOKEN_H */
|