#include "../includes/woody.h" int get_elf_file(t_elf_content *woody) { int fd; off_t off; fd = open(woody->file_path, O_RDONLY); if (fd < 0) { ft_printf("Error: Failed to open \'%s\'\n", woody->file_path); return EXIT_FAILURE; } off = lseek(fd, 0, SEEK_END); if (off == -1) { close(fd); ft_printf("Error: Failed to read file offset \'%s\'\n", woody->file_path); return EXIT_FAILURE; } woody->file_size = off; woody->file = mmap(NULL, woody->file_size, PROT_READ | PROT_WRITE | PROT_EXEC, MAP_PRIVATE, fd, 0); if (woody->file == MAP_FAILED) { close(fd); ft_printf("Error: Failed to map file \'%s\'\n", woody->file_path); return EXIT_FAILURE; } close(fd); return EXIT_SUCCESS; } int save_file(char *path, char *file, unsigned long int size) { int fd = open(path, O_CREAT | O_WRONLY | O_TRUNC, 0755); if (fd == -1) { printf("Error: Failed to create new file \'%s\'\n", path); return EXIT_FAILURE; } if (write(fd, file, size) == -1) { close(fd); printf("Error: Failed to write new file \'%s\'\n", path); return EXIT_FAILURE; } close(fd); return EXIT_SUCCESS; } int save_woody(t_elf_content *woody) { char *woody_file = malloc(woody->file_size); if (!woody_file) return ft_put_error("Allocation error"); ft_memcpy(woody_file, woody->file, woody->file_size); if (munmap(woody->file, woody->file_size)) return ft_put_error("Umapping error"); int save_error = save_file("woody", woody_file, woody->file_size); if (save_error) return save_error; free(woody_file); return EXIT_SUCCESS; } int main(int ac, char **av) { t_elf_content woody; if (ac != 2) { return ft_put_error("Woody_woodpacker take 1 argument\n"); } woody.file_path = av[1]; int elf_error = get_elf_file(&woody); if (elf_error) return elf_error; elf_error = get_elf_sections(&woody); if (elf_error) return elf_error; int inject_error = inject(&woody); if (!inject_error) { return save_woody(&woody); } return EXIT_SUCCESS; }