get_function_bytes/analysis_function.h

41 lines
1.0 KiB
C
Raw Normal View History

2025-01-15 18:29:50 +03:00
#pragma once
#include <stdio.h>
2025-01-15 18:51:31 +03:00
#include <stdint.h>
2025-01-15 18:29:50 +03:00
#define RET 0xC3 // ret opcode
typedef unsigned char byte;
2025-01-15 19:00:16 +03:00
// Returns the real address of the passed function
2025-01-15 19:02:15 +03:00
// Will return NULL if the compiler is not supported
2025-01-15 18:29:50 +03:00
byte* AF_address(byte* f) {
byte* real_address = NULL;
#ifdef _MSC_VER // MSVC
#ifdef NDEBUG // MSVC release mode
real_address = f;
#else // MSVC debug mode
byte* f_p = f;
2025-01-15 18:51:31 +03:00
byte* offset = (byte*)(*((int32_t*)f_p) >> 8);
real_address = f_p + (int32_t)offset + 5;
2025-01-15 18:29:50 +03:00
#endif
#elif defined(__GNUC__) // GCC
real_address = f;
#endif
return real_address;
}
2025-01-15 19:00:16 +03:00
// Print bytes from address to address+size (Use AF_address to get function ptr)
2025-01-15 18:51:31 +03:00
int32_t AF_print_bytes(byte* a, int32_t size) {
for (int32_t i = 0; i < size; ++i) {
2025-01-15 18:43:58 +03:00
printf("%02X ", *(a + i));
}
2025-01-15 18:29:50 +03:00
}
2025-01-15 18:51:31 +03:00
2025-01-15 19:00:16 +03:00
// Get any function size (Use AF_address to get function ptr)
2025-01-15 18:51:31 +03:00
int32_t AF_size(byte* f) {
2025-01-15 18:29:50 +03:00
byte* p = f;
for (; *p != RET; ++p);
return p - f + 1;
2025-01-15 19:00:36 +03:00
}