Skip to content

Instantly share code, notes, and snippets.

@NSEcho
Last active March 31, 2021 13:06
Show Gist options
  • Save NSEcho/62b512638530a4d5e03afd41813e1685 to your computer and use it in GitHub Desktop.
Save NSEcho/62b512638530a4d5e03afd41813e1685 to your computer and use it in GitHub Desktop.
/*
typedef struct {
char *name;
int age;
}person;
// Returns new person with name "test => name" and age equals age
person * create_person(char *name, int age) {
person * p = (person*)malloc(sizeof(person));
p->name = (char*)malloc(sizeof(char) * 250);
sprintf(p->name, "test => %s", name);
p->age = age;
return p;
}
// Will write struct information into output.txt file
void print_person(person *p) {
FILE *fp;
fp = fopen("output.txt", "w");
fprintf(fp, "Name is %s\n", p->name);
fprintf(fp, "Age is %d\n", p->age);
fclose(fp);
}
*/
// Interpreting struct
const _create_person = new NativeFunction(Module.getExportByName(null
, 'create_person'), 'pointer', ['pointer', 'int']);
conse
const p1 = _create_person(Memory.allocUtf8String("John"), 24);
const retName = p1.readPointer()
const retAge = p1.add(Process.pointerSize)
console.log(retName.readCString()) // test => John
console.log(retAge.readInt()) // 24
// Creating struct variable
const _print_person = new NativeFunction(Module.getExportByName(null, 'print_person'), 'void', ['pointer']);
const new_person = Memory.alloc(50); // need to alloc real size, this is way too much but fuck it
const name = Memory.allocUtf8String("erhad");
const age = 30;
/* since the first property inside the struct is char*, by accessing the struct
object we are accessing the pointer inside of it*/
new_person.writePointer(name);
/* inside the memory, age comes after the char *name, so we need to add write
our int to <struct object> + pointerSize */
new_person.add(Process.pointerSize).writeInt(age);
_print_person(new_person); // will call the function which will save the output to output.txt
/* If we take a look at output.txt we would see following
$ cat output.txt
Name is erhad
Age is 30
*/
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment