Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- ** EPITECH PROJECT, 2019
- ** corewar
- ** File description:
- ** utils
- */
- #include <fcntl.h>
- #include <unistd.h>
- #include <stdio.h>
- #include <stdlib.h>
- #include "../include/corewar.h"
- int parse_name_comment(unsigned char *binary, corewar_t *vm, int i)
- {
- int index = 4;
- int index_str = 0;
- for (; binary[index] != 0; index++) {
- if (binary[index] >= 32) {
- vm->champion[i]->header->prog_name[index_str] = binary[index];
- index_str++;
- }
- }
- vm->champion[i]->header->prog_name[index_str] = '\0';
- index_str = 0;
- for (; binary[index] == 0; index++);
- for (; binary[index] != 0; index++) {
- if (binary[index] >= 32) {
- vm->champion[i]->header->comment[index_str] = binary[index];
- index_str++;
- }
- }
- vm->champion[i]->header->comment[index_str] = '\0';
- return (0);
- }
- int check_magic_number(unsigned char *binary, corewar_t *vm)
- {
- int magic_nb[] = {0, 234, 131, 243};
- int check = 0;
- for (int i = 0; i < 4; i++)
- if (binary[i] == magic_nb[i])
- check++;
- if (check != 4)
- return (84);
- return (0);
- }
- int parse_header(corewar_t *vm)
- {
- unsigned char *binary = NULL;
- for (int i = 0; i < vm->nbr_champ; i++) {
- binary = open_file(vm->champion[i]->filepath, vm);
- if (check_magic_number(binary, vm) == 84)
- return (84);
- parse_name_comment(binary, vm, i);
- }
- return (0);
- }
- int parsing_flags(corewar_t *vm)
- {
- int (*instruction[16])(corewar_t *vm) = {live, ld, st, add, sub, and_func, or_func
- , xor_func, zjmp, ldi, sti, fork_func, lld, lldi, lfork, aff};
- int index = 0;
- for (; vm->parsing->index < vm->parsing->size; vm->parsing->index++) {
- if (vm->parsing->binary[vm->parsing->index] >= 1 && vm->parsing->binary[vm->parsing->index] <= 16) {
- index = vm->parsing->binary[vm->parsing->index];
- instruction[index - 1](vm);
- }
- }
- return (0);
- }
- int initialize_corewar(corewar_t *vm)
- {
- vm->parsing = malloc(sizeof(parsing_t));
- if (!vm->parsing)
- return (84);
- vm->parsing->binary = NULL;
- vm->parsing->index = 0;
- vm->champion = malloc(sizeof(champion_t *) * 4);
- for (int x = 0; x < 4; x++)
- vm->champion[x] = malloc(sizeof(champion_t));
- vm->nbr_champ = 0;
- for (int i = 0; i < 4; i++) {
- vm->champion[i]->filepath = NULL;
- vm->champion[i]->address = 0;
- vm->champion[i]->header = malloc(sizeof(header_t));
- }
- for (int a = 0; a < MEM_SIZE; a++)
- vm->map[a] = 0;
- return (0);
- }
- unsigned char *open_file(char *filepath, corewar_t *vm)
- {
- unsigned char *buffer = NULL;
- int fd = open(filepath, O_RDONLY);
- int size = 0;
- int return_value = 0;
- if (fd < 0)
- return (NULL);
- size = get_size_file(filepath);
- if (size <= 0)
- return (NULL);
- vm->parsing->size = size;
- buffer = malloc(sizeof(unsigned char) * (size + 1));
- return_value = read(fd, buffer, size);
- if (return_value <= 0)
- return (NULL);
- buffer[return_value] = '\0';
- return (buffer);
- }
- int get_size_file(char *filepath)
- {
- FILE *stream = fopen(filepath, "r");
- char *str = NULL;
- size_t nb = 0;
- int size = 0;
- int return_value = 0;
- while ((return_value = getline(&str, &nb, stream)) != -1) {
- size += return_value;
- }
- return (size);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement