This repository was archived by the owner on Aug 21, 2025. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.c
More file actions
61 lines (50 loc) · 1.61 KB
/
main.c
File metadata and controls
61 lines (50 loc) · 1.61 KB
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
51
52
53
54
55
56
57
58
59
60
61
#include<stdio.h>
#include<string.h>
// fixed storage location for the program's code
const char code_slot[] = "AAAAAAAAAA";
// the computed location of `code_slot` in resultant binary
//
// confirm memory address below using:
// - manually use either tool: hexedit or xdd
// - use pre-written script: node ./compute.js
#define CODE_SLOT_ADDR 0x2008
// function to update binary's code contents
int update_code(char* filename, char* data);
// dummy processing work for the binary
void print_code();
int main(int argc, char** argv) {
if (argc == 3 && strcmp(argv[1], "rewrite") == 0) {
// update with new code
int status = update_code(argv[0], argv[2]);
return status;
}
// otherwise, do the work normally
print_code();
return 0;
}
int update_code(char* filename, char* data) {
// open file for writing binary data
FILE* fp = fopen(filename, "r+b");
if (!fp) {
perror("cannot open file");
return 1;
}
// move file internal file pointer to the beginning of the file
// then move to `code_slot` location
fseek(fp, CODE_SLOT_ADDR, SEEK_SET);
//now write the new code
size_t size = sizeof(char);
size_t count = sizeof(data) / size;
// don't overwrite other things & keep null terminator character at the end
size_t limit = (sizeof(code_slot) / size) - 1;
count = count > limit ? limit : count;
fwrite(data, size, count, fp);
// close file
fclose(fp);
// success
return 0;
}
void print_code() {
size_t size = sizeof(code_slot) / sizeof(code_slot[0]);
printf("Size: %ld\nData: %s\n",size, code_slot);
}