Advertisement
Bewin

relocationLoader

Nov 10th, 2024 (edited)
76
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 6.88 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <string.h>
  4.  
  5. // Define constants for max line length and text record size
  6. #define Max_Line_Length 100
  7. #define Max_Text_Records 10
  8.  
  9. // Define global variables to store header and end information
  10. char Header[Max_Line_Length];
  11. char End[Max_Line_Length];
  12.  
  13. // Define structure for storing text records with address, length, and the rest of the line
  14. typedef struct TextRecords {
  15.     int address;  // Address of the text record
  16.     int length;   // Length of the text record
  17.     char restOfLine[Max_Line_Length];  // Rest of the text record line
  18. } TextRecords;
  19.  
  20. // Array to store text records and a counter to track the number of records
  21. TextRecords textRecords[Max_Text_Records];
  22. int textRecordCount = 0;
  23.  
  24. // Function to replace a portion of a string with a modified hexadecimal value
  25. char *replaceByDigits(char *to_replace, int start, int size) {
  26.     // Allocate memory for the string to be replaced
  27.     char *to_be_replaced = (char *) malloc((size + 1) * sizeof(char));
  28.     strcpy(to_be_replaced, to_replace);
  29.  
  30.     // Convert the string to a hexadecimal number
  31.     unsigned int hex_value = strtol(to_be_replaced, NULL, 16);
  32.  
  33.     // Extract the last 5 digits of the hexadecimal value
  34.     unsigned int last_five_digits = hex_value & 0xFFFFF;
  35.  
  36.     // Add the 'start' value to the last 5 digits
  37.     unsigned int modified_digits = last_five_digits + start;
  38.  
  39.     // Combine the modified digits with the original value (keeping the other bits intact)
  40.     hex_value = (hex_value & ~0xFFFFF) | (modified_digits & 0xFFFFF);
  41.  
  42.     // Convert the modified hexadecimal value back to a string
  43.     sprintf(to_be_replaced, "%08X", hex_value);
  44.  
  45.     return to_be_replaced;  // Return the modified string
  46. }
  47.  
  48. // Function to process a modification record and update the text records
  49. void processModification(char *line, int new_start) {
  50.     char rest_of_line[Max_Line_Length];
  51.     int address, size;
  52.  
  53.     // Parse the modification line to extract address, size, and rest of the line
  54.     sscanf(line, "M %X %X %[^\n]", &address, &size, rest_of_line);
  55.     char *to_replace = NULL;
  56.     int count = -1;
  57.  
  58.     // Loop through each text record to find the one that matches the address
  59.     for (int i = 0; i < textRecordCount; ++i) {
  60.         char text_object_code[Max_Line_Length];
  61.         strcpy(text_object_code, textRecords[i].restOfLine);
  62.  
  63.         // Count the bytes to find the matching address
  64.         if (count < address) {
  65.             char *token = strtok(text_object_code, " ");
  66.             count += strlen(token) / 2;
  67.  
  68.             // Keep tokenizing the text object code to find the correct token
  69.             while (count < address && token != NULL) {
  70.                 token = strtok(NULL, " ");
  71.                 if (token == NULL) {
  72.                     continue;
  73.                 }
  74.                 count += strlen(token) / 2;
  75.             }
  76.  
  77.             // If the address is found, replace the required part
  78.             if (count >= address) {
  79.                 to_replace = token;
  80.                 char *new_value = replaceByDigits(to_replace, new_start, size);
  81.                 if (new_value != NULL) {
  82.                     char *pos = strstr(textRecords[i].restOfLine, to_replace);
  83.                     if (pos != NULL) {
  84.                         int token_length = strlen(to_replace);
  85.                         // Replace the original token with the new value
  86.                         strncpy(pos, new_value, token_length);
  87.                     }
  88.                     free(new_value);  // Free the allocated memory for the new value
  89.                 }
  90.                 printf("\nRelocating %s", to_replace);  // Print the relocated token
  91.                 break;
  92.             }
  93.         }
  94.     }
  95. }
  96.  
  97. // Main function to process the input, modify the text records, and write to the output
  98. int main() {
  99.     printf("Relocation Loader...\n");
  100.  
  101.     // Open the input, output, and loader files
  102.     FILE *f1, *f2, *f3;
  103.     if ((f1 = fopen("input.txt", "r")) == NULL ||
  104.         (f2 = fopen("output.txt", "w")) == NULL ||
  105.         (f3 = fopen("loader.txt", "w")) == NULL) {
  106.         printf("Error opening files\n");
  107.         exit(1);  // Exit if files cannot be opened
  108.     }
  109.  
  110.     int new_start, old_start, length;
  111.     int address, text_length;
  112.     char name[7], restOfLine[Max_Line_Length];
  113.     char line[Max_Line_Length];
  114.  
  115.     // Ask the user for the new starting location
  116.     printf("Enter new starting location: ");
  117.     scanf("%X", &new_start);
  118.  
  119.     // Process each line from the input file
  120.     while (fgets(line, sizeof(line), f1)) {
  121.         // Process header line
  122.         if (line[0] == 'H') {
  123.             sscanf(line, "H %s %X %d", name, &old_start, &length);
  124.             snprintf(Header, Max_Line_Length, "H %s %06X %d", name, new_start, length);
  125.         }
  126.             // Process text record line
  127.         else if (line[0] == 'T') {
  128.             sscanf(line, "T %X %X %[^\n]", &address, &text_length, restOfLine);
  129.             snprintf(textRecords[textRecordCount].restOfLine, Max_Line_Length, "%s", restOfLine);
  130.             textRecords[textRecordCount].address = new_start + textRecords[textRecordCount - 1].length;
  131.             textRecords[textRecordCount].length = text_length;
  132.             textRecordCount++;  // Increment text record count
  133.         }
  134.             // Process modification line
  135.         else if (line[0] == 'M') {
  136.             processModification(line, new_start);
  137.         }
  138.             // Process end line
  139.         else if (line[0] == 'E') {
  140.             snprintf(End, Max_Line_Length, "E %06X", new_start);
  141.         }
  142.     }
  143.  
  144.     // Write the header to the output file
  145.     fprintf(f2, "%s\n", Header);
  146.  
  147.     // Write each text record to the output file
  148.     for (int i = 0; i < textRecordCount; ++i) {
  149.         fprintf(f2, "T %06X %02X %s\n", textRecords[i].address, textRecords[i].length, textRecords[i].restOfLine);
  150.  
  151.         // Write each token in the text record to the loader file
  152.         char *token = strtok(textRecords[i].restOfLine, " ");
  153.         while (token != NULL) {
  154.             int len = strlen(token);
  155.             while (len > 0) {
  156.                 fprintf(f3, "%06X %c%c\n", textRecords[i].address++, token[0], token[1]);
  157.                 len -= 2;
  158.                 token += 2;
  159.             }
  160.             token = strtok(NULL, " ");
  161.         }
  162.     }
  163.  
  164.     // Write the end record to the output file
  165.     fprintf(f2, "%s\n", End);
  166.  
  167.     // Close all opened files
  168.     fclose(f1);
  169.     fclose(f2);
  170.  
  171.     return 0;  // Return from main function
  172. }
  173.  
  174.  
  175.  
  176.  
  177.  
  178. /*
  179. input.txt
  180. ------------------------------------------------------------------------------
  181. H COPY 001000 000030
  182. T 001000 1D 17202D 69202D 4B101036 032026 290000 332007 4B10105D 3F2FEC 032010
  183. T 00101D 13 0F2016 010003 0F200D 4B10005D 3E2003 454F46
  184. M 000007 05 + COPY
  185. M 000014 05 + COPY
  186. M 000027 05 + COPY
  187. E 001000
  188. ------------------------------------------------------------------------------
  189. */
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement