From 4ff203f393cd7389196c9d190051dc15bffeedd2 Mon Sep 17 00:00:00 2001 From: Matt Nadareski Date: Wed, 13 Sep 2023 22:46:46 -0400 Subject: [PATCH] Port MZ to new printing --- BinaryObjectScanner.Printing/MSDOS.cs | 81 +++++++++++++++++++ .../MicrosoftCabinet.cs | 7 +- BinaryObjectScanner.Wrappers/MSDOS.cs | 64 +-------------- 3 files changed, 85 insertions(+), 67 deletions(-) create mode 100644 BinaryObjectScanner.Printing/MSDOS.cs diff --git a/BinaryObjectScanner.Printing/MSDOS.cs b/BinaryObjectScanner.Printing/MSDOS.cs new file mode 100644 index 00000000..0e09911f --- /dev/null +++ b/BinaryObjectScanner.Printing/MSDOS.cs @@ -0,0 +1,81 @@ +using System.Text; +using SabreTools.Models.MSDOS; + +namespace BinaryObjectScanner.Printing +{ + public static class MSDOS + { + public static void Print(StringBuilder builder, Executable executable) + { + builder.AppendLine("MS-DOS Executable Information:"); + builder.AppendLine("-------------------------"); + builder.AppendLine(); + + Print(builder, executable.Header); + Print(builder, executable.RelocationTable); + } + +#if NET48 + private static void Print(StringBuilder builder, ExecutableHeader header) +#else + private static void Print(StringBuilder builder, ExecutableHeader? header) +#endif + { + builder.AppendLine(" Header Information:"); + builder.AppendLine(" -------------------------"); + if (header == null) + { + builder.AppendLine(" No header"); + builder.AppendLine(); + return; + } + + builder.AppendLine($" Magic number: {header.Magic}"); + builder.AppendLine($" Last page bytes: {header.LastPageBytes} (0x{header.LastPageBytes:X})"); + builder.AppendLine($" Pages: {header.Pages} (0x{header.Pages:X})"); + builder.AppendLine($" Relocation items: {header.RelocationItems} (0x{header.RelocationItems:X})"); + builder.AppendLine($" Header paragraph size: {header.HeaderParagraphSize} (0x{header.HeaderParagraphSize:X})"); + builder.AppendLine($" Minimum extra paragraphs: {header.MinimumExtraParagraphs} (0x{header.MinimumExtraParagraphs:X})"); + builder.AppendLine($" Maximum extra paragraphs: {header.MaximumExtraParagraphs} (0x{header.MaximumExtraParagraphs:X})"); + builder.AppendLine($" Initial SS value: {header.InitialSSValue} (0x{header.InitialSSValue:X})"); + builder.AppendLine($" Initial SP value: {header.InitialSPValue} (0x{header.InitialSPValue:X})"); + builder.AppendLine($" Checksum: {header.Checksum} (0x{header.Checksum:X})"); + builder.AppendLine($" Initial IP value: {header.InitialIPValue} (0x{header.InitialIPValue:X})"); + builder.AppendLine($" Initial CS value: {header.InitialCSValue} (0x{header.InitialCSValue:X})"); + builder.AppendLine($" Relocation table address: {header.RelocationTableAddr} (0x{header.RelocationTableAddr:X})"); + builder.AppendLine($" Overlay number: {header.OverlayNumber} (0x{header.OverlayNumber:X})"); + builder.AppendLine(); + } + +#if NET48 + private static void Print(StringBuilder builder, RelocationEntry[] entries) +#else + private static void Print(StringBuilder builder, RelocationEntry?[]? entries) +#endif + { + builder.AppendLine(" Relocation Table Information:"); + builder.AppendLine(" -------------------------"); + if (entries == null || entries.Length == 0) + { + builder.AppendLine(" No relocation table items"); + builder.AppendLine(); + return; + } + + for (int i = 0; i < entries.Length; i++) + { + var entry = entries[i]; + builder.AppendLine($" Relocation Table Entry {i}"); + if (entry == null) + { + builder.AppendLine($" [NULL]"); + continue; + } + + builder.AppendLine($" Offset: {entry.Offset} (0x{entry.Offset:X})"); + builder.AppendLine($" Segment: {entry.Segment} (0x{entry.Segment:X})"); + } + builder.AppendLine(); + } + } +} \ No newline at end of file diff --git a/BinaryObjectScanner.Printing/MicrosoftCabinet.cs b/BinaryObjectScanner.Printing/MicrosoftCabinet.cs index 18fdc56e..b481df54 100644 --- a/BinaryObjectScanner.Printing/MicrosoftCabinet.cs +++ b/BinaryObjectScanner.Printing/MicrosoftCabinet.cs @@ -6,7 +6,6 @@ namespace BinaryObjectScanner.Printing { public static class MicrosoftCabinet { - public static void Print(StringBuilder builder, Cabinet cabinet) { builder.AppendLine("Microsoft Cabinet Information:"); @@ -15,7 +14,7 @@ namespace BinaryObjectScanner.Printing Print(builder, cabinet.Header); Print(builder, cabinet.Folders); - PrintFiles(builder, cabinet.Files); + Print(builder, cabinet.Files); } #if NET48 @@ -131,9 +130,9 @@ namespace BinaryObjectScanner.Printing } #if NET48 - private static void PrintFiles(StringBuilder builder, CFFILE[] entries) + private static void Print(StringBuilder builder, CFFILE[] entries) #else - private static void PrintFiles(StringBuilder builder, CFFILE?[]? entries) + private static void Print(StringBuilder builder, CFFILE?[]? entries) #endif { builder.AppendLine(" Files:"); diff --git a/BinaryObjectScanner.Wrappers/MSDOS.cs b/BinaryObjectScanner.Wrappers/MSDOS.cs index 547cf653..c01ce6a8 100644 --- a/BinaryObjectScanner.Wrappers/MSDOS.cs +++ b/BinaryObjectScanner.Wrappers/MSDOS.cs @@ -252,72 +252,10 @@ namespace BinaryObjectScanner.Wrappers public override StringBuilder PrettyPrint() { StringBuilder builder = new StringBuilder(); - - builder.AppendLine("MS-DOS Executable Information:"); - builder.AppendLine("-------------------------"); - builder.AppendLine(); - - PrintHeader(builder); - PrintRelocationTable(builder); - + Printing.MSDOS.Print(builder, _model); return builder; } - /// - /// Print header information - /// - /// StringBuilder to append information to - private void PrintHeader(StringBuilder builder) - { - builder.AppendLine(" Header Information:"); - builder.AppendLine(" -------------------------"); - builder.AppendLine($" Magic number: {Magic}"); - builder.AppendLine($" Last page bytes: {LastPageBytes} (0x{LastPageBytes:X})"); - builder.AppendLine($" Pages: {Pages} (0x{Pages:X})"); - builder.AppendLine($" Relocation items: {RelocationItems} (0x{RelocationItems:X})"); - builder.AppendLine($" Header paragraph size: {HeaderParagraphSize} (0x{HeaderParagraphSize:X})"); - builder.AppendLine($" Minimum extra paragraphs: {MinimumExtraParagraphs} (0x{MinimumExtraParagraphs:X})"); - builder.AppendLine($" Maximum extra paragraphs: {MaximumExtraParagraphs} (0x{MaximumExtraParagraphs:X})"); - builder.AppendLine($" Initial SS value: {InitialSSValue} (0x{InitialSSValue:X})"); - builder.AppendLine($" Initial SP value: {InitialSPValue} (0x{InitialSPValue:X})"); - builder.AppendLine($" Checksum: {Checksum} (0x{Checksum:X})"); - builder.AppendLine($" Initial IP value: {InitialIPValue} (0x{InitialIPValue:X})"); - builder.AppendLine($" Initial CS value: {InitialCSValue} (0x{InitialCSValue:X})"); - builder.AppendLine($" Relocation table address: {RelocationTableAddr} (0x{RelocationTableAddr:X})"); - builder.AppendLine($" Overlay number: {OverlayNumber} (0x{OverlayNumber:X})"); - } - - /// - /// Print relocation table information - /// - /// StringBuilder to append information to - private void PrintRelocationTable(StringBuilder builder) - { - builder.AppendLine(" Relocation Table Information:"); - builder.AppendLine(" -------------------------"); - if (RelocationItems == 0 || RelocationTable == null || RelocationTable.Length == 0) - { - builder.AppendLine(" No relocation table items"); - } - else - { - for (int i = 0; i < RelocationTable.Length; i++) - { - var entry = RelocationTable[i]; - builder.AppendLine($" Relocation Table Entry {i}"); - if (entry == null) - { - builder.AppendLine($" [NULL]"); - continue; - } - - builder.AppendLine($" Offset: {entry.Offset} (0x{entry.Offset:X})"); - builder.AppendLine($" Segment: {entry.Segment} (0x{entry.Segment:X})"); - } - } - builder.AppendLine(); - } - #if NET6_0_OR_GREATER ///