commit e84cf3cb41ffaa4b6cbade09c2159cbf034370b3 Author: Natalia Portillo Date: Tue Oct 3 13:26:49 2017 +0100 Added support for DOS MZ executables. diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..4e82d27 --- /dev/null +++ b/.gitignore @@ -0,0 +1,40 @@ +# Autosave files +*~ + +# build +[Oo]bj/ +[Bb]in/ +packages/ +TestResults/ + +# globs +Makefile.in +*.DS_Store +*.sln.cache +*.suo +*.cache +*.pidb +*.userprefs +*.usertasks +config.log +config.make +config.status +aclocal.m4 +install-sh +autom4te.cache/ +*.user +*.tar.gz +tarballs/ +test-results/ +Thumbs.db + +# Mac bundle stuff +*.dmg +*.app + +# resharper +*_Resharper.* +*.Resharper + +# dotCover +*.dotCover diff --git a/exeinfo.sln b/exeinfo.sln new file mode 100644 index 0000000..9110853 --- /dev/null +++ b/exeinfo.sln @@ -0,0 +1,17 @@ + +Microsoft Visual Studio Solution File, Format Version 12.00 +# Visual Studio 2012 +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "exeinfo", "exeinfo\exeinfo.csproj", "{504F0A15-25DC-42B1-81FE-BA22A8EF24B5}" +EndProject +Global + GlobalSection(SolutionConfigurationPlatforms) = preSolution + Debug|x86 = Debug|x86 + Release|x86 = Release|x86 + EndGlobalSection + GlobalSection(ProjectConfigurationPlatforms) = postSolution + {504F0A15-25DC-42B1-81FE-BA22A8EF24B5}.Debug|x86.ActiveCfg = Debug|x86 + {504F0A15-25DC-42B1-81FE-BA22A8EF24B5}.Debug|x86.Build.0 = Debug|x86 + {504F0A15-25DC-42B1-81FE-BA22A8EF24B5}.Release|x86.ActiveCfg = Release|x86 + {504F0A15-25DC-42B1-81FE-BA22A8EF24B5}.Release|x86.Build.0 = Release|x86 + EndGlobalSection +EndGlobal diff --git a/exeinfo/MZ.cs b/exeinfo/MZ.cs new file mode 100644 index 0000000..b612a6f --- /dev/null +++ b/exeinfo/MZ.cs @@ -0,0 +1,64 @@ +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/Program.cs b/exeinfo/Program.cs new file mode 100644 index 0000000..63ff91d --- /dev/null +++ b/exeinfo/Program.cs @@ -0,0 +1,42 @@ +using System; +using System.IO; +using System.Runtime.InteropServices; + +namespace exeinfo +{ + class MainClass + { + static MZ.Header mzHdr; + + public static void Main(string[] args) + { + if(args.Length != 1) + { + Console.WriteLine("exeinfo version 0.1 © 2017 Natalia Portillo"); + Console.WriteLine("Usage: exeinfo file.exe"); + return; + } + + FileStream exeFs = File.Open(args[0], FileMode.Open, FileAccess.Read); + + bool recognized = false; + + byte[] buffer = new byte[Marshal.SizeOf(typeof(MZ.Header))]; + + exeFs.Read(buffer, 0, buffer.Length); + IntPtr hdrPtr = Marshal.AllocHGlobal(buffer.Length); + Marshal.Copy(buffer, 0, hdrPtr, buffer.Length); + mzHdr = (MZ.Header)Marshal.PtrToStructure(hdrPtr, typeof(MZ.Header)); + Marshal.FreeHGlobal(hdrPtr); + + if(mzHdr.signature == MZ.Signature) + { + recognized = true; + MZ.PrintInfo(mzHdr); + } + + if (!recognized) + Console.WriteLine("Executalbe format not recognized"); + } + } +} diff --git a/exeinfo/Properties/AssemblyInfo.cs b/exeinfo/Properties/AssemblyInfo.cs new file mode 100644 index 0000000..73fad65 --- /dev/null +++ b/exeinfo/Properties/AssemblyInfo.cs @@ -0,0 +1,26 @@ +using System.Reflection; +using System.Runtime.CompilerServices; + +// Information about this assembly is defined by the following attributes. +// Change them to the values specific to your project. + +[assembly: AssemblyTitle("exeinfo")] +[assembly: AssemblyDescription("")] +[assembly: AssemblyConfiguration("")] +[assembly: AssemblyCompany("Claunia.com")] +[assembly: AssemblyProduct("")] +[assembly: AssemblyCopyright("Copyright © Claunia.com")] +[assembly: AssemblyTrademark("")] +[assembly: AssemblyCulture("")] + +// The assembly version has the format "{Major}.{Minor}.{Build}.{Revision}". +// The form "{Major}.{Minor}.*" will automatically update the build and revision, +// and "{Major}.{Minor}.{Build}.*" will update just the revision. + +[assembly: AssemblyVersion("1.0.*")] + +// The following attributes are used to specify the signing key for the assembly, +// if desired. See the Mono documentation for more information about signing. + +//[assembly: AssemblyDelaySign(false)] +//[assembly: AssemblyKeyFile("")] diff --git a/exeinfo/exeinfo.csproj b/exeinfo/exeinfo.csproj new file mode 100644 index 0000000..e8e8693 --- /dev/null +++ b/exeinfo/exeinfo.csproj @@ -0,0 +1,40 @@ + + + + Debug + x86 + {504F0A15-25DC-42B1-81FE-BA22A8EF24B5} + Exe + exeinfo + exeinfo + v4.6.1 + + + true + full + false + bin\Debug + DEBUG; + prompt + 4 + true + x86 + + + true + bin\Release + prompt + 4 + true + x86 + + + + + + + + + + + \ No newline at end of file