-
Notifications
You must be signed in to change notification settings - Fork 0
/
curry.c
50 lines (41 loc) · 1.21 KB
/
curry.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
#include <stdio.h>
#include <malloc.h>
//This is a function that takes a
// char func(char, char)
// and a
// char n
// as parameters and returns a
// char func_c(char)
char (*curry(char (*func)(char,char), char n))(char){
//Allocate a byte buffer to store the assembly code
char* newFunction = malloc(11);
//Insert the instructions
newFunction[0] = 0x58; //pop eax
newFunction[1] = 0x6a; //push
newFunction[2] = n; //n
newFunction[3] = 0x50; //push eax
newFunction[4] = 0xb8; //mov eax,
//The address for the jump has to be stored in the reverse order
long funcAddr = (long) func;
newFunction[8] = (funcAddr >> 24) & 0xFF;
newFunction[7] = (funcAddr >> 16) & 0xFF;
newFunction[6] = (funcAddr >> 8) & 0xFF;
newFunction[5] = funcAddr & 0xFF;
newFunction[9] = 0xff; //jmp
newFunction[10] = 0xe0; //eax
//Cast the array to a function pointer and return it
return (char (*)(char))newFunction;
}
//Test function
char add(char a, char b){
return a+b;
}
int main(){
char a = 42;
char b = 80;
//Pass the first parameter and get the curried function
char (*addA)(char) = curry(add, a);
//Use the curried function and print the result
printf("%d+%d=%d\n", a, b, addA(b));
return 0;
}