From abe712f14b698f1e1634d2a74876f9e3cd2e4414 Mon Sep 17 00:00:00 2001 From: Muhammad Rizwan Date: Mon, 16 Oct 2023 11:47:55 +0300 Subject: [PATCH] Refactor: Hex Dump Highlight --- WebRISCV/Pages/Index.razor | 40 ++++++++++++++++++++++++-------------- 1 file changed, 25 insertions(+), 15 deletions(-) diff --git a/WebRISCV/Pages/Index.razor b/WebRISCV/Pages/Index.razor index 458f0b2..338fcbb 100644 --- a/WebRISCV/Pages/Index.razor +++ b/WebRISCV/Pages/Index.razor @@ -31,10 +31,10 @@
-
+
-
+
             @((MarkupString)(@output))
         
@@ -64,6 +64,12 @@
@code { + + enum ByteType + { + ELF, PE, HEX + } + private string fileContent { get; set; } = ""; private string output { get; set; } = ""; @@ -144,11 +150,10 @@ var finalData = new SharpRISCV.Core.Windows.Compile("").BuildPeNoCheckSum(); var finalDataWithCheckSum = new SharpRISCV.Core.Windows.Compile("").AddCheckSumForWeb(finalData); string output = BytesConsoleOutputBuilder.Build(finalData); + output = Highlight(output, ByteType.PE); this.output = $"RAW DUMP:{Environment.NewLine}"; this.output += output; - - //await DownloadFile(finalDataWithCheckSum, OutputType.PE); - + await DownloadFile(finalDataWithCheckSum, OutputType.PE); StateHasChanged(); } @@ -158,7 +163,7 @@ RiscVAssembler.Assamble(fileContent); var finalData = new SharpRISCV.Core.Elf.Compile("").bytes(); string output = BytesConsoleOutputBuilder.Build(finalData); - output = HighlightElf(output); + output = Highlight(output, ByteType.ELF); this.output = $"RAW DUMP:{Environment.NewLine}"; this.output += output; await DownloadFile(finalData.ToArray(), OutputType.ELF); @@ -174,7 +179,7 @@ } - string HighlightElf(string hexDump) + string Highlight(string hexDump, ByteType type) { var lines = hexDump.Split('\n', StringSplitOptions.RemoveEmptyEntries); @@ -189,7 +194,7 @@ result.Append($"{saddress} "); foreach (var byteStr in bytespart.Split(' ')) { - var highlightedByte = HighlightElfByte(byteStr, address); + var highlightedByte = HighlightByte(byteStr, address, type); result.Append(highlightedByte).Append(' '); address++; } @@ -211,7 +216,7 @@ "","p_type","p_flags","p_offset","p_vaddr","p_paddr","p_filesz","p_memsz","p_flags","p_align","CODE", }; CircularIdGenerator elfTipsGenerator = new CircularIdGenerator(elfTips); - List chnageAddress = new List + List elfChnageAddress = new List { // ELF Header 0x03,0x04,0x05,0x06,0x07,0x08,0x0F,0x11,0x13,0x17,0x1F,0x27,0x2F,0x34,0x35,0x37,0x39,0x3B,0x3D,0x3F, @@ -219,14 +224,19 @@ 0X40+0x03,0X40+0x07,0X40+0x0F,0X40+0x17,0X40+0x1F,0X40+0x27,0X40+0x2B,0X40+0x33,0X40+0x37 }; - string HighlightElfByte(string byteStr, int address) + string HighlightByte(string byteStr, int address, ByteType type) { - string color = circularColors.Current; - string tip = elfTipsGenerator.Current; - if (chnageAddress.Contains(address)) + string color = "black"; + string tip = string.Empty; + if (type == ByteType.ELF) { - color = circularColors.GetNextId(); - tip = elfTipsGenerator.GetNextId(); + color = circularColors.Current; + tip = elfTipsGenerator.Current; + if (elfChnageAddress.Contains(address)) + { + color = circularColors.GetNextId(); + tip = elfTipsGenerator.GetNextId(); + } } return $"{byteStr}"; }