open first parameter as elf

This commit is contained in:
pbonilla 2024-02-14 11:37:05 +01:00
parent 8d69e7d1df
commit eea18f5651
5 changed files with 108 additions and 4 deletions

View File

@ -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

31
includes/woody.h Normal file
View File

@ -0,0 +1,31 @@
#ifndef WOODY_H
# define WOODY_H
#include "../ft_printf/includes/ft_printf.h"
#include <unistd.h>
#include <stdio.h>
#include <stdlib.h>
#include <assert.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <sys/mman.h>
#include <fcntl.h>
#include <elf.h>
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

View File

@ -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);
}

16
srcs/utils.c Normal file
View File

@ -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;
}

12
srcs/woody.c Normal file
View File

@ -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;
}