Refactor MZ.

This commit is contained in:
2017-10-03 20:38:08 +01:00
parent 0dafcd268e
commit 01523da96c
6 changed files with 78 additions and 67 deletions

View File

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

8
exeinfo/MZ/Consts.cs Normal file
View File

@@ -0,0 +1,8 @@
using System;
namespace exeinfo.MZ
{
public class Consts
{
public const ushort Signature = 0x5A4D;
}
}

27
exeinfo/MZ/Info.cs Normal file
View File

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

37
exeinfo/MZ/Structs.cs Normal file
View File

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

View File

@@ -33,10 +33,10 @@ namespace exeinfo
mzHdr = (MZ.Header)Marshal.PtrToStructure(hdrPtr, typeof(MZ.Header)); mzHdr = (MZ.Header)Marshal.PtrToStructure(hdrPtr, typeof(MZ.Header));
Marshal.FreeHGlobal(hdrPtr); Marshal.FreeHGlobal(hdrPtr);
if(mzHdr.signature == MZ.Signature) if(mzHdr.signature == MZ.Consts.Signature)
{ {
recognized = true; recognized = true;
MZ.PrintInfo(mzHdr); MZ.Info.PrintInfo(mzHdr);
if (mzHdr.new_offset < exeFs.Length) if (mzHdr.new_offset < exeFs.Length)
{ {

View File

@@ -34,15 +34,18 @@
<ItemGroup> <ItemGroup>
<Compile Include="Program.cs" /> <Compile Include="Program.cs" />
<Compile Include="Properties\AssemblyInfo.cs" /> <Compile Include="Properties\AssemblyInfo.cs" />
<Compile Include="MZ.cs" />
<Compile Include="NE\Version.cs" /> <Compile Include="NE\Version.cs" />
<Compile Include="NE\Structs.cs" /> <Compile Include="NE\Structs.cs" />
<Compile Include="NE\Enums.cs" /> <Compile Include="NE\Enums.cs" />
<Compile Include="NE\Consts.cs" /> <Compile Include="NE\Consts.cs" />
<Compile Include="NE\Info.cs" /> <Compile Include="NE\Info.cs" />
<Compile Include="MZ\Consts.cs" />
<Compile Include="MZ\Info.cs" />
<Compile Include="MZ\Structs.cs" />
</ItemGroup> </ItemGroup>
<ItemGroup> <ItemGroup>
<Folder Include="NE\" /> <Folder Include="NE\" />
<Folder Include="MZ\" />
</ItemGroup> </ItemGroup>
<Import Project="$(MSBuildBinPath)\Microsoft.CSharp.targets" /> <Import Project="$(MSBuildBinPath)\Microsoft.CSharp.targets" />
</Project> </Project>