Simplified custom print-functions and fixed some bugs

This commit is contained in:
flobernd 2017-09-14 02:59:20 +02:00
parent 41e943c34c
commit 9fe5d66380
3 changed files with 166 additions and 398 deletions

View File

@ -45,19 +45,17 @@
/* Lookup Tables */ /* Lookup Tables */
/* ---------------------------------------------------------------------------------------------- */ /* ---------------------------------------------------------------------------------------------- */
static uint16_t const decimalLookup[100] = static const char* decimalLookup =
{ "00010203040506070809"
0x3030, 0x3130, 0x3230, 0x3330, 0x3430, 0x3530, 0x3630, 0x3730, 0x3830, 0x3930, "10111213141516171819"
0x3031, 0x3131, 0x3231, 0x3331, 0x3431, 0x3531, 0x3631, 0x3731, 0x3831, 0x3931, "20212223242526272829"
0x3032, 0x3132, 0x3232, 0x3332, 0x3432, 0x3532, 0x3632, 0x3732, 0x3832, 0x3932, "30313233343536373839"
0x3033, 0x3133, 0x3233, 0x3333, 0x3433, 0x3533, 0x3633, 0x3733, 0x3833, 0x3933, "40414243444546474849"
0x3034, 0x3134, 0x3234, 0x3334, 0x3434, 0x3534, 0x3634, 0x3734, 0x3834, 0x3934, "50515253545556575859"
0x3035, 0x3135, 0x3235, 0x3335, 0x3435, 0x3535, 0x3635, 0x3735, 0x3835, 0x3935, "60616263646566676869"
0x3036, 0x3136, 0x3236, 0x3336, 0x3436, 0x3536, 0x3636, 0x3736, 0x3836, 0x3936, "70717273747576777879"
0x3037, 0x3137, 0x3237, 0x3337, 0x3437, 0x3537, 0x3637, 0x3737, 0x3837, 0x3937, "80818283848586878889"
0x3038, 0x3138, 0x3238, 0x3338, 0x3438, 0x3538, 0x3638, 0x3738, 0x3838, 0x3938, "90919293949596979899";
0x3039, 0x3139, 0x3239, 0x3339, 0x3439, 0x3539, 0x3639, 0x3739, 0x3839, 0x3939
};
/* ---------------------------------------------------------------------------------------------- */ /* ---------------------------------------------------------------------------------------------- */
@ -66,51 +64,14 @@ static uint16_t const decimalLookup[100] =
/* ============================================================================================== */ /* ============================================================================================== */
/* ---------------------------------------------------------------------------------------------- */ /* ---------------------------------------------------------------------------------------------- */
/* Public Functions */ /* Internal Functions */
/* ---------------------------------------------------------------------------------------------- */ /* ---------------------------------------------------------------------------------------------- */
ZydisStatus ZydisPrintStr(char** buffer, size_t bufferLen, const char* text, #ifdef ZYDIS_X86
ZydisLetterCase letterCase) ZydisStatus ZydisPrintDecU32(char** buffer, size_t bufferLen, uint32_t value, uint8_t paddingLength)
{ {
ZYDIS_ASSERT(buffer); ZYDIS_ASSERT(buffer);
ZYDIS_ASSERT(bufferLen > 0); ZYDIS_ASSERT(bufferLen > 0);
ZYDIS_ASSERT(text);
size_t strLen = strlen(text);
if (strLen >= bufferLen)
{
return ZYDIS_STATUS_INSUFFICIENT_BUFFER_SIZE;
}
memcpy(*buffer, text, strLen + 1);
switch (letterCase)
{
case ZYDIS_LETTER_CASE_LOWER:
for (size_t i = 0; i < strLen; ++i)
{
(*buffer[i]) = (char)tolower((*buffer)[i]);
}
break;
case ZYDIS_LETTER_CASE_UPPER:
for (size_t i = 0; i < strLen; ++i)
{
(*buffer)[i] = (char)toupper((*buffer)[i]);
}
break;
default:
break;
}
*buffer += strLen;
return ZYDIS_STATUS_SUCCESS;
}
ZydisStatus ZydisPrintDec32U(char** buffer, size_t bufferLen, uint32_t value, uint8_t paddingLength)
{
#ifdef ZYDIS_X64
return ZydisPrintDec64U(buffer, bufferLen, value, paddingLength);
#else
ZYDIS_ASSERT(buffer);
ZYDIS_ASSERT(bufferLen > 0);
char temp[ZYDIS_MAXCHARS_DEC_32 + 1]; char temp[ZYDIS_MAXCHARS_DEC_32 + 1];
char *p = &temp[ZYDIS_MAXCHARS_DEC_32]; char *p = &temp[ZYDIS_MAXCHARS_DEC_32];
@ -120,13 +81,13 @@ ZydisStatus ZydisPrintDec32U(char** buffer, size_t bufferLen, uint32_t value, ui
uint32_t const old = value; uint32_t const old = value;
p -= 2; p -= 2;
value /= 100; value /= 100;
memcpy(p, &decimalLookup[old - (value * 100)], sizeof(uint16_t)); memcpy(p, &decimalLookup[(old - (value * 100)) * 2], sizeof(uint16_t));
} }
p -= 2; p -= 2;
memcpy(p, &decimalLookup[value], sizeof(uint16_t)); memcpy(p, &decimalLookup[value * 2], sizeof(uint16_t));
size_t n = &temp[ZYDIS_MAXCHARS_DEC_32] - p; size_t n = &temp[ZYDIS_MAXCHARS_DEC_32] - p;
if ((bufferLen < n + 1) || (bufferLen < paddingLength + 1)) if ((bufferLen < (size_t)(n + 1)) || (bufferLen < (size_t)(paddingLength + 1)))
{ {
return ZYDIS_STATUS_INSUFFICIENT_BUFFER_SIZE; return ZYDIS_STATUS_INSUFFICIENT_BUFFER_SIZE;
} }
@ -135,99 +96,18 @@ ZydisStatus ZydisPrintDec32U(char** buffer, size_t bufferLen, uint32_t value, ui
if (n <= paddingLength) if (n <= paddingLength)
{ {
offset = paddingLength - n + 1; offset = paddingLength - n + 1;
memset((*buffer), '0', offset); memset(*buffer, '0', offset);
} }
memcpy(&(*buffer)[offset], &p[value < 10], n + 1); memcpy(&(*buffer)[offset], &p[value < 10], n + 1);
*buffer += n + offset - 1; *buffer += n + offset - (uint8_t)(value < 10);
return ZYDIS_STATUS_SUCCESS;
#endif
}
ZydisStatus ZydisPrintDec64U(char** buffer, size_t bufferLen, uint64_t value, uint8_t paddingLength)
{
ZYDIS_ASSERT(buffer);
ZYDIS_ASSERT(bufferLen > 0);
char temp[ZYDIS_MAXCHARS_DEC_64 + 1];
char *p = &temp[ZYDIS_MAXCHARS_DEC_64];
*p = '\0';
while(value >= 100)
{
uint64_t const old = value;
p -= 2;
value /= 100;
memcpy(p, &decimalLookup[old - (value * 100)], sizeof(uint16_t));
}
p -= 2;
memcpy(p, &decimalLookup[value], sizeof(uint16_t));
size_t n = &temp[ZYDIS_MAXCHARS_DEC_64] - p;
if ((bufferLen < n + 1) || (bufferLen < paddingLength + 1))
{
return ZYDIS_STATUS_INSUFFICIENT_BUFFER_SIZE;
}
uintptr_t offset = 0;
if (n <= paddingLength)
{
offset = paddingLength - n + 1;
memset((*buffer), '0', offset);
}
memcpy(&(*buffer)[offset], &p[value < 20], n + 1);
*buffer += n + offset - 1;
return ZYDIS_STATUS_SUCCESS; return ZYDIS_STATUS_SUCCESS;
} }
ZydisStatus ZydisPrintDec8S(char** buffer, size_t bufferLen, int8_t value, uint8_t paddingLength) ZydisStatus ZydisPrintHexU32(char** buffer, size_t bufferLen, uint32_t value, uint8_t paddingLength,
{
if (value < 0)
{
ZYDIS_CHECK(ZydisPrintStr(buffer, bufferLen, "-", ZYDIS_LETTER_CASE_DEFAULT));
return ZydisPrintDec32U(buffer, bufferLen - 1, -value, paddingLength);
}
return ZydisPrintDec32U(buffer, bufferLen, value, paddingLength);
}
ZydisStatus ZydisPrintDec16S(char** buffer, size_t bufferLen, int16_t value, uint8_t paddingLength)
{
if (value < 0)
{
ZYDIS_CHECK(ZydisPrintStr(buffer, bufferLen, "-", ZYDIS_LETTER_CASE_DEFAULT));
return ZydisPrintDec32U(buffer, bufferLen - 1, -value, paddingLength);
}
return ZydisPrintDec32U(buffer, bufferLen, value, paddingLength);
}
ZydisStatus ZydisPrintDec32S(char** buffer, size_t bufferLen, int32_t value, uint8_t paddingLength)
{
if (value < 0)
{
ZYDIS_CHECK(ZydisPrintStr(buffer, bufferLen, "-", ZYDIS_LETTER_CASE_DEFAULT));
return ZydisPrintDec32U(buffer, bufferLen - 1, -value, paddingLength);
}
return ZydisPrintDec32U(buffer, bufferLen, value, paddingLength);
}
ZydisStatus ZydisPrintDec64S(char** buffer, size_t bufferLen, int64_t value, uint8_t paddingLength)
{
if (value < 0)
{
ZYDIS_CHECK(ZydisPrintStr(buffer, bufferLen, "-", ZYDIS_LETTER_CASE_DEFAULT));
return ZydisPrintDec64U(buffer, bufferLen - 1, -value, paddingLength);
}
return ZydisPrintDec64U(buffer, bufferLen, value, paddingLength);
}
ZydisStatus ZydisPrintHex32U(char** buffer, size_t bufferLen, uint32_t value, uint8_t paddingLength,
ZydisBool uppercase, ZydisBool prefix) ZydisBool uppercase, ZydisBool prefix)
{ {
#ifdef ZYDIS_X64
return ZydisPrintHex64U(buffer, bufferLen, value, paddingLength, uppercase, prefix);
#else
ZYDIS_ASSERT(buffer); ZYDIS_ASSERT(buffer);
ZYDIS_ASSERT(bufferLen); ZYDIS_ASSERT(bufferLen);
@ -236,7 +116,7 @@ ZydisStatus ZydisPrintHex32U(char** buffer, size_t bufferLen, uint32_t value, ui
ZYDIS_CHECK(ZydisPrintStr(buffer, bufferLen, "0x", ZYDIS_LETTER_CASE_DEFAULT)); ZYDIS_CHECK(ZydisPrintStr(buffer, bufferLen, "0x", ZYDIS_LETTER_CASE_DEFAULT));
bufferLen -= 2; bufferLen -= 2;
} }
if (bufferLen < paddingLength + 1) if (bufferLen < (size_t)(paddingLength + 1))
{ {
return ZYDIS_STATUS_INSUFFICIENT_BUFFER_SIZE; return ZYDIS_STATUS_INSUFFICIENT_BUFFER_SIZE;
} }
@ -245,12 +125,12 @@ ZydisStatus ZydisPrintHex32U(char** buffer, size_t bufferLen, uint32_t value, ui
{ {
uint8_t n = (paddingLength ? paddingLength : 1); uint8_t n = (paddingLength ? paddingLength : 1);
if (bufferLen < n + 1) if (bufferLen < (size_t)(n + 1))
{ {
return ZYDIS_STATUS_INSUFFICIENT_BUFFER_SIZE; return ZYDIS_STATUS_INSUFFICIENT_BUFFER_SIZE;
} }
memset((*buffer), '0', n); memset(*buffer, '0', n);
(*buffer)[n] = '\0'; (*buffer)[n] = '\0';
*buffer += n; *buffer += n;
return ZYDIS_STATUS_SUCCESS; return ZYDIS_STATUS_SUCCESS;
@ -293,10 +173,47 @@ ZydisStatus ZydisPrintHex32U(char** buffer, size_t bufferLen, uint32_t value, ui
*buffer += n + offset; *buffer += n + offset;
return ZYDIS_STATUS_SUCCESS; return ZYDIS_STATUS_SUCCESS;
}
#endif #endif
ZydisStatus ZydisPrintDecU64(char** buffer, size_t bufferLen, uint64_t value, uint8_t paddingLength)
{
ZYDIS_ASSERT(buffer);
ZYDIS_ASSERT(bufferLen > 0);
char temp[ZYDIS_MAXCHARS_DEC_64 + 1];
char *p = &temp[ZYDIS_MAXCHARS_DEC_64];
*p = '\0';
while(value >= 100)
{
uint64_t const old = value;
p -= 2;
value /= 100;
memcpy(p, &decimalLookup[(old - (value * 100)) * 2], 2);
}
p -= 2;
memcpy(p, &decimalLookup[value * 2], 2);
size_t n = &temp[ZYDIS_MAXCHARS_DEC_64] - p;
if ((bufferLen < (size_t)(n + 1)) || (bufferLen < (size_t)(paddingLength + 1)))
{
return ZYDIS_STATUS_INSUFFICIENT_BUFFER_SIZE;
}
uintptr_t offset = 0;
if (n <= paddingLength)
{
offset = paddingLength - n + 1;
memset(*buffer, '0', offset);
}
memcpy(&(*buffer)[offset], &p[value < 10], n + 1);
*buffer += n + offset - (uint8_t)(value < 10);
return ZYDIS_STATUS_SUCCESS;
} }
ZydisStatus ZydisPrintHex64U(char** buffer, size_t bufferLen, uint64_t value, uint8_t paddingLength, ZydisStatus ZydisPrintHexU64(char** buffer, size_t bufferLen, uint64_t value, uint8_t paddingLength,
ZydisBool uppercase, ZydisBool prefix) ZydisBool uppercase, ZydisBool prefix)
{ {
ZYDIS_ASSERT(buffer); ZYDIS_ASSERT(buffer);
@ -307,7 +224,7 @@ ZydisStatus ZydisPrintHex64U(char** buffer, size_t bufferLen, uint64_t value, ui
ZYDIS_CHECK(ZydisPrintStr(buffer, bufferLen, "0x", ZYDIS_LETTER_CASE_DEFAULT)); ZYDIS_CHECK(ZydisPrintStr(buffer, bufferLen, "0x", ZYDIS_LETTER_CASE_DEFAULT));
bufferLen -= 2; bufferLen -= 2;
} }
if (bufferLen < paddingLength + 1) if (bufferLen < (size_t)(paddingLength + 1))
{ {
return ZYDIS_STATUS_INSUFFICIENT_BUFFER_SIZE; return ZYDIS_STATUS_INSUFFICIENT_BUFFER_SIZE;
} }
@ -316,12 +233,12 @@ ZydisStatus ZydisPrintHex64U(char** buffer, size_t bufferLen, uint64_t value, ui
{ {
uint8_t n = (paddingLength ? paddingLength : 1); uint8_t n = (paddingLength ? paddingLength : 1);
if (bufferLen < n + 1) if (bufferLen < (size_t)(n + 1))
{ {
return ZYDIS_STATUS_INSUFFICIENT_BUFFER_SIZE; return ZYDIS_STATUS_INSUFFICIENT_BUFFER_SIZE;
} }
memset((*buffer), '0', n); memset(*buffer, '0', n);
(*buffer)[n] = '\0'; (*buffer)[n] = '\0';
*buffer += n; *buffer += n;
return ZYDIS_STATUS_SUCCESS; return ZYDIS_STATUS_SUCCESS;
@ -329,9 +246,8 @@ ZydisStatus ZydisPrintHex64U(char** buffer, size_t bufferLen, uint64_t value, ui
char temp[ZYDIS_MAXCHARS_HEX_64]; char temp[ZYDIS_MAXCHARS_HEX_64];
uint8_t n = 0; uint8_t n = 0;
for (int8_t i = uint8_t c = ((value & 0xFFFFFFFF00000000) ? ZYDIS_MAXCHARS_HEX_64 : ZYDIS_MAXCHARS_HEX_32);
((value & 0xFFFFFFFF00000000) ? ZYDIS_MAXCHARS_HEX_64 : ZYDIS_MAXCHARS_HEX_32) - 1; for (int8_t i = c - 1; i >= 0; --i)
i >= 0; --i)
{ {
uint8_t v = (value >> i * 4) & 0x0F; uint8_t v = (value >> i * 4) & 0x0F;
if (!n) if (!n)
@ -368,64 +284,88 @@ ZydisStatus ZydisPrintHex64U(char** buffer, size_t bufferLen, uint64_t value, ui
return ZYDIS_STATUS_SUCCESS; return ZYDIS_STATUS_SUCCESS;
} }
ZydisStatus ZydisPrintHex8S(char** buffer, size_t bufferLen, int8_t value, uint8_t paddingLength, /* ---------------------------------------------------------------------------------------------- */
ZydisBool uppercase, ZydisBool prefix) /* Public Functions */
/* ---------------------------------------------------------------------------------------------- */
ZydisStatus ZydisPrintStr(char** buffer, size_t bufferLen, const char* text,
ZydisLetterCase letterCase)
{ {
if (value < 0) ZYDIS_ASSERT(buffer);
ZYDIS_ASSERT(bufferLen > 0);
ZYDIS_ASSERT(text);
size_t strLen = strlen(text);
if (strLen >= bufferLen)
{ {
if (prefix) return ZYDIS_STATUS_INSUFFICIENT_BUFFER_SIZE;
{
ZYDIS_CHECK(ZydisPrintStr(buffer, bufferLen, "-0x", ZYDIS_LETTER_CASE_DEFAULT));
bufferLen -= 3;
} else
{
ZYDIS_CHECK(ZydisPrintStr(buffer, bufferLen, "-", ZYDIS_LETTER_CASE_DEFAULT));
--bufferLen;
}
return ZydisPrintHex32U(buffer, bufferLen, -value, paddingLength, uppercase, ZYDIS_FALSE);
} }
return ZydisPrintHex32U(buffer, bufferLen, value, paddingLength, uppercase, prefix); memcpy(*buffer, text, strLen + 1);
switch (letterCase)
{
case ZYDIS_LETTER_CASE_LOWER:
for (size_t i = 0; i < strLen; ++i)
{
(*buffer)[i] = (char)tolower((*buffer)[i]);
}
break;
case ZYDIS_LETTER_CASE_UPPER:
for (size_t i = 0; i < strLen; ++i)
{
(*buffer)[i] = (char)toupper((*buffer)[i]);
}
break;
default:
break;
}
*buffer += strLen;
return ZYDIS_STATUS_SUCCESS;
} }
ZydisStatus ZydisPrintHex16S(char** buffer, size_t bufferLen, int16_t value, uint8_t paddingLength, ZydisStatus ZydisPrintDecU(char** buffer, size_t bufferLen, uint64_t value, uint8_t paddingLength)
ZydisBool uppercase, ZydisBool prefix)
{ {
if (value < 0) #ifdef ZYDIS_X64
{ return ZydisPrintDecU64(buffer, bufferLen, value, paddingLength);
if (prefix) #else
{ if (value & 0xFFFFFFFF00000000)
ZYDIS_CHECK(ZydisPrintStr(buffer, bufferLen, "-0x", ZYDIS_LETTER_CASE_DEFAULT)); {
bufferLen -= 3; return ZydisPrintDecU64(buffer, bufferLen, value, paddingLength);
} else } else
{ {
ZYDIS_CHECK(ZydisPrintStr(buffer, bufferLen, "-", ZYDIS_LETTER_CASE_DEFAULT)); return ZydisPrintDecU32(buffer, bufferLen, (uint32_t)value, paddingLength);
--bufferLen; }
} #endif
return ZydisPrintHex32U(buffer, bufferLen, -value, paddingLength, uppercase, ZYDIS_FALSE);
}
return ZydisPrintHex32U(buffer, bufferLen, value, paddingLength, uppercase, prefix);
} }
ZydisStatus ZydisPrintHex32S(char** buffer, size_t bufferLen, int32_t value, uint8_t paddingLength, ZydisStatus ZydisPrintDecS(char** buffer, size_t bufferLen, int64_t value, uint8_t paddingLength)
ZydisBool uppercase, ZydisBool prefix)
{ {
if (value < 0) if (value < 0)
{ {
if (prefix) ZYDIS_CHECK(ZydisPrintStr(buffer, bufferLen, "-", ZYDIS_LETTER_CASE_DEFAULT));
{ return ZydisPrintDecU(buffer, bufferLen - 1, -value, paddingLength);
ZYDIS_CHECK(ZydisPrintStr(buffer, bufferLen, "-0x", ZYDIS_LETTER_CASE_DEFAULT));
bufferLen -= 3;
} else
{
ZYDIS_CHECK(ZydisPrintStr(buffer, bufferLen, "-", ZYDIS_LETTER_CASE_DEFAULT));
--bufferLen;
}
return ZydisPrintHex32U(buffer, bufferLen, -value, paddingLength, uppercase, ZYDIS_FALSE);
} }
return ZydisPrintHex32U(buffer, bufferLen, value, paddingLength, uppercase, prefix); return ZydisPrintDecU(buffer, bufferLen, value, paddingLength);
} }
ZydisStatus ZydisPrintHex64S(char** buffer, size_t bufferLen, int64_t value, uint8_t paddingLength, ZydisStatus ZydisPrintHexU(char** buffer, size_t bufferLen, uint64_t value, uint8_t paddingLength,
ZydisBool uppercase, ZydisBool prefix)
{
#ifdef ZYDIS_X64
return ZydisPrintHexU64(buffer, bufferLen, value, paddingLength, uppercase, prefix);
#else
if (value & 0xFFFFFFFF00000000)
{
return ZydisPrintHexU64(buffer, bufferLen, value, paddingLength, uppercase, prefix);
} else
{
return ZydisPrintHexU32(
buffer, bufferLen, (uint32_t)value, paddingLength, uppercase, prefix);
}
#endif
}
ZydisStatus ZydisPrintHexS(char** buffer, size_t bufferLen, int64_t value, uint8_t paddingLength,
ZydisBool uppercase, ZydisBool prefix) ZydisBool uppercase, ZydisBool prefix)
{ {
if (value < 0) if (value < 0)
@ -439,9 +379,9 @@ ZydisStatus ZydisPrintHex64S(char** buffer, size_t bufferLen, int64_t value, uin
ZYDIS_CHECK(ZydisPrintStr(buffer, bufferLen, "-", ZYDIS_LETTER_CASE_DEFAULT)); ZYDIS_CHECK(ZydisPrintStr(buffer, bufferLen, "-", ZYDIS_LETTER_CASE_DEFAULT));
--bufferLen; --bufferLen;
} }
return ZydisPrintHex64U(buffer, bufferLen, -value, paddingLength, uppercase, ZYDIS_FALSE); return ZydisPrintHexU(buffer, bufferLen, -value, paddingLength, uppercase, ZYDIS_FALSE);
} }
return ZydisPrintHex64U(buffer, bufferLen, value, paddingLength, uppercase, prefix); return ZydisPrintHexU(buffer, bufferLen, value, paddingLength, uppercase, prefix);
} }
/* ---------------------------------------------------------------------------------------------- */ /* ---------------------------------------------------------------------------------------------- */

View File

@ -99,8 +99,8 @@ ZYDIS_NO_EXPORT ZydisStatus ZydisPrintStr(char** buffer, size_t bufferLen, const
/* ---------------------------------------------------------------------------------------------- */ /* ---------------------------------------------------------------------------------------------- */
/** /**
* @brief Formats the given unsigned 64-bit ordinal @c value to its decimal text-representation * @brief Formats the given unsigned ordinal @c value to its decimal text-representation and
* and appends it to the @c buffer. * appends it to the @c buffer.
* *
* @param buffer A pointer to the string-buffer. * @param buffer A pointer to the string-buffer.
* @param bufferLen The length of the string-buffer. * @param bufferLen The length of the string-buffer.
@ -115,32 +115,12 @@ ZYDIS_NO_EXPORT ZydisStatus ZydisPrintStr(char** buffer, size_t bufferLen, const
* The string-buffer pointer is increased by the number of chars written, if the call was * The string-buffer pointer is increased by the number of chars written, if the call was
* successfull. * successfull.
*/ */
ZYDIS_NO_EXPORT ZydisStatus ZydisPrintDec64U(char** buffer, size_t bufferLen, uint64_t value, ZYDIS_NO_EXPORT ZydisStatus ZydisPrintDecU(char** buffer, size_t bufferLen, uint64_t value,
uint8_t paddingLength); uint8_t paddingLength);
/** /**
* @brief Formats the given unsigned 32-bit ordinal @c value to its decimal text-representation * @brief Formats the given signed ordinal @c value to its decimal text-representation and
* and appends it to the @c buffer. * appends it to the @c buffer.
*
* @param buffer A pointer to the string-buffer.
* @param bufferLen The length of the string-buffer.
* @param value The value.
* @param paddingLength Padds the converted value with leading zeros, if the number of chars is
* less than the @c paddingLength.
*
* @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.
*
* The string-buffer pointer is increased by the number of chars written, if the call was
* successfull.
*/
ZYDIS_NO_EXPORT ZydisStatus ZydisPrintDec32U(char** buffer, size_t bufferLen, uint32_t value,
uint8_t paddingLength);
/**
* @brief Formats the given signed 64-bit ordinal @c value to its decimal text-representation
* and appends it to the @c buffer.
* *
* @param buffer A pointer to the string-buffer. * @param buffer A pointer to the string-buffer.
* @param bufferLen The length of the string-buffer. * @param bufferLen The length of the string-buffer.
@ -155,67 +135,7 @@ ZYDIS_NO_EXPORT ZydisStatus ZydisPrintDec32U(char** buffer, size_t bufferLen, ui
* The string-buffer pointer is increased by the number of chars written, if the call was * The string-buffer pointer is increased by the number of chars written, if the call was
* successfull. * successfull.
*/ */
ZYDIS_NO_EXPORT ZydisStatus ZydisPrintDec64S(char** buffer, size_t bufferLen, int64_t value, ZYDIS_NO_EXPORT ZydisStatus ZydisPrintDecS(char** buffer, size_t bufferLen, int64_t value,
uint8_t paddingLength);
/**
* @brief Formats the given signed 32-bit ordinal @c value to its decimal text-representation
* and appends it to the @c buffer.
*
* @param buffer A pointer to the string-buffer.
* @param bufferLen The length of the string-buffer.
* @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).
*
* @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.
*
* The string-buffer pointer is increased by the number of chars written, if the call was
* successfull.
*/
ZYDIS_NO_EXPORT ZydisStatus ZydisPrintDec32S(char** buffer, size_t bufferLen, int32_t value,
uint8_t paddingLength);
/**
* @brief Formats the given signed 16-bit ordinal @c value to its decimal text-representation
* and appends it to the @c buffer.
*
* @param buffer A pointer to the string-buffer.
* @param bufferLen The length of the string-buffer.
* @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).
*
* @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.
*
* The string-buffer pointer is increased by the number of chars written, if the call was
* successfull.
*/
ZYDIS_NO_EXPORT ZydisStatus ZydisPrintDec16S(char** buffer, size_t bufferLen, int16_t value,
uint8_t paddingLength);
/**
* @brief Formats the given signed 8-bit ordinal @c value to its decimal text-representation
* and appends it to the @c buffer.
*
* @param buffer A pointer to the string-buffer.
* @param bufferLen The length of the string-buffer.
* @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).
*
* @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.
*
* The string-buffer pointer is increased by the number of chars written, if the call was
* successfull.
*/
ZYDIS_NO_EXPORT ZydisStatus ZydisPrintDec8S(char** buffer, size_t bufferLen, int8_t value,
uint8_t paddingLength); uint8_t paddingLength);
/* ---------------------------------------------------------------------------------------------- */ /* ---------------------------------------------------------------------------------------------- */
@ -223,8 +143,8 @@ ZYDIS_NO_EXPORT ZydisStatus ZydisPrintDec8S(char** buffer, size_t bufferLen, int
/* ---------------------------------------------------------------------------------------------- */ /* ---------------------------------------------------------------------------------------------- */
/** /**
* @brief Formats the given unsigned 64-bit ordinal @c value to its hexadecimal text- * @brief Formats the given unsigned ordinal @c value to its hexadecimal text-representation and
* representation and appends it to the @c buffer. * appends it to the @c buffer.
* *
* @param buffer A pointer to the string-buffer. * @param buffer A pointer to the string-buffer.
* @param bufferLen The length of the string-buffer. * @param bufferLen The length of the string-buffer.
@ -242,35 +162,12 @@ ZYDIS_NO_EXPORT ZydisStatus ZydisPrintDec8S(char** buffer, size_t bufferLen, int
* The string-buffer pointer is increased by the number of chars written, if the call was * The string-buffer pointer is increased by the number of chars written, if the call was
* successfull. * successfull.
*/ */
ZYDIS_NO_EXPORT ZydisStatus ZydisPrintHex64U(char** buffer, size_t bufferLen, uint64_t value, ZYDIS_NO_EXPORT ZydisStatus ZydisPrintHexU(char** buffer, size_t bufferLen, uint64_t value,
uint8_t paddingLength, ZydisBool uppercase, ZydisBool prefix); uint8_t paddingLength, ZydisBool uppercase, ZydisBool prefix);
/** /**
* @brief Formats the given unsigned 32-bit ordinal @c value to its hexadecimal text- * @brief Formats the given signed ordinal @c value to its hexadecimal text-representation and
* representation and appends it to the @c buffer. * appends it to the @c buffer.
*
* @param buffer A pointer to the string-buffer.
* @param bufferLen The length of the string-buffer.
* @param value The value.
* @param paddingLength Padds the converted value with leading zeros, if the number of chars is
* less than the @c paddingLength.
* @param uppercase Set @c TRUE to print the hexadecimal value in uppercase letters instead
* of lowercase ones.
* @param prefix Set @c TRUE to add the "0x" prefix to the hexadecimal value.
*
* @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.
*
* The string-buffer pointer is increased by the number of chars written, if the call was
* successfull.
*/
ZYDIS_NO_EXPORT ZydisStatus ZydisPrintHex32U(char** buffer, size_t bufferLen, uint32_t value,
uint8_t paddingLength, ZydisBool uppercase, ZydisBool prefix);
/**
* @brief Formats the given signed 64-bit ordinal @c value to its hexadecimal text-
* representation and appends it to the @c buffer.
* *
* @param buffer A pointer to the string-buffer. * @param buffer A pointer to the string-buffer.
* @param bufferLen The length of the string-buffer. * @param bufferLen The length of the string-buffer.
@ -288,76 +185,7 @@ ZYDIS_NO_EXPORT ZydisStatus ZydisPrintHex32U(char** buffer, size_t bufferLen, ui
* The string-buffer pointer is increased by the number of chars written, if the call was * The string-buffer pointer is increased by the number of chars written, if the call was
* successfull. * successfull.
*/ */
ZYDIS_NO_EXPORT ZydisStatus ZydisPrintHex64S(char** buffer, size_t bufferLen, int64_t value, ZYDIS_NO_EXPORT ZydisStatus ZydisPrintHexS(char** buffer, size_t bufferLen, int64_t value,
uint8_t paddingLength, ZydisBool uppercase, ZydisBool prefix);
/**
* @brief Formats the given signed 32-bit ordinal @c value to its hexadecimal text-
* representation and appends it to the @c buffer.
*
* @param buffer A pointer to the string-buffer.
* @param bufferLen The length of the string-buffer.
* @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).
* @param uppercase Set @c TRUE to print the hexadecimal value in uppercase letters instead
* of lowercase ones.
* @param prefix Set @c TRUE to add the "0x" prefix to the hexadecimal value.
*
* @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.
*
* The string-buffer pointer is increased by the number of chars written, if the call was
* successfull.
*/
ZYDIS_NO_EXPORT ZydisStatus ZydisPrintHex32S(char** buffer, size_t bufferLen, int32_t value,
uint8_t paddingLength, ZydisBool uppercase, ZydisBool prefix);
/**
* @brief Formats the given signed 16-bit ordinal @c value to its hexadecimal text-
* representation and appends it to the @c buffer.
*
* @param buffer A pointer to the string-buffer.
* @param bufferLen The length of the string-buffer.
* @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).
* @param uppercase Set @c TRUE to print the hexadecimal value in uppercase letters instead
* of lowercase ones.
* @param prefix Set @c TRUE to add the "0x" prefix to the hexadecimal value.
*
* @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.
*
* The string-buffer pointer is increased by the number of chars written, if the call was
* successfull.
*/
ZYDIS_NO_EXPORT ZydisStatus ZydisPrintHex16S(char** buffer, size_t bufferLen, int16_t value,
uint8_t paddingLength, ZydisBool uppercase, ZydisBool prefix);
/**
* @brief Formats the given signed 8-bit ordinal @c value to its hexadecimal text-
* representation and appends it to the @c buffer.
*
* @param buffer A pointer to the string-buffer.
* @param bufferLen The length of the string-buffer.
* @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).
* @param uppercase Set @c TRUE to print the hexadecimal value in uppercase letters instead
* of lowercase ones.
* @param prefix Set @c TRUE to add the "0x" prefix to the hexadecimal value.
*
* @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.
*
* The string-buffer pointer is increased by the number of chars written, if the call was
* successfull.
*/
ZYDIS_NO_EXPORT ZydisStatus ZydisPrintHex8S(char** buffer, size_t bufferLen, int8_t value,
uint8_t paddingLength, ZydisBool uppercase, ZydisBool prefix); uint8_t paddingLength, ZydisBool uppercase, ZydisBool prefix);
/* ---------------------------------------------------------------------------------------------- */ /* ---------------------------------------------------------------------------------------------- */

View File

@ -193,7 +193,7 @@ static ZydisStatus ZydisFormatterFormatOperandMemIntel(const ZydisFormatter* for
{ {
ZYDIS_CHECK( ZYDIS_CHECK(
ZydisPrintStr(buffer, bufEnd - *buffer, "*", ZYDIS_LETTER_CASE_DEFAULT)); ZydisPrintStr(buffer, bufEnd - *buffer, "*", ZYDIS_LETTER_CASE_DEFAULT));
ZYDIS_CHECK(ZydisPrintDec32U(buffer, bufEnd - *buffer, operand->mem.scale, 0)); ZYDIS_CHECK(ZydisPrintDecU(buffer, bufEnd - *buffer, operand->mem.scale, 0));
//ZYDIS_CHECK(ZydisPrintStrFormat(buffer, bufEnd - *buffer, //ZYDIS_CHECK(ZydisPrintStrFormat(buffer, bufEnd - *buffer,
// ZYDIS_STRBUF_APPEND_MODE_DEFAULT, "*%d", operand->mem.scale)); // ZYDIS_STRBUF_APPEND_MODE_DEFAULT, "*%d", operand->mem.scale));
} }
@ -215,10 +215,10 @@ static ZydisStatus ZydisFormatterFormatOperandPtrIntel(const ZydisFormatter* for
} }
char* bufEnd = *buffer + bufferLen; char* bufEnd = *buffer + bufferLen;
ZYDIS_CHECK(ZydisPrintHex32U( ZYDIS_CHECK(ZydisPrintHexU(
buffer, bufEnd - *buffer, operand->ptr.segment, 4, ZYDIS_TRUE, ZYDIS_TRUE)); buffer, bufEnd - *buffer, operand->ptr.segment, 4, ZYDIS_TRUE, ZYDIS_TRUE));
ZYDIS_CHECK(ZydisPrintStr(buffer, bufEnd - *buffer, ":", ZYDIS_LETTER_CASE_DEFAULT)); ZYDIS_CHECK(ZydisPrintStr(buffer, bufEnd - *buffer, ":", ZYDIS_LETTER_CASE_DEFAULT));
return ZydisPrintHex32U( return ZydisPrintHexU(
buffer, bufEnd - *buffer, operand->ptr.offset, 8, ZYDIS_TRUE, ZYDIS_TRUE); buffer, bufEnd - *buffer, operand->ptr.offset, 8, ZYDIS_TRUE, ZYDIS_TRUE);
//return ZydisPrintStrFormat(buffer, bufferLen, ZYDIS_STRBUF_APPEND_MODE_DEFAULT, //return ZydisPrintStrFormat(buffer, bufferLen, ZYDIS_STRBUF_APPEND_MODE_DEFAULT,
// "0x%04"PRIX16":0x%08"PRIX32, operand->ptr.segment, operand->ptr.offset); // "0x%04"PRIX16":0x%08"PRIX32, operand->ptr.segment, operand->ptr.offset);
@ -256,7 +256,7 @@ static ZydisStatus ZydisFormatterFormatOperandImmIntel(const ZydisFormatter* for
return ZYDIS_STATUS_INVALID_PARAMETER; return ZYDIS_STATUS_INVALID_PARAMETER;
} }
return ZydisPrintHex32S( return ZydisPrintHexS(
buffer, bufferLen, (int32_t)operand->imm.value.s, 2, ZYDIS_TRUE, ZYDIS_TRUE); buffer, bufferLen, (int32_t)operand->imm.value.s, 2, ZYDIS_TRUE, ZYDIS_TRUE);
/*if (printSignedHEX && (operand->imm.value.s < 0)) /*if (printSignedHEX && (operand->imm.value.s < 0))
{ {
@ -287,11 +287,11 @@ static ZydisStatus ZydisFormatterPrintAddressIntel(const ZydisFormatter* formatt
{ {
case 16: case 16:
case 32: case 32:
return ZydisPrintHex64U(buffer, bufferLen, address, 8, ZYDIS_TRUE, ZYDIS_TRUE); return ZydisPrintHexU(buffer, bufferLen, (uint32_t)address, 8, ZYDIS_TRUE, ZYDIS_TRUE);
//return ZydisPrintStrFormat(buffer, bufferLen, ZYDIS_STRBUF_APPEND_MODE_DEFAULT, //return ZydisPrintStrFormat(buffer, bufferLen, ZYDIS_STRBUF_APPEND_MODE_DEFAULT,
// "0x%08"PRIX64, address); // "0x%08"PRIX64, address);
case 64: case 64:
return ZydisPrintHex64U(buffer, bufferLen, address, 16, ZYDIS_TRUE, ZYDIS_TRUE); return ZydisPrintHexU(buffer, bufferLen, address, 16, ZYDIS_TRUE, ZYDIS_TRUE);
//return ZydisPrintStrFormat(buffer, bufferLen, ZYDIS_STRBUF_APPEND_MODE_DEFAULT, //return ZydisPrintStrFormat(buffer, bufferLen, ZYDIS_STRBUF_APPEND_MODE_DEFAULT,
// "0x%016"PRIX64, address); // "0x%016"PRIX64, address);
default: default:
@ -318,7 +318,7 @@ static ZydisStatus ZydisFormatterPrintDisplacementIntel(const ZydisFormatter* fo
(operand->mem.base != ZYDIS_REGISTER_NONE) || (operand->mem.base != ZYDIS_REGISTER_NONE) ||
(operand->mem.index != ZYDIS_REGISTER_NONE))) (operand->mem.index != ZYDIS_REGISTER_NONE)))
{ {
return ZydisPrintHex64S( return ZydisPrintHexS(
buffer, bufferLen, operand->mem.disp.value, 2, ZYDIS_TRUE, ZYDIS_TRUE); buffer, bufferLen, operand->mem.disp.value, 2, ZYDIS_TRUE, ZYDIS_TRUE);
} }
char* bufEnd = *buffer + bufferLen; char* bufEnd = *buffer + bufferLen;
@ -327,8 +327,8 @@ static ZydisStatus ZydisFormatterPrintDisplacementIntel(const ZydisFormatter* fo
{ {
ZYDIS_CHECK(ZydisPrintStr(buffer, bufferLen, "+", ZYDIS_LETTER_CASE_DEFAULT)); ZYDIS_CHECK(ZydisPrintStr(buffer, bufferLen, "+", ZYDIS_LETTER_CASE_DEFAULT));
} }
return ZydisPrintHex64U( return ZydisPrintHexU(
buffer, bufEnd - *buffer, operand->mem.disp.value, 2, ZYDIS_TRUE, ZYDIS_TRUE); buffer, bufEnd - *buffer, (uint64_t)operand->mem.disp.value, 2, ZYDIS_TRUE, ZYDIS_TRUE);
} }
return ZYDIS_STATUS_SUCCESS; return ZYDIS_STATUS_SUCCESS;
} }
@ -353,17 +353,17 @@ static ZydisStatus ZydisFormatterPrintImmediateIntel(const ZydisFormatter* forma
switch (operand->size) switch (operand->size)
{ {
case 8: case 8:
return ZydisPrintHex8S( return ZydisPrintHexS(
buffer, bufferLen, (int8_t)operand->imm.value.s, 2, ZYDIS_TRUE, ZYDIS_TRUE); buffer, bufferLen, (int8_t)operand->imm.value.s, 2, ZYDIS_TRUE, ZYDIS_TRUE);
case 16: case 16:
return ZydisPrintHex16S( return ZydisPrintHexS(
buffer, bufferLen, (int16_t)operand->imm.value.s, 2, ZYDIS_TRUE, ZYDIS_TRUE); buffer, bufferLen, (int16_t)operand->imm.value.s, 2, ZYDIS_TRUE, ZYDIS_TRUE);
case 32: case 32:
return ZydisPrintHex32S( return ZydisPrintHexS(
buffer, bufferLen, (int32_t)operand->imm.value.s, 2, ZYDIS_TRUE, ZYDIS_TRUE); buffer, bufferLen, (int32_t)operand->imm.value.s, 2, ZYDIS_TRUE, ZYDIS_TRUE);
case 64: case 64:
return ZydisPrintHex64S( return ZydisPrintHexS(
buffer, bufferLen, (int64_t)operand->imm.value.s, 2, ZYDIS_TRUE, ZYDIS_TRUE); buffer, bufferLen, operand->imm.value.s, 2, ZYDIS_TRUE, ZYDIS_TRUE);
default: default:
return ZYDIS_STATUS_INVALID_PARAMETER; return ZYDIS_STATUS_INVALID_PARAMETER;
} }
@ -371,14 +371,14 @@ static ZydisStatus ZydisFormatterPrintImmediateIntel(const ZydisFormatter* forma
switch (instruction->operandSize) switch (instruction->operandSize)
{ {
case 16: case 16:
return ZydisPrintHex32U( return ZydisPrintHexU(
buffer, bufferLen, (uint16_t)operand->imm.value.u, 2, ZYDIS_TRUE, ZYDIS_TRUE); buffer, bufferLen, (uint16_t)operand->imm.value.u, 2, ZYDIS_TRUE, ZYDIS_TRUE);
case 32: case 32:
return ZydisPrintHex32U( return ZydisPrintHexU(
buffer, bufferLen, (int32_t)operand->imm.value.u, 2, ZYDIS_TRUE, ZYDIS_TRUE); buffer, bufferLen, (int32_t)operand->imm.value.u, 2, ZYDIS_TRUE, ZYDIS_TRUE);
case 64: case 64:
return ZydisPrintHex64U( return ZydisPrintHexU(
buffer, bufferLen, (int64_t)operand->imm.value.u, 2, ZYDIS_TRUE, ZYDIS_TRUE); buffer, bufferLen, operand->imm.value.u, 2, ZYDIS_TRUE, ZYDIS_TRUE);
default: default:
return ZYDIS_STATUS_INVALID_PARAMETER; return ZYDIS_STATUS_INVALID_PARAMETER;
} }