//
//  utils.h
//  lara
//
//  Created by ruter on 25.03.26.
//

#ifndef utils_h
#define utils_h

#include <stdint.h>
#include <unistd.h>
#include <sys/types.h>
#include <stdbool.h>
#include <mach/mach.h>

#ifdef __cplusplus
extern "C" {
#endif

void init_offsets(void);
uint64_t ourproc(void);
uint64_t taskbyproc(uint64_t procaddr);
uint64_t procbyname(const char *procname);
uint64_t procbypid(pid_t targetpid);

typedef struct {
    uint32_t pid;
    uint32_t uid;
    uint32_t gid;
    char name[32];
    uint64_t kaddr;
} proc_entry_t;

proc_entry_t* proclist(const char *search, int *out_count);
void free_proclist(proc_entry_t *list);

extern bool aslrstate;
void getaslrstate(void);
int toggleaslr(void);

int killproc(const char* name);
bool islcruntime(void);

void hexdump(const void* data, size_t size);
void filehexdump(const char *path, size_t size);

uint64_t ipc_entry_lookup(uint64_t space, mach_port_name_t name);
uint64_t task_get_ipc_port_table_entry(uint64_t task, mach_port_t port);
uint64_t task_get_ipc_port_object(uint64_t task, mach_port_t port);
uint64_t task_get_ipc_port_kobject(uint64_t task, mach_port_t port);
uint64_t task_get_vm_map(uint64_t task_ptr);

int disable_excguard_kill(uint64_t task);
uint64_t thread_get_t_tro(uint64_t thread);
uint64_t thread_get_task(uint64_t thread);
uint16_t thread_get_options(uint64_t thread);
void thread_set_options(uint64_t thread, uint16_t options);
void thread_set_mutex(uint64_t thread, uint32_t ctid);
uint32_t thread_get_mutex(uint64_t thread);
uint64_t thread_get_kstackptr(uint64_t thread);
uint64_t thread_get_jop_pid(uint64_t thread);
uint64_t thread_get_rop_pid(uint64_t thread);

uint64_t proc_task(uint64_t proc);
uint64_t proc_find_by_name(const char* name);
uint64_t proc_self(void);
uint64_t task_self(void);

int crashproc(const char* pid);

#ifdef __cplusplus
}
#endif

#endif /* utils_h */
