diff --git a/BurnOutSharp.sln b/BurnOutSharp.sln index 05dd7a56..4f21b965 100644 --- a/BurnOutSharp.sln +++ b/BurnOutSharp.sln @@ -24,7 +24,9 @@ Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "WixToolset.Dtf.Compression" EndProject Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "BurnOutSharp.Models", "BurnOutSharp.Models\BurnOutSharp.Models.csproj", "{44939210-BDC2-4250-BC0F-0AB8F316F09D}" EndProject -Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "BurnOutSharp.Builder", "BurnOutSharp.Builder\BurnOutSharp.Builder.csproj", "{7577733A-CC8D-4E7C-8B6D-FFC7EC1B3D07}" +Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "BurnOutSharp.Builder", "BurnOutSharp.Builder\BurnOutSharp.Builder.csproj", "{7577733A-CC8D-4E7C-8B6D-FFC7EC1B3D07}" +EndProject +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "ExecutableTest", "ExecutableTest\ExecutableTest.csproj", "{4B59824C-7E0A-4478-B408-FEAA1FD80F8F}" EndProject Global GlobalSection(SolutionConfigurationPlatforms) = preSolution @@ -64,6 +66,10 @@ Global {7577733A-CC8D-4E7C-8B6D-FFC7EC1B3D07}.Debug|Any CPU.Build.0 = Debug|Any CPU {7577733A-CC8D-4E7C-8B6D-FFC7EC1B3D07}.Release|Any CPU.ActiveCfg = Release|Any CPU {7577733A-CC8D-4E7C-8B6D-FFC7EC1B3D07}.Release|Any CPU.Build.0 = Release|Any CPU + {4B59824C-7E0A-4478-B408-FEAA1FD80F8F}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {4B59824C-7E0A-4478-B408-FEAA1FD80F8F}.Debug|Any CPU.Build.0 = Debug|Any CPU + {4B59824C-7E0A-4478-B408-FEAA1FD80F8F}.Release|Any CPU.ActiveCfg = Release|Any CPU + {4B59824C-7E0A-4478-B408-FEAA1FD80F8F}.Release|Any CPU.Build.0 = Release|Any CPU EndGlobalSection GlobalSection(SolutionProperties) = preSolution HideSolutionNode = FALSE diff --git a/ExecutableTest/ExecutableTest.csproj b/ExecutableTest/ExecutableTest.csproj new file mode 100644 index 00000000..ee83a18d --- /dev/null +++ b/ExecutableTest/ExecutableTest.csproj @@ -0,0 +1,15 @@ + + + + Exe + net6.0 + enable + enable + + + + + + + + diff --git a/ExecutableTest/Program.cs b/ExecutableTest/Program.cs new file mode 100644 index 00000000..dc08c3bb --- /dev/null +++ b/ExecutableTest/Program.cs @@ -0,0 +1,157 @@ +using BurnOutSharp.Builder; + +namespace ExecutableTest +{ + public class ExecutableTest + { + public static void Main(string[] args) + { + // Invalid arguments means nothing to do + if (args == null || args.Length == 0) + { + Console.WriteLine("Please provide at least one file path"); + Console.ReadLine(); + return; + } + + // Loop through the args + foreach (string arg in args) + { + // Check the file exists + if (!File.Exists(arg)) + { + Console.WriteLine($"{arg} does not exist or is not a file, skipping..."); + continue; + } + + using (Stream stream = File.OpenRead(arg)) + { + // Read the first 4 bytes + byte[] magic = stream.ReadBytes(2); + if (magic[0] != 'M' || magic[1] != 'Z') + { + Console.WriteLine("Not a recognized executable format, skipping..."); + Console.WriteLine(); + continue; + } + + // Build the executable information + Console.WriteLine("Creating MS-DOS executable builder"); + Console.WriteLine(); + + stream.Seek(0, SeekOrigin.Begin); + var msdos = MSDOS.ParseExecutable(stream); + if (msdos == null) + { + Console.WriteLine("Something went wrong parsing MS-DOS executable"); + Console.WriteLine(); + continue; + } + + // Print the executable info to screen + PrintMSDOS(msdos); + + // Check for a valid new executable address + if (msdos.Header.NewExeHeaderAddr >= stream.Length) + { + Console.WriteLine("New EXE header address invalid, skipping additional reading..."); + Console.WriteLine(); + continue; + } + + // Try to read the new executable info + stream.Seek(msdos.Header.NewExeHeaderAddr, SeekOrigin.Begin); + magic = stream.ReadBytes(4); + + // New Executable + if (magic[0] == 'N' && magic[1] == 'E') + { + Console.WriteLine($"New executable found. No parsing currently available."); + Console.WriteLine(); + continue; + + // TODO: Implement NE reading + } + + // Linear Executable + else if (magic[0] == 'L' && (magic[1] == 'E' || magic[1] == 'X')) + { + Console.WriteLine($"Linear executable found. No parsing currently available."); + Console.WriteLine(); + continue; + } + + // Portable Executable + else if (magic[0] == 'P' && magic[1] == 'E' && magic[2] == '\0' && magic[3] == '\0') + { + Console.WriteLine($"Portable executable found. No parsing currently available."); + Console.WriteLine(); + continue; + } + + // Unknown + else + { + Console.WriteLine($"Unrecognized header signature: {BitConverter.ToString(magic).Replace("-", string.Empty)}"); + Console.WriteLine(); + continue; + } + } + } + } + + /// + /// Pretty print the MS-DOS executable information + /// + /// + private static void PrintMSDOS(BurnOutSharp.Models.MSDOS.Executable executable) + { + Console.WriteLine("MS-DOS Executable Information:"); + Console.WriteLine("-------------------------"); + Console.WriteLine(); + + Console.WriteLine(" Header Information:"); + Console.WriteLine(" -------------------------"); + Console.WriteLine($" Magic number: {BitConverter.ToString(executable.Header.Magic).Replace("-", string.Empty)}"); + Console.WriteLine($" Last page bytes: {executable.Header.LastPageBytes}"); + Console.WriteLine($" Pages: {executable.Header.Pages}"); + Console.WriteLine($" Relocation items: {executable.Header.RelocationItems}"); + Console.WriteLine($" Header paragraph size: {executable.Header.HeaderParagraphSize}"); + Console.WriteLine($" Minimum extra paragraphs: {executable.Header.MinimumExtraParagraphs}"); + Console.WriteLine($" Maximum extra paragraphs: {executable.Header.MaximumExtraParagraphs}"); + Console.WriteLine($" Initial SS value: {executable.Header.InitialSSValue}"); + Console.WriteLine($" Initial SP value: {executable.Header.InitialSPValue}"); + Console.WriteLine($" Checksum: {executable.Header.Checksum}"); + Console.WriteLine($" Initial IP value: {executable.Header.InitialIPValue}"); + Console.WriteLine($" Initial CS value: {executable.Header.InitialCSValue}"); + Console.WriteLine($" Relocation table address: {executable.Header.RelocationTableAddr}"); + Console.WriteLine($" Overlay number: {executable.Header.OverlayNumber}"); + Console.WriteLine(); + + Console.WriteLine(" Extended Header Information:"); + Console.WriteLine(" -------------------------"); + Console.WriteLine($" Reserved words: {string.Join(", ", executable.Header.Reserved1)}"); + Console.WriteLine($" OEM identifier: {executable.Header.OEMIdentifier}"); + Console.WriteLine($" OEM information: {executable.Header.OEMInformation}"); + Console.WriteLine($" Reserved words: {string.Join(", ", executable.Header.Reserved2)}"); + Console.WriteLine($" New EXE header address: {executable.Header.NewExeHeaderAddr}"); + Console.WriteLine(); + + Console.WriteLine(" Relocation Table Information:"); + Console.WriteLine(" -------------------------"); + if (executable.Header.RelocationItems == 0 || executable.RelocationTable.Length == 0) + { + Console.WriteLine(" No relocation table items"); + } + else + { + for (int i = 0; i < executable.RelocationTable.Length; i++) + { + var entry = executable.RelocationTable[i]; + Console.WriteLine($" Relocation Table Entry {i} - Offset = {entry.Offset}, Segment = {entry.Segment}"); + } + } + Console.WriteLine(); + } + } +} \ No newline at end of file