Advertisement
Guest User

Binary include for bin86/as86

a guest
Mar 16th, 2015
260
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 2.55 KB | None | 0 0
  1. #include <sys/types.h>
  2. #include <unistd.h>
  3. #include <fcntl.h>
  4. #include <stdio.h>
  5. #include <stdlib.h>
  6. #include <string.h>
  7.  
  8. void printUsageHelp()
  9. {
  10.     printf("as86 binary includer\n\n");
  11.     printf("\t Usage: as86_binin assembler_binary as86_symbol_file label_name include_file\n\n");
  12.     printf("as86_binin replaces the content of the 'assembler_binary' starting from the offset of the label 'label_name' found in 'as86_symbol_file' with the content of 'include_file'. Make sure you created enough space in the binary, i.e. by using .SPACE\n");
  13. }
  14.  
  15. int main(int argc, char* argv[]) {
  16.     if (argc != 5) {
  17.         printUsageHelp();
  18.         return 1;
  19.     }
  20.    
  21.     int bin_desc, symbol_desc, inc_desc;
  22.    
  23.     if ((bin_desc = open(argv[1], O_RDWR)) == -1)
  24.     {
  25.         printf("Could not open binary file '%s'\n", argv[1]);
  26.         return 1;
  27.     }
  28.     if ((symbol_desc = open(argv[2], O_RDONLY)) == -1)
  29.     {
  30.         close(bin_desc);
  31.         printf("Could not open symbol file '%s'\n", argv[2]);
  32.         return 1;
  33.     }
  34.     if((inc_desc = open(argv[4], O_RDONLY)) == -1)
  35.     {
  36.         close(bin_desc);
  37.         close(symbol_desc);
  38.         printf("Could not open include file '%s'\n", argv[4]);
  39.         return 1;
  40.     }
  41.    
  42.     long size = lseek(symbol_desc, 0, SEEK_END);
  43.     lseek(symbol_desc, 0, SEEK_SET);
  44.    
  45.     char *buffer = (char*)malloc(size + 1);
  46.     read(symbol_desc, buffer, size);
  47.     buffer[size + 1] = 0;
  48.    
  49.     int search_lenght = strlen(argv[3]);
  50.     char *pattern = (char*)malloc(search_lenght + 2);
  51.     pattern[0] = ' ';
  52.     strcpy(pattern + 1, argv[3]);
  53.    
  54.     char* hex_string = strstr(buffer, pattern);
  55.    
  56.     if (hex_string == 0 || hex_string < buffer + 14) {
  57.         printf("Incompatible symbol file '%s'\n", argv[2]);
  58.        
  59.         free(buffer);
  60.         free(pattern);
  61.    
  62.         close(bin_desc);
  63.         close(symbol_desc);
  64.         close(inc_desc);
  65.  
  66.         return 1;
  67.     }
  68.    
  69.     hex_string -= 14; // hex number sequence is 14 chars in front of the symbol name with whitespace (simply the file format)
  70.    
  71.     long bin_offset = strtol(hex_string, 0, 16);
  72.    
  73.     free(buffer);
  74.     free(pattern);
  75.     close(symbol_desc);
  76.    
  77.    
  78.     size = lseek(inc_desc, 0, SEEK_END);
  79.     lseek(inc_desc, 0, SEEK_SET);
  80.    
  81.     buffer = (char*)malloc(size);
  82.    
  83.     lseek(bin_desc, bin_offset, SEEK_SET);
  84.    
  85.     if (read(inc_desc, buffer, size) == -1)
  86.     {
  87.         printf("Could read from include file '%s'\n", argv[4]);
  88.        
  89.         free(buffer);
  90.    
  91.         close(bin_desc);   
  92.         close(inc_desc);
  93.    
  94.         return 1;
  95.     }
  96.     if (write(bin_desc, buffer, size) == -1)
  97.     {
  98.         printf("Could not write to binary file '%s'\n", argv[1]);
  99.        
  100.         free(buffer);
  101.    
  102.         close(bin_desc);   
  103.         close(inc_desc);
  104.    
  105.         return 1;
  106.     }
  107.    
  108.     free(buffer);
  109.    
  110.     close(bin_desc);   
  111.     close(inc_desc);
  112.    
  113.     return 0;
  114. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement