Advertisement
Guest User

Untitled

a guest
May 21st, 2019
67
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.47 KB | None | 0 0
  1. /*
  2. ** EPITECH PROJECT, 2019
  3. ** corewar
  4. ** File description:
  5. ** utils
  6. */
  7.  
  8. #include <fcntl.h>
  9. #include <unistd.h>
  10. #include <stdio.h>
  11. #include <stdlib.h>
  12. #include "../include/corewar.h"
  13.  
  14. int parse_name_comment(unsigned char *binary, corewar_t *vm, int i)
  15. {
  16. int index = 4;
  17. int index_str = 0;
  18.  
  19. for (; binary[index] != 0; index++) {
  20. if (binary[index] >= 32) {
  21. vm->champion[i]->header->prog_name[index_str] = binary[index];
  22. index_str++;
  23. }
  24. }
  25. vm->champion[i]->header->prog_name[index_str] = '\0';
  26. index_str = 0;
  27. for (; binary[index] == 0; index++);
  28. for (; binary[index] != 0; index++) {
  29. if (binary[index] >= 32) {
  30. vm->champion[i]->header->comment[index_str] = binary[index];
  31. index_str++;
  32. }
  33. }
  34. vm->champion[i]->header->comment[index_str] = '\0';
  35. return (0);
  36. }
  37.  
  38. int check_magic_number(unsigned char *binary, corewar_t *vm)
  39. {
  40. int magic_nb[] = {0, 234, 131, 243};
  41. int check = 0;
  42.  
  43. for (int i = 0; i < 4; i++)
  44. if (binary[i] == magic_nb[i])
  45. check++;
  46. if (check != 4)
  47. return (84);
  48. return (0);
  49. }
  50.  
  51. int parse_header(corewar_t *vm)
  52. {
  53. unsigned char *binary = NULL;
  54.  
  55. for (int i = 0; i < vm->nbr_champ; i++) {
  56. binary = open_file(vm->champion[i]->filepath, vm);
  57. if (check_magic_number(binary, vm) == 84)
  58. return (84);
  59. parse_name_comment(binary, vm, i);
  60. }
  61. return (0);
  62. }
  63.  
  64. int parsing_flags(corewar_t *vm)
  65. {
  66. int (*instruction[16])(corewar_t *vm) = {live, ld, st, add, sub, and_func, or_func
  67. , xor_func, zjmp, ldi, sti, fork_func, lld, lldi, lfork, aff};
  68. int index = 0;
  69.  
  70. for (; vm->parsing->index < vm->parsing->size; vm->parsing->index++) {
  71. if (vm->parsing->binary[vm->parsing->index] >= 1 && vm->parsing->binary[vm->parsing->index] <= 16) {
  72. index = vm->parsing->binary[vm->parsing->index];
  73. instruction[index - 1](vm);
  74. }
  75. }
  76. return (0);
  77. }
  78.  
  79. int initialize_corewar(corewar_t *vm)
  80. {
  81. vm->parsing = malloc(sizeof(parsing_t));
  82. if (!vm->parsing)
  83. return (84);
  84. vm->parsing->binary = NULL;
  85. vm->parsing->index = 0;
  86. vm->champion = malloc(sizeof(champion_t *) * 4);
  87. for (int x = 0; x < 4; x++)
  88. vm->champion[x] = malloc(sizeof(champion_t));
  89. vm->nbr_champ = 0;
  90. for (int i = 0; i < 4; i++) {
  91. vm->champion[i]->filepath = NULL;
  92. vm->champion[i]->address = 0;
  93. vm->champion[i]->header = malloc(sizeof(header_t));
  94. }
  95. for (int a = 0; a < MEM_SIZE; a++)
  96. vm->map[a] = 0;
  97. return (0);
  98. }
  99.  
  100. unsigned char *open_file(char *filepath, corewar_t *vm)
  101. {
  102. unsigned char *buffer = NULL;
  103. int fd = open(filepath, O_RDONLY);
  104. int size = 0;
  105. int return_value = 0;
  106.  
  107. if (fd < 0)
  108. return (NULL);
  109. size = get_size_file(filepath);
  110. if (size <= 0)
  111. return (NULL);
  112. vm->parsing->size = size;
  113. buffer = malloc(sizeof(unsigned char) * (size + 1));
  114. return_value = read(fd, buffer, size);
  115. if (return_value <= 0)
  116. return (NULL);
  117. buffer[return_value] = '\0';
  118. return (buffer);
  119. }
  120.  
  121. int get_size_file(char *filepath)
  122. {
  123. FILE *stream = fopen(filepath, "r");
  124. char *str = NULL;
  125. size_t nb = 0;
  126. int size = 0;
  127. int return_value = 0;
  128.  
  129. while ((return_value = getline(&str, &nb, stream)) != -1) {
  130. size += return_value;
  131. }
  132. return (size);
  133. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement