zydis/include/Zydis/String.h

361 lines
14 KiB
C
Raw Normal View History

2017-12-02 03:40:56 +08:00
/***************************************************************************************************
Zyan Disassembler Library (Zydis)
2018-03-01 01:57:04 +08:00
Original Author : Florian Bernd, Joel Hoener
2017-12-02 03:40:56 +08:00
* 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.
***************************************************************************************************/
#ifndef ZYDIS_STRING_H
#define ZYDIS_STRING_H
#include <Zydis/CommonTypes.h>
#include <Zydis/Status.h>
#include <Zydis/Internal/LibC.h>
2017-12-02 03:40:56 +08:00
#ifdef __cplusplus
extern "C" {
#endif
2017-12-02 03:40:56 +08:00
/* ============================================================================================== */
/* Enums and types */
2017-12-02 03:40:56 +08:00
/* ============================================================================================== */
/* ---------------------------------------------------------------------------------------------- */
/* String */
/* ---------------------------------------------------------------------------------------------- */
/**
* @brief Defines the `ZydisString` struct.
*/
2018-03-01 01:15:18 +08:00
typedef struct ZydisString_
2017-12-02 03:40:56 +08:00
{
/**
* @brief The buffer that contains the actual string (0-termination is optional!).
*/
2018-02-27 04:57:53 +08:00
char* buffer;
/**
2017-12-03 06:46:05 +08:00
* @brief The length of the string (without 0-termination).
*/
2017-12-02 03:40:56 +08:00
ZydisUSize length;
/**
* @brief The total buffer capacity.
*/
ZydisUSize capacity;
2017-12-02 03:40:56 +08:00
} ZydisString;
2017-12-03 06:46:05 +08:00
/* ---------------------------------------------------------------------------------------------- */
/* Static string */
/* ---------------------------------------------------------------------------------------------- */
#pragma pack(push, 1)
/**
2018-03-01 01:15:18 +08:00
* @brief Defines the `ZydisStaticString` struct.
*
2017-12-03 06:46:05 +08:00
* This more compact struct is mainly used for internal string-tables to save up some bytes.
*/
typedef struct ZydisStaticString_
{
/**
* @brief The buffer that contains the actual string (0-termination is optional!).
*/
const char* buffer;
/**
* @brief The length of the string (without 0-termination).
*/
ZydisU8 length;
} ZydisStaticString;
#pragma pack(pop)
/* ---------------------------------------------------------------------------------------------- */
/* Letter Case */
/* ---------------------------------------------------------------------------------------------- */
/**
* @brief Defines the `ZydisLetterCase` datatype.
*/
typedef ZydisU8 ZydisLetterCase;
/**
* @brief Values that represent letter cases.
*/
enum ZydisLetterCases
{
/**
2017-12-03 00:50:34 +08:00
* @brief Uses the given text "as is".
*/
ZYDIS_LETTER_CASE_DEFAULT,
/**
* @brief Converts the given text to lowercase letters.
*/
ZYDIS_LETTER_CASE_LOWER,
/**
* @brief Converts the given text to uppercase letters.
*/
ZYDIS_LETTER_CASE_UPPER,
/**
* @brief Maximum value of this enum.
*/
ZYDIS_LETTER_CASE_MAX_VALUE = ZYDIS_LETTER_CASE_UPPER
};
/* ---------------------------------------------------------------------------------------------- */
/* ============================================================================================== */
/* Macros */
/* ============================================================================================== */
/* ---------------------------------------------------------------------------------------------- */
/* Helper Macros */
/* ---------------------------------------------------------------------------------------------- */
/**
2017-12-02 14:05:29 +08:00
* @brief Creates a `ZydisString` struct from a static C-string.
2018-03-01 01:15:18 +08:00
*
* @param string The C-string constant.
*/
#define ZYDIS_MAKE_STRING(string) \
{ (char*)string, sizeof(string) - 1, sizeof(string) - 1 }
2017-12-03 06:46:05 +08:00
/**
* @brief Creates a `ZydisStaticString` from a static C-string.
2018-03-01 01:15:18 +08:00
*
* @param string The C-string constant.
2017-12-03 06:46:05 +08:00
*/
#define ZYDIS_MAKE_STATIC_STRING(string) \
{ string, sizeof(string) - 1 }
/* ---------------------------------------------------------------------------------------------- */
/* ============================================================================================== */
/* Functions */
/* ============================================================================================== */
/* ---------------------------------------------------------------------------------------------- */
/* Basic Operations */
/* ---------------------------------------------------------------------------------------------- */
/**
* @brief Initializes a `ZydisString` struct with a C-string.
2018-03-01 01:15:18 +08:00
*
* @param string The string to initialize.
2018-03-01 01:15:18 +08:00
* @param text The C-string constant.
*
* @return A zydis status code.
*/
ZYDIS_EXPORT ZydisStatus ZydisStringInit(ZydisString* string, char* text);
2017-12-03 06:46:05 +08:00
/**
* @brief Finalizes a `ZydisString` struct by adding a terminating zero byte.
2018-03-01 01:15:18 +08:00
*
2017-12-03 06:46:05 +08:00
* @param string The string to finalize.
2018-03-01 01:15:18 +08:00
*
2017-12-03 06:46:05 +08:00
* @return A zydis status code.
*/
ZYDIS_EXPORT ZydisStatus ZydisStringFinalize(ZydisString* string);
2017-12-03 06:46:05 +08:00
/* ---------------------------------------------------------------------------------------------- */
/**
* @brief Appends a `ZydisString` to another `ZydisString`.
*
* @param string The string to append to.
* @param text The string to append.
*
2018-03-01 01:15:18 +08:00
* @return @c ZYDIS_STATUS_SUCCESS, if the function succeeded, or
* @c ZYDIS_STATUS_INSUFFICIENT_BUFFER_SIZE, if the size of the buffer was not
* sufficient to append the given @c text.
*/
ZYDIS_EXPORT ZydisStatus ZydisStringAppend(ZydisString* string, const ZydisString* text);
/**
* @brief Appends a `ZydisString` to another `ZydisString`, converting it to the specified
* letter-case.
*
* @param string The string to append to.
* @param text The string to append.
* @param letterCase The letter case to use.
*
2018-03-01 01:15:18 +08:00
* @return @c ZYDIS_STATUS_SUCCESS, if the function succeeded, or
* @c ZYDIS_STATUS_INSUFFICIENT_BUFFER_SIZE, if the size of the buffer was not
* sufficient to append the given @c text.
*/
ZYDIS_EXPORT ZydisStatus ZydisStringAppendEx(ZydisString* string, const ZydisString* text,
ZydisLetterCase letterCase);
2017-12-03 06:46:05 +08:00
/**
* @brief Appends the given C-string to a `ZydisString`.
2017-12-03 06:46:05 +08:00
*
* @param string The string to append to.
* @param text The C-string to append.
2017-12-03 06:46:05 +08:00
*
2018-03-01 01:15:18 +08:00
* @return @c ZYDIS_STATUS_SUCCESS, if the function succeeded, or
* @c ZYDIS_STATUS_INSUFFICIENT_BUFFER_SIZE, if the size of the buffer was not
2017-12-03 06:46:05 +08:00
* sufficient to append the given @c text.
*/
ZYDIS_EXPORT ZydisStatus ZydisStringAppendC(ZydisString* string, const char* text);
2017-12-03 06:46:05 +08:00
/**
* @brief Appends the given C-string to a `ZydisString`, converting it to the specified
* letter-case.
*
* @param string The string to append to.
* @param text The C-string to append.
* @param letterCase The letter case to use.
*
2018-03-01 01:15:18 +08:00
* @return @c ZYDIS_STATUS_SUCCESS, if the function succeeded, or
* @c ZYDIS_STATUS_INSUFFICIENT_BUFFER_SIZE, if the size of the buffer was not
* sufficient to append the given @c text.
*/
2018-03-01 01:15:18 +08:00
ZYDIS_EXPORT ZydisStatus ZydisStringAppendExC(ZydisString* string, const char* text,
ZydisLetterCase letterCase);
/**
* @brief Appends the given 'ZydisStaticString' to a `ZydisString`.
*
* @param string The string to append to.
* @param text The static-string to append.
*
2018-03-01 01:15:18 +08:00
* @return @c ZYDIS_STATUS_SUCCESS, if the function succeeded, or
* @c ZYDIS_STATUS_INSUFFICIENT_BUFFER_SIZE, if the size of the buffer was not
* sufficient to append the given @c text.
*/
2018-03-01 01:15:18 +08:00
ZYDIS_EXPORT ZydisStatus ZydisStringAppendStatic(ZydisString* string,
const ZydisStaticString* text, ZydisLetterCase letterCase);
2017-12-03 06:46:05 +08:00
/**
2018-03-01 01:15:18 +08:00
* @brief Appends the given 'ZydisStaticString' to a `ZydisString`, converting it to the
* specified letter-case.
2017-12-03 06:46:05 +08:00
*
* @param string The string to append to.
* @param text The static-string to append.
* @param letterCase The letter case to use.
2017-12-03 06:46:05 +08:00
*
2018-03-01 01:15:18 +08:00
* @return @c ZYDIS_STATUS_SUCCESS, if the function succeeded, or
* @c ZYDIS_STATUS_INSUFFICIENT_BUFFER_SIZE, if the size of the buffer was not
2017-12-03 06:46:05 +08:00
* sufficient to append the given @c text.
*/
2018-03-01 01:15:18 +08:00
ZYDIS_EXPORT ZydisStatus ZydisStringAppendExStatic(ZydisString* string,
const ZydisStaticString* text, ZydisLetterCase letterCase);
2017-12-03 06:46:05 +08:00
/* ---------------------------------------------------------------------------------------------- */
/* Formatting */
/* ---------------------------------------------------------------------------------------------- */
/**
2018-03-01 01:15:18 +08:00
* @brief Formats the given unsigned ordinal @c value to its decimal text-representation and
* appends it to the @c string.
*
* @param string A pointer to the string.
* @param value The value.
* @param paddingLength Padds the converted value with leading zeros, if the number of chars is
* less than the @c paddingLength.
*
2018-03-01 01:15:18 +08:00
* @return @c ZYDIS_STATUS_SUCCESS, if the function succeeded, or
* @c ZYDIS_STATUS_INSUFFICIENT_BUFFER_SIZE, if the size of the buffer was not
* sufficient to append the given @c value.
2018-03-01 01:15:18 +08:00
*
* The string-buffer pointer is increased by the number of chars written, if the call was
* successfull.
*/
2018-03-01 01:15:18 +08:00
ZYDIS_EXPORT ZydisStatus ZydisStringAppendDecU(ZydisString* string, ZydisU64 value,
ZydisU8 paddingLength);
/**
2018-03-01 01:15:18 +08:00
* @brief Formats the given signed ordinal @c value to its decimal text-representation and
* appends it to the @c string.
*
* @param string A pointer to the string.
* @param value The value.
* @param paddingLength Padds the converted value with leading zeros, if the number of chars is
* less than the @c paddingLength (the sign char is ignored).
*
2018-03-01 01:15:18 +08:00
* @return @c ZYDIS_STATUS_SUCCESS, if the function succeeded, or
* @c ZYDIS_STATUS_INSUFFICIENT_BUFFER_SIZE, if the size of the buffer was not
* sufficient to append the given @c value.
2018-03-01 01:15:18 +08:00
*
* The string-buffer pointer is increased by the number of chars written, if the call was
* successfull.
*/
2018-03-01 01:15:18 +08:00
ZYDIS_EXPORT ZydisStatus ZydisStringAppendDecS(ZydisString* string, ZydisI64 value,
ZydisU8 paddingLength);
/**
2018-03-01 01:15:18 +08:00
* @brief Formats the given unsigned ordinal @c value to its hexadecimal text-representation and
* appends it to the @c string.
*
* @param string A pointer to the string.
* @param value The value.
* @param paddingLength Padds the converted value with leading zeros, if the number of chars is
* less than the @c paddingLength.
2018-03-01 01:15:18 +08:00
* @param uppercase Set @c TRUE to print the hexadecimal value in uppercase letters instead
* of lowercase ones.
* @param prefix The string to use as prefix or `NULL`, if not needed.
* @param suffix The string to use as suffix or `NULL`, if not needed.
*
2018-03-01 01:15:18 +08:00
* @return @c ZYDIS_STATUS_SUCCESS, if the function succeeded, or
* @c ZYDIS_STATUS_INSUFFICIENT_BUFFER_SIZE, if the size of the buffer was not
* sufficient to append the given @c value.
2018-03-01 01:15:18 +08:00
*
* The string-buffer pointer is increased by the number of chars written, if the call was
* successfull.
*/
ZYDIS_EXPORT ZydisStatus ZydisStringAppendHexU(ZydisString* string, ZydisU64 value,
2018-03-01 01:15:18 +08:00
ZydisU8 paddingLength, ZydisBool uppercase, const ZydisString* prefix,
const ZydisString* suffix);
/**
2018-03-01 01:15:18 +08:00
* @brief Formats the given signed ordinal @c value to its hexadecimal text-representation and
* appends it to the @c string.
*
* @param string A pointer to the string.
* @param value The value.
* @param paddingLength Padds the converted value with leading zeros, if the number of chars is
* less than the @c paddingLength (the sign char is ignored).
2018-03-01 01:15:18 +08:00
* @param uppercase Set @c TRUE to print the hexadecimal value in uppercase letters instead
* of lowercase ones.
* @param prefix The string to use as prefix or `NULL`, if not needed.
* @param suffix The string to use as suffix or `NULL`, if not needed.
*
2018-03-01 01:15:18 +08:00
* @return @c ZYDIS_STATUS_SUCCESS, if the function succeeded, or
* @c ZYDIS_STATUS_INSUFFICIENT_BUFFER_SIZE, if the size of the buffer was not
* sufficient to append the given @c value.
2018-03-01 01:15:18 +08:00
*
* The string-buffer pointer is increased by the number of chars written, if the call was
* successfull.
*/
2018-03-01 01:15:18 +08:00
ZYDIS_EXPORT ZydisStatus ZydisStringAppendHexS(ZydisString* string, ZydisI64 value,
ZydisU8 paddingLength, ZydisBool uppercase, const ZydisString* prefix,
const ZydisString* suffix);
/* ---------------------------------------------------------------------------------------------- */
2017-12-02 03:40:56 +08:00
/* ============================================================================================== */
#ifdef __cplusplus
}
#endif
2017-12-02 03:40:56 +08:00
#endif // ZYDIS_STRING_H