PIR/lib/rondoudouPatch.c

118 lines
2.4 KiB
C

#include <stdio.h>
#include <stdlib.h>
#include <stdint.h>
#include "rondoudouPatch.h"
//WARNING SUR TOUT LES TYPES !!!
#ifdef NOCIPHER
void cipher(void *address) {}
void decipher() {}
void rondoudou_patch_init() {}
#else
struct t_pile_addrs {
int index;
void **tab[TAB_SIZE];
struct t_pile_addrs *next;
};
uintptr_t rondoudou_patch_key;
struct t_pile_addrs *pile_addrs;
void rondoudou_patch_init(void) {
pile_addrs = (struct t_pile_addrs *)malloc(sizeof(struct t_pile_addrs));
pile_addrs->index = -1;
pile_addrs->next = NULL;
rondoudou_patch_key = 12345;
}
int print_pile_aux(int profondeur, struct t_pile_addrs * pile) {
if (pile != NULL) {
int prof_max = print_pile_aux(profondeur + 1, pile->next);
int i;
int max;
if (pile->index != TAB_SIZE) {
max = pile->index + 1;
} else {
max = TAB_SIZE;
}
for (i = 0; i<max; i++) {
// printf("%d -> %p\n", (prof_max - profondeur)*TAB_SIZE + i, pile->tab[i]);
}
return prof_max;
} else {
return profondeur - 1;
}
}
void print_pile(void) {
print_pile_aux(0, pile_addrs);
}
/*
*
* ATTENTION !!!!! FONCTION ULTRA CHELOUE
*
*/
void ** find_address_in_stack(void * addr) {
void ** ret = 0;
int trouve = 0;
int i;
for (i=0; i<1000; i++) {
if ((*(&ret + i)) == addr) {
if (trouve) {
ret = (void **)(&ret + i);
break;
}
trouve = 1;
}
}
return ret;
}
void cipher(void * address) {
void ** addr = find_address_in_stack(address);
if (addr != 0) {
*addr = (void *)((uintptr_t)*addr ^ (uintptr_t)rondoudou_patch_key);
if ((pile_addrs->index) == (TAB_SIZE - 1)) {
struct t_pile_addrs * aux = (struct t_pile_addrs *)malloc(sizeof(struct t_pile_addrs));
aux->index = 0;
aux->next = pile_addrs;
pile_addrs = aux;
} else {
pile_addrs->index++;
}
pile_addrs->tab[pile_addrs->index] = addr;
// printf("APPEL A CIPHER\n");
print_pile();
}
}
void decipher(void) {
if (pile_addrs->index == -1) {
if (pile_addrs->next == NULL) {
// printf("Ouille ouille ouille qu'est ce que j'ai mal aux nouilles ! \n");
exit(2);
} else {
struct t_pile_addrs * aux = pile_addrs;
pile_addrs = pile_addrs->next;
free(aux);
}
}
*((pile_addrs->tab)[pile_addrs->index]) = (void *) ((uintptr_t) (*((pile_addrs->tab)[pile_addrs->index])) ^
(uintptr_t) rondoudou_patch_key);
pile_addrs->index--;
// printf("APPEL A DECIPHER\n");
print_pile();
}
#endif // NOCIPHER
void changekey(void);