diff --git a/Makefile b/Makefile index 7c41f03..1cd28f8 100644 --- a/Makefile +++ b/Makefile @@ -2,7 +2,10 @@ NAME = woody_woodpacker SRCS_PATH = srcs/ -SRCS = $(SRCS_PATH)main.c +SRCS = $(SRCS_PATH)main.c \ + $(SRCS_PATH)utils.c \ + $(SRCS_PATH)woody.c + OBJS = ${SRCS:.c=.o} @@ -17,7 +20,7 @@ CFLAGS = -Wall -Wextra -Werror all: ${NAME} .c.o: - ${CC} ${DEFINES} ${CFLAGS} -c $< -o $@ + ${CC} ${INCLUDES} ${DEFINES} ${CFLAGS} -c $< -o $@ $(NAME): ${OBJS} make -C ft_printf diff --git a/includes/woody.h b/includes/woody.h new file mode 100644 index 0000000..a9005cc --- /dev/null +++ b/includes/woody.h @@ -0,0 +1,31 @@ +#ifndef WOODY_H +# define WOODY_H + +#include "../ft_printf/includes/ft_printf.h" +#include +#include +#include +#include +#include +#include +#include +#include +#include + +typedef struct efl_content +{ + long unsigned int file_size; + char *file_path; + char *file; + +} t_efl_content; + + +// utils.c +void *secure_access(char *file, unsigned long file_size, unsigned long offset_to_data, unsigned long supposed_data_size); +int ft_put_error(char *str); + +// woody.c +int woody(t_efl_content *file_content); + +#endif \ No newline at end of file diff --git a/srcs/main.c b/srcs/main.c index 5aa1174..a5d84b1 100644 --- a/srcs/main.c +++ b/srcs/main.c @@ -1,4 +1,46 @@ -int main() -{ +#include "../includes/woody.h" +int get_elf_file(t_efl_content *file_content) +{ + int fd; + off_t off; + + fd = open(file_content->file_path, O_RDONLY); + if (fd < 0) + { + ft_printf("Error: Failed to open \'%s\'\n", file_content->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", file_content->file_path); + return EXIT_FAILURE; + } + file_content->file_size = off; + file_content->file = mmap(NULL, file_content->file_size, PROT_READ, MAP_PRIVATE, fd, 0); + if (file_content->file == MAP_FAILED) + { + close(fd); + ft_printf("Error: Failed to map file \'%s\'\n", file_content->file_path); + return EXIT_FAILURE; + } + close(fd); + return EXIT_SUCCESS; +} + +int main(int ac, char **av) +{ + t_efl_content file_content; + if (ac != 2) + { + return ft_put_error("Woody_woodpacker take 1 argument\n"); + } + file_content.file_path = av[1]; + int ret = get_elf_file(&file_content); + if (ret == EXIT_FAILURE) + return ret; + + return woody(&file_content); } \ No newline at end of file diff --git a/srcs/utils.c b/srcs/utils.c new file mode 100644 index 0000000..81a6e3c --- /dev/null +++ b/srcs/utils.c @@ -0,0 +1,16 @@ +#include "../includes/woody.h" + +void *secure_access(char *file, unsigned long file_size, unsigned long offset_to_data, unsigned long supposed_data_size) +{ + if (file_size > offset_to_data && file_size >= (offset_to_data + supposed_data_size)) + return (file + offset_to_data); + return NULL; +} + +int ft_put_error(char *str) +{ + ft_putstr_fd("Error: ", STDERR_FILENO); + ft_putstr_fd(str, 2); + ft_putstr_fd("\n", STDERR_FILENO); + return EXIT_FAILURE; +} diff --git a/srcs/woody.c b/srcs/woody.c new file mode 100644 index 0000000..57f8a3f --- /dev/null +++ b/srcs/woody.c @@ -0,0 +1,12 @@ +#include "../includes/woody.h" + +int woody(t_efl_content *file_content) +{ + Elf64_Ehdr *Ehdr = (Elf64_Ehdr *)secure_access(file_content->file, file_content->file_size, 0, sizeof(Elf64_Ehdr)); + + if (!Ehdr) + return EXIT_FAILURE; + + // if (Ehdr->e_ident[EI_CLASS]) + return EXIT_SUCCESS; +} \ No newline at end of file