Add unprotect_addr to util.c

This commit is contained in:
8dcc 2023-07-24 15:57:25 +02:00
parent 44f00ace13
commit fee20bb1ec
2 changed files with 25 additions and 0 deletions

View File

@ -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_ */

View File

@ -6,6 +6,8 @@
#include <math.h>
#include <GL/gl.h>
#include <unistd.h> /* getpagesize */
#include <sys/mman.h> /* 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;
}