#include #include #include #include static int buffer[65536]; int MaxAddress = 0; int LineAnalysis(char *line, int *ByteCount, int *Address, int *RType, int Data[], int *TempAdd){ char ArrTemp[20]; int sum, CheckSum, Pos = 9; if(line[0] != ':' || strlen(line) < 11) return 1; strncpy(ArrTemp, line+1, 2); ArrTemp[2] = '\0'; *ByteCount = strtol(ArrTemp, NULL, 16); ArrTemp[0] = '\0'; if(*ByteCount != ((strlen(line)-11)/2)) return 1; strncpy(ArrTemp, line+3, 4); ArrTemp[4] = '\0'; *Address = strtol(ArrTemp, NULL, 16); ArrTemp[0] = '\0'; strncpy(ArrTemp, line+7, 2); ArrTemp[2] = '\0'; *RType = strtol(ArrTemp, NULL, 16); ArrTemp[0] = '\0'; if(*RType > 5) return 1; sum = (*ByteCount & 255) + ((*Address >> 8) & 255) + (*Address & 255) + (*RType & 255); if(*RType == 4){ strncpy(ArrTemp, line+Pos, 4); ArrTemp[4] = '\0'; *TempAdd = strtol(ArrTemp, NULL, 16); ArrTemp[0] = '\0'; } for(int i=0; i < *ByteCount; i++){ strncpy(ArrTemp, line+Pos, 2); ArrTemp[2] = '\0'; Data[i] = strtol(ArrTemp, NULL, 16); sum += (Data[i] & 255); ArrTemp[0] = '\0'; Pos += 2; } strncpy(ArrTemp, line+Pos, 2); ArrTemp[2] = '\0'; CheckSum = strtol(ArrTemp, NULL, 16); ArrTemp[0] = '\0'; if(((sum & 255) + (CheckSum & 255)) & 255){ printf("This line has ERROR CHECKSUM \n"); return 1; } return 0; } int OpenFile(char *FileHex){ int NumLine = 1; int ByteCount, Address, RType, Data[256], TempAdd, flag; char line[300]; FILE *fp; fp = fopen(FileHex, "r"); //mo file Hex //Neu khong mo duoc file Hex se dua ra thong bao if(fp == NULL){ printf("Error: %s could not be opened.", FileHex); return 1; } while(!feof(fp)){ line[0] = '\0'; fgets(line, 300, fp); if(ferror(fp)){ printf("Error when read Line %d", NumLine); return 1; } if(line[strlen(line)-1]=='\n' || line[strlen(line)-1]=='\r') line[strlen(line)-1] = '\0'; if(LineAnalysis(line, &ByteCount, &Address, &RType, Data, &TempAdd) == 1){ printf("ERROR: Line %d", NumLine); return 1; } else{ if(RType == 1){ fclose(fp); return 0; } if(RType == 4 && TempAdd > 0) flag = 0; if(RType == 4 && TempAdd == 0) flag = 1; if(RType == 0 && flag == 1){ for(int i = 0; i < ByteCount; i++){ buffer[Address] = Data[i]; if(Address > MaxAddress) MaxAddress = Address; Address++; } } } NumLine++; } return 0; } int main(int argc, char *argv[]){ int LineCount = 1, choose, TempData; if(argc != 2){ printf("Need Hex file name.\n"); printf("To run, type 'HexDisplay Hex_File_Name.hex'\n"); return 0; } for(int i = 0; i < 65536; i++){ buffer[i] = 255; } if(OpenFile(argv[1]) == 1) return 0; for(int i = 0; i <= MaxAddress; i++){ if(LineCount % 25 == 1 && LineCount != 1){ printf("Do you want to continue printing?\n"); printf("1: Yes 2: No\n"); scanf("%d", &choose); if(choose == 1){ system("cls"); LineCount = 1; } else if(choose == 2){ printf("Thanks for using. Bye bye <3"); return 0; } } if(i % 16 == 0){ printf("%04X : %02X ", i, buffer[i]); } else if(i % 16 == 15){ printf("%02X : ", buffer[i]); for(int j = i - 15; j <= i; j++){ if(buffer[j] < 32) TempData = 46; else TempData = buffer[j]; printf("%c ", TempData); } printf("\n"); LineCount++; } else printf("%02X ", buffer[i]); if(i == MaxAddress && (i % 16) != 15){ for(int j = i+1; j <= (i + (15 - (i%16))); j++){ printf("%2X ", buffer[j]); } printf(": "); for(int j = i - (i%16); j <= (i + (15 - (i%16))); j++){ if(buffer[j] < 32) TempData = 46; else TempData = buffer[j]; printf("%c ", TempData); } printf("\n"); } } printf("End program. Thanks for using. Bye bye <3"); return 0; }