Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <stdlib.h>
- #include <string.h>
- // Define constants for max line length and text record size
- #define Max_Line_Length 100
- #define Max_Text_Records 10
- // Define global variables to store header and end information
- char Header[Max_Line_Length];
- char End[Max_Line_Length];
- // Define structure for storing text records with address, length, and the rest of the line
- typedef struct TextRecords {
- int address; // Address of the text record
- int length; // Length of the text record
- char restOfLine[Max_Line_Length]; // Rest of the text record line
- } TextRecords;
- // Array to store text records and a counter to track the number of records
- TextRecords textRecords[Max_Text_Records];
- int textRecordCount = 0;
- // Function to replace a portion of a string with a modified hexadecimal value
- char *replaceByDigits(char *to_replace, int start, int size) {
- // Allocate memory for the string to be replaced
- char *to_be_replaced = (char *) malloc((size + 1) * sizeof(char));
- strcpy(to_be_replaced, to_replace);
- // Convert the string to a hexadecimal number
- unsigned int hex_value = strtol(to_be_replaced, NULL, 16);
- // Extract the last 5 digits of the hexadecimal value
- unsigned int last_five_digits = hex_value & 0xFFFFF;
- // Add the 'start' value to the last 5 digits
- unsigned int modified_digits = last_five_digits + start;
- // Combine the modified digits with the original value (keeping the other bits intact)
- hex_value = (hex_value & ~0xFFFFF) | (modified_digits & 0xFFFFF);
- // Convert the modified hexadecimal value back to a string
- sprintf(to_be_replaced, "%08X", hex_value);
- return to_be_replaced; // Return the modified string
- }
- // Function to process a modification record and update the text records
- void processModification(char *line, int new_start) {
- char rest_of_line[Max_Line_Length];
- int address, size;
- // Parse the modification line to extract address, size, and rest of the line
- sscanf(line, "M %X %X %[^\n]", &address, &size, rest_of_line);
- char *to_replace = NULL;
- int count = -1;
- // Loop through each text record to find the one that matches the address
- for (int i = 0; i < textRecordCount; ++i) {
- char text_object_code[Max_Line_Length];
- strcpy(text_object_code, textRecords[i].restOfLine);
- // Count the bytes to find the matching address
- if (count < address) {
- char *token = strtok(text_object_code, " ");
- count += strlen(token) / 2;
- // Keep tokenizing the text object code to find the correct token
- while (count < address && token != NULL) {
- token = strtok(NULL, " ");
- if (token == NULL) {
- continue;
- }
- count += strlen(token) / 2;
- }
- // If the address is found, replace the required part
- if (count >= address) {
- to_replace = token;
- char *new_value = replaceByDigits(to_replace, new_start, size);
- if (new_value != NULL) {
- char *pos = strstr(textRecords[i].restOfLine, to_replace);
- if (pos != NULL) {
- int token_length = strlen(to_replace);
- // Replace the original token with the new value
- strncpy(pos, new_value, token_length);
- }
- free(new_value); // Free the allocated memory for the new value
- }
- printf("\nRelocating %s", to_replace); // Print the relocated token
- break;
- }
- }
- }
- }
- // Main function to process the input, modify the text records, and write to the output
- int main() {
- printf("Relocation Loader...\n");
- // Open the input, output, and loader files
- FILE *f1, *f2, *f3;
- if ((f1 = fopen("input.txt", "r")) == NULL ||
- (f2 = fopen("output.txt", "w")) == NULL ||
- (f3 = fopen("loader.txt", "w")) == NULL) {
- printf("Error opening files\n");
- exit(1); // Exit if files cannot be opened
- }
- int new_start, old_start, length;
- int address, text_length;
- char name[7], restOfLine[Max_Line_Length];
- char line[Max_Line_Length];
- // Ask the user for the new starting location
- printf("Enter new starting location: ");
- scanf("%X", &new_start);
- // Process each line from the input file
- while (fgets(line, sizeof(line), f1)) {
- // Process header line
- if (line[0] == 'H') {
- sscanf(line, "H %s %X %d", name, &old_start, &length);
- snprintf(Header, Max_Line_Length, "H %s %06X %d", name, new_start, length);
- }
- // Process text record line
- else if (line[0] == 'T') {
- sscanf(line, "T %X %X %[^\n]", &address, &text_length, restOfLine);
- snprintf(textRecords[textRecordCount].restOfLine, Max_Line_Length, "%s", restOfLine);
- textRecords[textRecordCount].address = new_start + textRecords[textRecordCount - 1].length;
- textRecords[textRecordCount].length = text_length;
- textRecordCount++; // Increment text record count
- }
- // Process modification line
- else if (line[0] == 'M') {
- processModification(line, new_start);
- }
- // Process end line
- else if (line[0] == 'E') {
- snprintf(End, Max_Line_Length, "E %06X", new_start);
- }
- }
- // Write the header to the output file
- fprintf(f2, "%s\n", Header);
- // Write each text record to the output file
- for (int i = 0; i < textRecordCount; ++i) {
- fprintf(f2, "T %06X %02X %s\n", textRecords[i].address, textRecords[i].length, textRecords[i].restOfLine);
- // Write each token in the text record to the loader file
- char *token = strtok(textRecords[i].restOfLine, " ");
- while (token != NULL) {
- int len = strlen(token);
- while (len > 0) {
- fprintf(f3, "%06X %c%c\n", textRecords[i].address++, token[0], token[1]);
- len -= 2;
- token += 2;
- }
- token = strtok(NULL, " ");
- }
- }
- // Write the end record to the output file
- fprintf(f2, "%s\n", End);
- // Close all opened files
- fclose(f1);
- fclose(f2);
- return 0; // Return from main function
- }
- /*
- input.txt
- ------------------------------------------------------------------------------
- H COPY 001000 000030
- T 001000 1D 17202D 69202D 4B101036 032026 290000 332007 4B10105D 3F2FEC 032010
- T 00101D 13 0F2016 010003 0F200D 4B10005D 3E2003 454F46
- M 000007 05 + COPY
- M 000014 05 + COPY
- M 000027 05 + COPY
- E 001000
- ------------------------------------------------------------------------------
- */
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement