Make MZ non-static.

This commit is contained in:
2017-10-16 14:58:13 +01:00
parent 71b8f55a69
commit 8ec5f3a07c
5 changed files with 108 additions and 17 deletions

View File

@@ -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)

View File

@@ -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);
}
}
}

91
libexeinfo/MZ/MZ.cs Normal file
View File

@@ -0,0 +1,91 @@
//
// MZ.cs
//
// Author:
// Natalia Portillo <claunia@claunia.com>
//
// 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;
}
}
}

View File

@@ -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;

View File

@@ -49,6 +49,7 @@
<Compile Include="NE\Info.cs" />
<Compile Include="NE\Structs.cs" />
<Compile Include="NE\Version.cs" />
<Compile Include="MZ\MZ.cs" />
</ItemGroup>
<ItemGroup>
<None Include="packages.config" />