forked from flathub/com.albiononline.AlbionOnline
-
Notifications
You must be signed in to change notification settings - Fork 0
/
splitelf.c
74 lines (69 loc) · 1.99 KB
/
splitelf.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
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
/*
splitelf: Extract trailing data from an ELF file.
Copyright (C) 2017 Valentin David
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <stdio.h>
#include <unistd.h>
#include <elf.h>
int main(int argc, char *argv[]) {
if (argc < 3) {
fprintf(stderr, "splitelf: missing arguments");
return 1;
}
int infd = open(argv[1], O_RDONLY);
if (infd < 0) {
perror("splitelf");
return 1;
}
int outfd = open(argv[2], O_WRONLY|O_CREAT, 0666);
if (outfd < 0) {
perror("splitelf");
return 1;
}
Elf64_Ehdr header;
ssize_t r = read(infd, (void*)&header, sizeof(Elf64_Ehdr));
if (r < 0) {
perror("splitelf");
return 1;
} else if (r != sizeof(Elf64_Ehdr)) {
fprintf(stderr, "splitelf: not enough bytes in elf header");
return 1;
}
off_t elf_size = (off_t)(header.e_shoff + header.e_shentsize*header.e_shnum);
off_t seeked = lseek(infd, (off_t)elf_size, SEEK_SET);
if (seeked < 0) {
perror("splitelf");
return 1;
} else if (seeked != elf_size) {
fprintf(stderr, "splitelf: not enough bytes in elf");
return 1;
}
while (1) {
char data[4096];
ssize_t dr = read(infd, &data, 4096);
if (dr < 0) {
perror("splitelf");
return 1;
}
write(outfd, &data, dr);
if (dr != 4096) {
break ;
}
}
close(infd);
close(outfd);
return 0;
}