diff --git a/src/include/util.h b/src/include/util.h index 0b6298b..d725301 100644 --- a/src/include/util.h +++ b/src/include/util.h @@ -35,5 +35,6 @@ float angle_delta_rad(float a, float b); bool world_to_screen(vec3_t vec, vec2_t screen); void gl_drawbox(int x, int y, int w, int h, rgb_t c); void gl_drawline(int x0, int y0, int x1, int y1, float w, rgb_t col); +bool unprotect_addr(void* ptr); #endif /* UTIL_H_ */ diff --git a/src/util.c b/src/util.c index 8884bbe..abcb044 100644 --- a/src/util.c +++ b/src/util.c @@ -6,6 +6,8 @@ #include #include +#include /* getpagesize */ +#include /* mprotect */ cl_entity_t* get_entity(int ent_idx) { if (ent_idx < 0 || ent_idx > 32) @@ -125,3 +127,25 @@ void gl_drawline(int x0, int y0, int x1, int y1, float w, rgb_t col) { glEnable(GL_TEXTURE_2D); glDisable(GL_BLEND); } + +/* + * Credits: + * https://github.com/UnkwUsr/hlhax/blob/26491984996c8389efec977ed940c5a67a0ecca4/src/utils/mem/mem.cpp + * Linux kernel, tools/virtio/linux/kernel.h + */ +#define PAGE_SIZE getpagesize() +#define PAGE_MASK (~(PAGE_SIZE - 1)) +#define PAGE_ALIGN(x) ((x + PAGE_SIZE - 1) & PAGE_MASK) +#define PAGE_ALIGN_DOWN(x) (PAGE_ALIGN(x) - PAGE_SIZE) + +bool unprotect_addr(void* ptr) { + void* p = (void*)PAGE_ALIGN_DOWN((int)ptr); + int pgsz = getpagesize(); + + if (mprotect(p, pgsz, PROT_READ | PROT_WRITE) == -1) { + printf("hl-cheat: error unprotecting %p\n", ptr); + return false; + } + + return true; +}