From 01523da96c8a644f95d53a9c4335c02f2a9b9167 Mon Sep 17 00:00:00 2001 From: Natalia Portillo Date: Tue, 3 Oct 2017 20:38:08 +0100 Subject: [PATCH] Refactor MZ. --- exeinfo/MZ.cs | 64 ------------------------------------------ exeinfo/MZ/Consts.cs | 8 ++++++ exeinfo/MZ/Info.cs | 27 ++++++++++++++++++ exeinfo/MZ/Structs.cs | 37 ++++++++++++++++++++++++ exeinfo/Program.cs | 4 +-- exeinfo/exeinfo.csproj | 5 +++- 6 files changed, 78 insertions(+), 67 deletions(-) delete mode 100644 exeinfo/MZ.cs create mode 100644 exeinfo/MZ/Consts.cs create mode 100644 exeinfo/MZ/Info.cs create mode 100644 exeinfo/MZ/Structs.cs diff --git a/exeinfo/MZ.cs b/exeinfo/MZ.cs deleted file mode 100644 index b612a6f..0000000 --- a/exeinfo/MZ.cs +++ /dev/null @@ -1,64 +0,0 @@ -using System; -using System.Runtime.InteropServices; - -namespace exeinfo -{ - public static class MZ - { - public const ushort Signature = 0x5A4D; - - [StructLayout(LayoutKind.Sequential, Pack = 1)] - public struct Header - { - public ushort signature; - public ushort bytes_in_last_block; - public ushort blocks_in_file; - public ushort num_relocs; - public ushort header_paragraphs; - public ushort min_extra_paragraphs; - public ushort max_extra_paragraphs; - public ushort ss; - public ushort sp; - public ushort checksum; - public ushort ip; - public ushort cs; - public ushort reloc_table_offset; - public ushort overlay_number; - [MarshalAs(UnmanagedType.ByValArray, SizeConst = 4)] - public ushort[] reserved; - public ushort oem_id; - public ushort oem_info; - [MarshalAs(UnmanagedType.ByValArray, SizeConst = 10)] - public ushort[] reserved2; - public uint new_offset; - } - - [StructLayout(LayoutKind.Sequential, Pack = 1)] - struct RelocationTableEntry - { - public ushort offset; - public ushort segment; - } - - public static void PrintInfo(Header header) - { - Console.WriteLine("DOS MZ executable:"); - Console.WriteLine("\tBlocks in file: {0}", header.blocks_in_file); - Console.WriteLine("\t{0} bytes used in last block", header.bytes_in_last_block == 0 ? 512 : header.bytes_in_last_block); - Console.WriteLine("\t{0} relocations present after the header", header.num_relocs); - Console.WriteLine("\t{0} paragraphs in header", header.header_paragraphs); - Console.WriteLine("\t{0} paragraphs of additional memory required", header.min_extra_paragraphs); - Console.WriteLine("\t{0} paragraphs of additional memory requested", header.max_extra_paragraphs); - Console.WriteLine("\tSegment address for SS: {0:X4}h", header.ss); - Console.WriteLine("\tInitial value of SP: {0:X4}h", header.sp); - Console.WriteLine("\tInitial value of IP: {0:X4}h", header.ip); - Console.WriteLine("\tInitial value of CS: {0:X4}h", header.cs); - Console.WriteLine("\tOffset to relocation table: {0}", header.reloc_table_offset); - Console.WriteLine("\tFile contains {0} overlays", header.overlay_number); - Console.WriteLine("\tFile checksum: 0x{0:X4}", header.checksum); - Console.WriteLine("\tOEM ID: {0}", header.oem_id); - Console.WriteLine("\tOEM information: 0x{0:X4}", header.oem_info); - Console.WriteLine("\tOffset to new header: {0}", header.new_offset); - } - } -} diff --git a/exeinfo/MZ/Consts.cs b/exeinfo/MZ/Consts.cs new file mode 100644 index 0000000..efb1e4e --- /dev/null +++ b/exeinfo/MZ/Consts.cs @@ -0,0 +1,8 @@ +using System; +namespace exeinfo.MZ +{ + public class Consts + { + public const ushort Signature = 0x5A4D; + } +} diff --git a/exeinfo/MZ/Info.cs b/exeinfo/MZ/Info.cs new file mode 100644 index 0000000..89ae317 --- /dev/null +++ b/exeinfo/MZ/Info.cs @@ -0,0 +1,27 @@ +using System; +namespace exeinfo.MZ +{ + public class Info + { + public static void PrintInfo(Header header) + { + Console.WriteLine("DOS MZ executable:"); + Console.WriteLine("\tBlocks in file: {0}", header.blocks_in_file); + Console.WriteLine("\t{0} bytes used in last block", header.bytes_in_last_block == 0 ? 512 : header.bytes_in_last_block); + Console.WriteLine("\t{0} relocations present after the header", header.num_relocs); + Console.WriteLine("\t{0} paragraphs in header", header.header_paragraphs); + Console.WriteLine("\t{0} paragraphs of additional memory required", header.min_extra_paragraphs); + Console.WriteLine("\t{0} paragraphs of additional memory requested", header.max_extra_paragraphs); + Console.WriteLine("\tSegment address for SS: {0:X4}h", header.ss); + Console.WriteLine("\tInitial value of SP: {0:X4}h", header.sp); + Console.WriteLine("\tInitial value of IP: {0:X4}h", header.ip); + Console.WriteLine("\tInitial value of CS: {0:X4}h", header.cs); + Console.WriteLine("\tOffset to relocation table: {0}", header.reloc_table_offset); + Console.WriteLine("\tFile contains {0} overlays", header.overlay_number); + Console.WriteLine("\tFile checksum: 0x{0:X4}", header.checksum); + Console.WriteLine("\tOEM ID: {0}", header.oem_id); + Console.WriteLine("\tOEM information: 0x{0:X4}", header.oem_info); + Console.WriteLine("\tOffset to new header: {0}", header.new_offset); + } + } +} diff --git a/exeinfo/MZ/Structs.cs b/exeinfo/MZ/Structs.cs new file mode 100644 index 0000000..19085dd --- /dev/null +++ b/exeinfo/MZ/Structs.cs @@ -0,0 +1,37 @@ +using System.Runtime.InteropServices; + +namespace exeinfo.MZ +{ + [StructLayout(LayoutKind.Sequential, Pack = 1)] + public struct Header + { + public ushort signature; + public ushort bytes_in_last_block; + public ushort blocks_in_file; + public ushort num_relocs; + public ushort header_paragraphs; + public ushort min_extra_paragraphs; + public ushort max_extra_paragraphs; + public ushort ss; + public ushort sp; + public ushort checksum; + public ushort ip; + public ushort cs; + public ushort reloc_table_offset; + public ushort overlay_number; + [MarshalAs(UnmanagedType.ByValArray, SizeConst = 4)] + public ushort[] reserved; + public ushort oem_id; + public ushort oem_info; + [MarshalAs(UnmanagedType.ByValArray, SizeConst = 10)] + public ushort[] reserved2; + public uint new_offset; + } + + [StructLayout(LayoutKind.Sequential, Pack = 1)] + struct RelocationTableEntry + { + public ushort offset; + public ushort segment; + } +} diff --git a/exeinfo/Program.cs b/exeinfo/Program.cs index 93a90ef..9dd600f 100644 --- a/exeinfo/Program.cs +++ b/exeinfo/Program.cs @@ -33,10 +33,10 @@ namespace exeinfo mzHdr = (MZ.Header)Marshal.PtrToStructure(hdrPtr, typeof(MZ.Header)); Marshal.FreeHGlobal(hdrPtr); - if(mzHdr.signature == MZ.Signature) + if(mzHdr.signature == MZ.Consts.Signature) { recognized = true; - MZ.PrintInfo(mzHdr); + MZ.Info.PrintInfo(mzHdr); if (mzHdr.new_offset < exeFs.Length) { diff --git a/exeinfo/exeinfo.csproj b/exeinfo/exeinfo.csproj index f8f6e71..4d53ed9 100644 --- a/exeinfo/exeinfo.csproj +++ b/exeinfo/exeinfo.csproj @@ -34,15 +34,18 @@ - + + + + \ No newline at end of file