From e61b27e6c699ae175d7704ddaaee0d39b6d14382 Mon Sep 17 00:00:00 2001 From: Quentin LADEVIE Date: Tue, 3 Sep 2024 11:15:10 +0200 Subject: Log dll name on loading Print output debug string Refactored code Improved print Add a function to read memory from the deubgged process Fixed a read out of bounds --- src/utils.c | 54 ++++++++++++++++++++++++++++++++++++++++++------------ 1 file changed, 42 insertions(+), 12 deletions(-) (limited to 'src/utils.c') diff --git a/src/utils.c b/src/utils.c index f7994f9..de719e9 100644 --- a/src/utils.c +++ b/src/utils.c @@ -1,3 +1,22 @@ +#if defined (_DEBUG) +#define assert(x) if (!(x)) __debugbreak(); +#else +#define assert(x) (x) +#endif +void print(const char* format, ...); + +u8 is_alpha(char c) { + return ('a' <= c && c <= 'z') || ('A' <= c && c <= 'Z'); +} + +u8 is_decimal_digit(char c) { + return '0' <= c && c <= '9'; +} + +u8 is_hexadecimal_digit(char c) { + return ('0' <= c && c <= '9') || ('a' <= c && c <= 'f') || ('A' <= c && c <= 'F'); +} + void u64toa(u64 number, char* str, u32 base) { static char codes[16U] = { '0', @@ -89,25 +108,36 @@ void print(const char* format, ...) { char c = va_arg(args, char); StringCchCatA(cur_char, 1U, &c); cur_char += 1U; + } else if (*cur_fmt == 'x' && is_alpha(cur_fmt[1])) { + u32 base = 16U; + *cur_char = '0'; + cur_char++; + *cur_char = 'x'; + cur_char++; + cur_fmt++; + + if(*cur_fmt == 'b') { + u8 number = va_arg(args, u8); + u64toa((u64)number, cur_char, base); + size_t length = 0U; + StringCchLengthA(formatted_str, sizeof(formatted_str), &length); + cur_char = formatted_str + length; + } else if(*cur_fmt == 'u') { + u64 number = va_arg(args, u64); + u64toa(number, cur_char, base); + size_t length = 0U; + StringCchLengthA(formatted_str, sizeof(formatted_str), &length); + cur_char = formatted_str + length; + } } else if(*cur_fmt == 'b') { u8 number = va_arg(args, u8); - u64toa((u64)number, cur_char, 10); + u64toa((u64)number, cur_char, 10U); size_t length = 0U; StringCchLengthA(formatted_str, sizeof(formatted_str), &length); cur_char = formatted_str + length; } else if(*cur_fmt == 'u') { u64 number = va_arg(args, u64); - u64toa(number, cur_char, 10); - size_t length = 0U; - StringCchLengthA(formatted_str, sizeof(formatted_str), &length); - cur_char = formatted_str + length; - } else if(*cur_fmt == 'x') { - u64 number = va_arg(args, u64); - *cur_char = '0'; - cur_char++; - *cur_char = 'x'; - *cur_char++; - u64toa(number, cur_char, 16); + u64toa(number, cur_char, 10U); size_t length = 0U; StringCchLengthA(formatted_str, sizeof(formatted_str), &length); cur_char = formatted_str + length; -- cgit v1.3