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 307 308 309 310 311 312 313 314 315 316 317 318 319
|
/***************************************************************************************************
Zyan Disassembler Library (Zydis)
Original Author : Florian Bernd, Joel Hoener
* 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
* Provides formatter functions that are shared between the different formatters.
*/
#ifndef ZYDIS_FORMATTER_BASE_H
#define ZYDIS_FORMATTER_BASE_H
#include "ZydisFormatter.h"
#include "ZydisInternalString.h"
#include <wtf/Compiler.h>
WTF_ALLOW_UNSAFE_BUFFER_USAGE_BEGIN
#ifdef __cplusplus
extern "C" {
#endif
/* ============================================================================================== */
/* Macros */
/* ============================================================================================== */
/* ---------------------------------------------------------------------------------------------- */
/* String */
/* ---------------------------------------------------------------------------------------------- */
/**
* Appends an unsigned numeric value to the given string.
*
* @param formatter A pointer to the `ZydisFormatter` instance.
* @param base The numeric base.
* @param str The destination string.
* @param value The value.
* @param padding_length The padding length.
*/
#define ZYDIS_STRING_APPEND_NUM_U(formatter, base, str, value, padding_length) \
switch (base) \
{ \
case ZYDIS_NUMERIC_BASE_DEC: \
ZYAN_CHECK(ZydisStringAppendDecU(str, value, padding_length, \
(formatter)->number_format[base][0].string, \
(formatter)->number_format[base][1].string)); \
break; \
case ZYDIS_NUMERIC_BASE_HEX: \
ZYAN_CHECK(ZydisStringAppendHexU(str, value, padding_length, \
(formatter)->hex_uppercase, \
(formatter)->number_format[base][0].string, \
(formatter)->number_format[base][1].string)); \
break; \
default: \
return ZYAN_STATUS_INVALID_ARGUMENT; \
}
/**
* Appends a signed numeric value to the given string.
*
* @param formatter A pointer to the `ZydisFormatter` instance.
* @param base The numeric base.
* @param str The destination string.
* @param value The value.
* @param padding_length The padding length.
* @param force_sign Forces printing of the '+' sign for positive numbers.
*/
#define ZYDIS_STRING_APPEND_NUM_S(formatter, base, str, value, padding_length, force_sign) \
switch (base) \
{ \
case ZYDIS_NUMERIC_BASE_DEC: \
ZYAN_CHECK(ZydisStringAppendDecS(str, value, padding_length, force_sign, \
(formatter)->number_format[base][0].string, \
(formatter)->number_format[base][1].string)); \
break; \
case ZYDIS_NUMERIC_BASE_HEX: \
ZYAN_CHECK(ZydisStringAppendHexS(str, value, padding_length, \
(formatter)->hex_uppercase, force_sign, \
(formatter)->number_format[base][0].string, \
(formatter)->number_format[base][1].string)); \
break; \
default: \
return ZYAN_STATUS_INVALID_ARGUMENT; \
}
/* ---------------------------------------------------------------------------------------------- */
/* Buffer */
/* ---------------------------------------------------------------------------------------------- */
/**
* Invokes the `ZydisFormatterBufferAppend` routine, if tokenization is enabled for the
* current pass.
*
* @param buffer A pointer to the `ZydisFormatterBuffer` struct.
* @param type The token type.
*
* Using this macro instead of direct calls to `ZydisFormatterBufferAppend` greatly improves the
* performance for non-tokenizing passes.
*/
#define ZYDIS_BUFFER_APPEND_TOKEN(buffer, type) \
if ((buffer)->is_token_list) \
{ \
ZYAN_CHECK(ZydisFormatterBufferAppend(buffer, type)); \
}
/**
* Returns a snapshot of the buffer-state.
*
* @param buffer A pointer to the `ZydisFormatterBuffer` struct.
* @param state Receives a snapshot of the buffer-state.
*
* Using this macro instead of direct calls to `ZydisFormatterBufferRemember` improves the
* performance for non-tokenizing passes.
*/
#define ZYDIS_BUFFER_REMEMBER(buffer, state) \
if ((buffer)->is_token_list) \
{ \
(state) = (ZyanUPointer)(buffer)->string.vector.data; \
} else \
{ \
(state) = (ZyanUPointer)(buffer)->string.vector.size; \
}
/**
* Appends a string (`STR_`-prefix) or a predefined token-list (`TOK_`-prefix).
*
* @param buffer A pointer to the `ZydisFormatterBuffer` struct.
* @param name The base name (without prefix) of the string- or token.
*/
#define ZYDIS_BUFFER_APPEND(buffer, name) \
if ((buffer)->is_token_list) \
{ \
ZYAN_CHECK(ZydisFormatterBufferAppendPredefined(buffer, TOK_ ## name)); \
} else \
{ \
ZYAN_CHECK(ZydisStringAppendShort(&buffer->string, &STR_ ## name)); \
}
// TODO: Implement `letter_case` for predefined tokens
/**
* Appends a string (`STR_`-prefix) or a predefined token-list (`TOK_`-prefix).
*
* @param buffer A pointer to the `ZydisFormatterBuffer` struct.
* @param name The base name (without prefix) of the string- or token.
* @param letter-case The desired letter-case.
*/
#define ZYDIS_BUFFER_APPEND_CASE(buffer, name, letter_case) \
if ((buffer)->is_token_list) \
{ \
ZYAN_CHECK(ZydisFormatterBufferAppendPredefined(buffer, TOK_ ## name)); \
} else \
{ \
ZYAN_CHECK(ZydisStringAppendShortCase(&buffer->string, &STR_ ## name, letter_case)); \
}
/* ---------------------------------------------------------------------------------------------- */
/* ============================================================================================== */
/* Helper functions */
/* ============================================================================================== */
/* ---------------------------------------------------------------------------------------------- */
/* Buffer */
/* ---------------------------------------------------------------------------------------------- */
// MSVC does not like the C99 flexible-array extension
#ifdef ZYAN_MSVC
# pragma warning(push)
# pragma warning(disable:4200)
#endif
typedef struct ZydisPredefinedToken_
{
ZyanU8 size;
ZyanU8 next;
ZyanU8 data[];
} ZydisPredefinedToken;
#ifdef ZYAN_MSVC
# pragma warning(pop)
#endif
/**
* Appends a predefined token-list to the `buffer`.
*
* @param buffer A pointer to the `ZydisFormatterBuffer` struct.
* @param data A pointer to the `ZydisPredefinedToken` struct.
*
* @return A zycore status code.
*
* This function is internally used to improve performance while adding static strings or multiple
* tokens at once.
*/
ZYAN_INLINE ZyanStatus ZydisFormatterBufferAppendPredefined(ZydisFormatterBuffer* buffer,
const ZydisPredefinedToken* data)
{
ZYAN_ASSERT(buffer);
ZYAN_ASSERT(data);
const ZyanUSize len = buffer->string.vector.size;
ZYAN_ASSERT((len > 0) && (len < 256));
if (buffer->capacity <= len + data->size)
{
return ZYAN_STATUS_INSUFFICIENT_BUFFER_SIZE;
}
ZydisFormatterToken* const last = (ZydisFormatterToken*)buffer->string.vector.data - 1;
last->next = (ZyanU8)len;
ZYAN_MEMCPY((ZyanU8*)buffer->string.vector.data + len, &data->data[0], data->size);
const ZyanUSize delta = len + data->next;
buffer->capacity -= delta;
buffer->string.vector.data = (ZyanU8*)buffer->string.vector.data + delta;
buffer->string.vector.size = data->size - data->next;
buffer->string.vector.capacity = ZYAN_MIN(buffer->capacity, 255);
return ZYAN_STATUS_SUCCESS;
}
/* ---------------------------------------------------------------------------------------------- */
/* General */
/* ---------------------------------------------------------------------------------------------- */
/**
* Returns the size to be used as explicit size suffix (`AT&T`) or explicit typecast
* (`INTEL`), if required.
*
* @param formatter A pointer to the `ZydisFormatter` instance.
* @param context A pointer to the `ZydisFormatterContext` struct.
* @param memop_id The operand-id of the instructions first memory operand.
*
* @return Returns the explicit size, if required, or `0`, if not needed.
*
* This function always returns a size different to `0`, if the `ZYDIS_FORMATTER_PROP_FORCE_SIZE`
* is set to `ZYAN_TRUE`.
*/
ZyanU32 ZydisFormatterHelperGetExplicitSize(const ZydisFormatter* formatter,
ZydisFormatterContext* context, ZyanU8 memop_id);
/* ---------------------------------------------------------------------------------------------- */
/* ============================================================================================== */
/* Formatter functions */
/* ============================================================================================== */
/* ---------------------------------------------------------------------------------------------- */
/* Operands */
/* ---------------------------------------------------------------------------------------------- */
ZyanStatus ZydisFormatterBaseFormatOperandREG(const ZydisFormatter* formatter,
ZydisFormatterBuffer* buffer, ZydisFormatterContext* context);
ZyanStatus ZydisFormatterBaseFormatOperandPTR(const ZydisFormatter* formatter,
ZydisFormatterBuffer* buffer, ZydisFormatterContext* context);
ZyanStatus ZydisFormatterBaseFormatOperandIMM(const ZydisFormatter* formatter,
ZydisFormatterBuffer* buffer, ZydisFormatterContext* context);
/* ---------------------------------------------------------------------------------------------- */
/* Elemental tokens */
/* ---------------------------------------------------------------------------------------------- */
ZyanStatus ZydisFormatterBasePrintAddressABS(const ZydisFormatter* formatter,
ZydisFormatterBuffer* buffer, ZydisFormatterContext* context);
ZyanStatus ZydisFormatterBasePrintAddressREL(const ZydisFormatter* formatter,
ZydisFormatterBuffer* buffer, ZydisFormatterContext* context);
ZyanStatus ZydisFormatterBasePrintIMM(const ZydisFormatter* formatter,
ZydisFormatterBuffer* buffer, ZydisFormatterContext* context);
/* ---------------------------------------------------------------------------------------------- */
/* Optional tokens */
/* ---------------------------------------------------------------------------------------------- */
ZyanStatus ZydisFormatterBasePrintSegment(const ZydisFormatter* formatter,
ZydisFormatterBuffer* buffer, ZydisFormatterContext* context);
ZyanStatus ZydisFormatterBasePrintPrefixes(const ZydisFormatter* formatter,
ZydisFormatterBuffer* buffer, ZydisFormatterContext* context);
ZyanStatus ZydisFormatterBasePrintDecorator(const ZydisFormatter* formatter,
ZydisFormatterBuffer* buffer, ZydisFormatterContext* context, ZydisDecorator decorator);
/* ---------------------------------------------------------------------------------------------- */
/* ============================================================================================== */
#ifdef __cplusplus
}
#endif
WTF_ALLOW_UNSAFE_BUFFER_USAGE_END
#endif // ZYDIS_FORMATTER_BASE_H
|