From 8ec5f3a07cf4b49e70f63a4dbd2285e168959b56 Mon Sep 17 00:00:00 2001 From: Natalia Portillo Date: Mon, 16 Oct 2017 14:58:13 +0100 Subject: [PATCH] Make MZ non-static. --- exeinfo/Program.cs | 24 ++++------ libexeinfo/MZ/Info.cs | 7 ++- libexeinfo/MZ/MZ.cs | 91 ++++++++++++++++++++++++++++++++++++ libexeinfo/MZ/Structs.cs | 2 +- libexeinfo/libexeinfo.csproj | 1 + 5 files changed, 108 insertions(+), 17 deletions(-) create mode 100644 libexeinfo/MZ/MZ.cs diff --git a/exeinfo/Program.cs b/exeinfo/Program.cs index 8301217..a33a306 100644 --- a/exeinfo/Program.cs +++ b/exeinfo/Program.cs @@ -30,12 +30,12 @@ using System.Runtime.InteropServices; using System.Collections.Generic; using System.Globalization; using System.Text; +using libexeinfo; namespace exeinfo { class MainClass { - static libexeinfo.MZ.Header mzHdr; static libexeinfo.NE.Header neHdr; public static void Main(string[] args) @@ -51,26 +51,20 @@ namespace exeinfo bool recognized = false; - byte[] buffer = new byte[Marshal.SizeOf(typeof(libexeinfo.MZ.Header))]; + MZ mzExe = new MZ(exeFs); - exeFs.Read(buffer, 0, buffer.Length); - IntPtr hdrPtr = Marshal.AllocHGlobal(buffer.Length); - Marshal.Copy(buffer, 0, hdrPtr, buffer.Length); - mzHdr = (libexeinfo.MZ.Header)Marshal.PtrToStructure(hdrPtr, typeof(libexeinfo.MZ.Header)); - Marshal.FreeHGlobal(hdrPtr); - - if(mzHdr.signature == libexeinfo.MZ.Signature) + if(mzExe.IsMZ) { recognized = true; - Console.Write(libexeinfo.MZ.GetInfo(mzHdr)); + Console.Write(mzExe.GetInfo()); - if (mzHdr.new_offset < exeFs.Length) + if (mzExe.Header.new_offset < exeFs.Length) { - exeFs.Seek(mzHdr.new_offset, SeekOrigin.Begin); + exeFs.Seek(mzExe.Header.new_offset, SeekOrigin.Begin); - buffer = new byte[Marshal.SizeOf(typeof(libexeinfo.NE.Header))]; + byte[] buffer = new byte[Marshal.SizeOf(typeof(libexeinfo.NE.Header))]; exeFs.Read(buffer, 0, buffer.Length); - hdrPtr = Marshal.AllocHGlobal(buffer.Length); + IntPtr hdrPtr = Marshal.AllocHGlobal(buffer.Length); Marshal.Copy(buffer, 0, hdrPtr, buffer.Length); neHdr = (libexeinfo.NE.Header)Marshal.PtrToStructure(hdrPtr, typeof(libexeinfo.NE.Header)); Marshal.FreeHGlobal(hdrPtr); @@ -78,7 +72,7 @@ namespace exeinfo if (neHdr.signature == libexeinfo.NE.Signature) { Console.Write(libexeinfo.NE.GetInfo(neHdr)); - libexeinfo.NE.ResourceTable resources = libexeinfo.NE.GetResources(exeFs, mzHdr.new_offset, neHdr.resource_table_offset); + libexeinfo.NE.ResourceTable resources = libexeinfo.NE.GetResources(exeFs, mzExe.Header.new_offset, neHdr.resource_table_offset); foreach(libexeinfo.NE.ResourceType type in resources.types) { if((type.id & 0x7FFF) == (int)libexeinfo.NE.ResourceTypes.RT_VERSION) diff --git a/libexeinfo/MZ/Info.cs b/libexeinfo/MZ/Info.cs index 54bc7ed..357b39c 100644 --- a/libexeinfo/MZ/Info.cs +++ b/libexeinfo/MZ/Info.cs @@ -30,7 +30,7 @@ namespace libexeinfo { public partial class MZ { - public static string GetInfo(Header header) + public static string GetInfo(MZHeader header) { StringBuilder sb = new StringBuilder(); sb.AppendLine("DOS MZ executable:"); @@ -52,5 +52,10 @@ namespace libexeinfo sb.AppendFormat("\tOffset to new header: {0}", header.new_offset).AppendLine(); return sb.ToString(); } + + public string GetInfo() + { + return GetInfo(Header); + } } } diff --git a/libexeinfo/MZ/MZ.cs b/libexeinfo/MZ/MZ.cs new file mode 100644 index 0000000..07c98cc --- /dev/null +++ b/libexeinfo/MZ/MZ.cs @@ -0,0 +1,91 @@ +// +// MZ.cs +// +// Author: +// Natalia Portillo +// +// Copyright (c) 2017 Copyright © Claunia.com +// +// Permission is hereby granted, free of charge, to any person obtaining a copy +// of this software and associated documentation files (the "Software"), to deal +// in the Software without restriction, including without limitation the rights +// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +// copies of the Software, and to permit persons to whom the Software is +// furnished to do so, subject to the following conditions: +// +// The above copyright notice and this permission notice shall be included in +// all copies or substantial portions of the Software. +// +// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN +// THE SOFTWARE. +using System; +using System.IO; +using System.Runtime.InteropServices; + +namespace libexeinfo +{ + public partial class MZ + { + public readonly FileStream BaseStream; + public readonly MZHeader Header; + public readonly bool IsMZ; + + public MZ(string path) + { + byte[] buffer = new byte[Marshal.SizeOf(typeof(MZHeader))]; + + BaseStream = File.Open(path, FileMode.Open, FileAccess.Read); + BaseStream.Position = 0; + BaseStream.Read(buffer, 0, buffer.Length); + IntPtr hdrPtr = Marshal.AllocHGlobal(buffer.Length); + Marshal.Copy(buffer, 0, hdrPtr, buffer.Length); + Header = (MZHeader)Marshal.PtrToStructure(hdrPtr, typeof(MZHeader)); + Marshal.FreeHGlobal(hdrPtr); + IsMZ = Header.signature == Signature; + } + + public MZ(FileStream stream) + { + byte[] buffer = new byte[Marshal.SizeOf(typeof(MZHeader))]; + + BaseStream = stream; + BaseStream.Position = 0; + BaseStream.Read(buffer, 0, buffer.Length); + IntPtr hdrPtr = Marshal.AllocHGlobal(buffer.Length); + Marshal.Copy(buffer, 0, hdrPtr, buffer.Length); + Header = (MZHeader)Marshal.PtrToStructure(hdrPtr, typeof(MZHeader)); + Marshal.FreeHGlobal(hdrPtr); + IsMZ = Header.signature == Signature; + } + + public bool Identify() + { + return IsMZ; + } + + public static bool Identify(string path) + { + FileStream exeFs = File.Open(path, FileMode.Open, FileAccess.Read); + return Identify(exeFs); + } + + public static bool Identify(FileStream stream) + { + byte[] buffer = new byte[Marshal.SizeOf(typeof(MZHeader))]; + + stream.Position = 0; + stream.Read(buffer, 0, buffer.Length); + IntPtr hdrPtr = Marshal.AllocHGlobal(buffer.Length); + Marshal.Copy(buffer, 0, hdrPtr, buffer.Length); + MZHeader mzHdr = (MZHeader)Marshal.PtrToStructure(hdrPtr, typeof(MZHeader)); + Marshal.FreeHGlobal(hdrPtr); + + return mzHdr.signature == Signature; + } + } +} diff --git a/libexeinfo/MZ/Structs.cs b/libexeinfo/MZ/Structs.cs index f204a5a..32d712b 100644 --- a/libexeinfo/MZ/Structs.cs +++ b/libexeinfo/MZ/Structs.cs @@ -31,7 +31,7 @@ namespace libexeinfo public partial class MZ { [StructLayout(LayoutKind.Sequential, Pack = 1)] - public struct Header + public struct MZHeader { public ushort signature; public ushort bytes_in_last_block; diff --git a/libexeinfo/libexeinfo.csproj b/libexeinfo/libexeinfo.csproj index b2360bf..e7507bd 100644 --- a/libexeinfo/libexeinfo.csproj +++ b/libexeinfo/libexeinfo.csproj @@ -49,6 +49,7 @@ +