Change how information is output from library.

This commit is contained in:
2017-10-16 14:32:16 +01:00
parent 1d9835f6ef
commit 71b8f55a69
3 changed files with 92 additions and 87 deletions

View File

@@ -62,7 +62,7 @@ namespace exeinfo
if(mzHdr.signature == libexeinfo.MZ.Signature) if(mzHdr.signature == libexeinfo.MZ.Signature)
{ {
recognized = true; recognized = true;
libexeinfo.MZ.PrintInfo(mzHdr); Console.Write(libexeinfo.MZ.GetInfo(mzHdr));
if (mzHdr.new_offset < exeFs.Length) if (mzHdr.new_offset < exeFs.Length)
{ {
@@ -77,7 +77,7 @@ namespace exeinfo
if (neHdr.signature == libexeinfo.NE.Signature) if (neHdr.signature == libexeinfo.NE.Signature)
{ {
libexeinfo.NE.PrintInfo(neHdr); 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, mzHdr.new_offset, neHdr.resource_table_offset);
foreach(libexeinfo.NE.ResourceType type in resources.types) foreach(libexeinfo.NE.ResourceType type in resources.types)
{ {

View File

@@ -25,29 +25,32 @@
// THE SOFTWARE. // THE SOFTWARE.
using System; using System;
using System.Text;
namespace libexeinfo namespace libexeinfo
{ {
public partial class MZ public partial class MZ
{ {
public static void PrintInfo(Header header) public static string GetInfo(Header header)
{ {
Console.WriteLine("DOS MZ executable:"); StringBuilder sb = new StringBuilder();
Console.WriteLine("\tBlocks in file: {0}", header.blocks_in_file); sb.AppendLine("DOS MZ executable:");
Console.WriteLine("\t{0} bytes used in last block", header.bytes_in_last_block == 0 ? 512 : header.bytes_in_last_block); sb.AppendFormat("\tBlocks in file: {0}", header.blocks_in_file).AppendLine();
Console.WriteLine("\t{0} relocations present after the header", header.num_relocs); sb.AppendFormat("\t{0} bytes used in last block", header.bytes_in_last_block == 0 ? 512 : header.bytes_in_last_block).AppendLine();
Console.WriteLine("\t{0} paragraphs in header", header.header_paragraphs); sb.AppendFormat("\t{0} relocations present after the header", header.num_relocs).AppendLine();
Console.WriteLine("\t{0} paragraphs of additional memory required", header.min_extra_paragraphs); sb.AppendFormat("\t{0} paragraphs in header", header.header_paragraphs).AppendLine();
Console.WriteLine("\t{0} paragraphs of additional memory requested", header.max_extra_paragraphs); sb.AppendFormat("\t{0} paragraphs of additional memory required", header.min_extra_paragraphs).AppendLine();
Console.WriteLine("\tSegment address for SS: {0:X4}h", header.ss); sb.AppendFormat("\t{0} paragraphs of additional memory requested", header.max_extra_paragraphs).AppendLine();
Console.WriteLine("\tInitial value of SP: {0:X4}h", header.sp); sb.AppendFormat("\tSegment address for SS: {0:X4}h", header.ss).AppendLine();
Console.WriteLine("\tInitial value of IP: {0:X4}h", header.ip); sb.AppendFormat("\tInitial value of SP: {0:X4}h", header.sp).AppendLine();
Console.WriteLine("\tInitial value of CS: {0:X4}h", header.cs); sb.AppendFormat("\tInitial value of IP: {0:X4}h", header.ip).AppendLine();
Console.WriteLine("\tOffset to relocation table: {0}", header.reloc_table_offset); sb.AppendFormat("\tInitial value of CS: {0:X4}h", header.cs).AppendLine();
Console.WriteLine("\tFile contains {0} overlays", header.overlay_number); sb.AppendFormat("\tOffset to relocation table: {0}", header.reloc_table_offset).AppendLine();
Console.WriteLine("\tFile checksum: 0x{0:X4}", header.checksum); sb.AppendFormat("\tFile contains {0} overlays", header.overlay_number).AppendLine();
Console.WriteLine("\tOEM ID: {0}", header.oem_id); sb.AppendFormat("\tFile checksum: 0x{0:X4}", header.checksum).AppendLine();
Console.WriteLine("\tOEM information: 0x{0:X4}", header.oem_info); sb.AppendFormat("\tOEM ID: {0}", header.oem_id).AppendLine();
Console.WriteLine("\tOffset to new header: {0}", header.new_offset); sb.AppendFormat("\tOEM information: 0x{0:X4}", header.oem_info).AppendLine();
sb.AppendFormat("\tOffset to new header: {0}", header.new_offset).AppendLine();
return sb.ToString();
} }
} }
} }

View File

@@ -33,127 +33,129 @@ namespace libexeinfo
{ {
public partial class NE public partial class NE
{ {
public static void PrintInfo(Header header) public static string GetInfo(Header header)
{ {
Console.WriteLine("New Executable (NE):"); StringBuilder sb = new StringBuilder();
Console.WriteLine("\tFile's CRC: 0x{0:X8}", header.crc); sb.AppendLine("New Executable (NE):");
Console.WriteLine("\tLinker version: {0}.{1}", header.linker_major, header.linker_minor); sb.AppendFormat("\tFile's CRC: 0x{0:X8}", header.crc).AppendLine();
sb.AppendFormat("\tLinker version: {0}.{1}", header.linker_major, header.linker_minor).AppendLine();
if (header.program_flags.HasFlag(ProgramFlags.SingleDGroup) && !header.program_flags.HasFlag(ProgramFlags.MultipleDGroup)) if (header.program_flags.HasFlag(ProgramFlags.SingleDGroup) && !header.program_flags.HasFlag(ProgramFlags.MultipleDGroup))
Console.WriteLine("\tApplication uses a single shared DGroup"); sb.AppendLine("\tApplication uses a single shared DGroup");
else if (!header.program_flags.HasFlag(ProgramFlags.SingleDGroup) && header.program_flags.HasFlag(ProgramFlags.MultipleDGroup)) else if (!header.program_flags.HasFlag(ProgramFlags.SingleDGroup) && header.program_flags.HasFlag(ProgramFlags.MultipleDGroup))
Console.WriteLine("\tApplication uses a multiple DGroup"); sb.AppendLine("\tApplication uses a multiple DGroup");
else if (header.program_flags.HasFlag(ProgramFlags.SingleDGroup) && header.program_flags.HasFlag(ProgramFlags.MultipleDGroup)) else if (header.program_flags.HasFlag(ProgramFlags.SingleDGroup) && header.program_flags.HasFlag(ProgramFlags.MultipleDGroup))
Console.WriteLine("\tApplication indicates an incorrect DGroup value"); sb.AppendLine("\tApplication indicates an incorrect DGroup value");
else if (!header.program_flags.HasFlag(ProgramFlags.SingleDGroup) && !header.program_flags.HasFlag(ProgramFlags.MultipleDGroup)) else if (!header.program_flags.HasFlag(ProgramFlags.SingleDGroup) && !header.program_flags.HasFlag(ProgramFlags.MultipleDGroup))
Console.WriteLine("\tApplication does not use DGroup"); sb.AppendLine("\tApplication does not use DGroup");
if (header.program_flags.HasFlag(ProgramFlags.GlobalInit)) if (header.program_flags.HasFlag(ProgramFlags.GlobalInit))
Console.WriteLine("\tApplication uses global initialization"); sb.AppendLine("\tApplication uses global initialization");
if (header.program_flags.HasFlag(ProgramFlags.ProtectedMode)) if (header.program_flags.HasFlag(ProgramFlags.ProtectedMode))
Console.WriteLine("\tApplication uses protected mode"); sb.AppendLine("\tApplication uses protected mode");
if (header.program_flags.HasFlag(ProgramFlags.i86)) if (header.program_flags.HasFlag(ProgramFlags.i86))
Console.WriteLine("\tApplication uses 8086 instructions"); sb.AppendLine("\tApplication uses 8086 instructions");
if (header.program_flags.HasFlag(ProgramFlags.i286)) if (header.program_flags.HasFlag(ProgramFlags.i286))
Console.WriteLine("\tApplication uses 80286 instructions"); sb.AppendLine("\tApplication uses 80286 instructions");
if (header.program_flags.HasFlag(ProgramFlags.i386)) if (header.program_flags.HasFlag(ProgramFlags.i386))
Console.WriteLine("\tApplication uses 80386 instructions"); sb.AppendLine("\tApplication uses 80386 instructions");
if (header.program_flags.HasFlag(ProgramFlags.i87)) if (header.program_flags.HasFlag(ProgramFlags.i87))
Console.WriteLine("\tApplication uses floating point instructions"); sb.AppendLine("\tApplication uses floating point instructions");
if (header.target_os == TargetOS.OS2) if (header.target_os == TargetOS.OS2)
{ {
Console.WriteLine("\tOS/2 application"); sb.AppendLine("\tOS/2 application");
Console.WriteLine("\tApplication requires OS/2 {0}.{1} to run", header.os_major, header.os_minor); sb.AppendFormat("\tApplication requires OS/2 {0}.{1} to run", header.os_major, header.os_minor).AppendLine();
if (header.application_flags.HasFlag(ApplicationFlags.FullScreen) && !header.application_flags.HasFlag(ApplicationFlags.GUICompatible)) if (header.application_flags.HasFlag(ApplicationFlags.FullScreen) && !header.application_flags.HasFlag(ApplicationFlags.GUICompatible))
Console.WriteLine("\tApplication is full screen, unaware of Presentation Manager"); sb.AppendLine("\tApplication is full screen, unaware of Presentation Manager");
else if (!header.application_flags.HasFlag(ApplicationFlags.FullScreen) && header.application_flags.HasFlag(ApplicationFlags.GUICompatible)) else if (!header.application_flags.HasFlag(ApplicationFlags.FullScreen) && header.application_flags.HasFlag(ApplicationFlags.GUICompatible))
Console.WriteLine("\tApplication is aware of Presentation Manager, but doesn't use it"); sb.AppendLine("\tApplication is aware of Presentation Manager, but doesn't use it");
else if (header.application_flags.HasFlag(ApplicationFlags.FullScreen) && header.application_flags.HasFlag(ApplicationFlags.GUICompatible)) else if (header.application_flags.HasFlag(ApplicationFlags.FullScreen) && header.application_flags.HasFlag(ApplicationFlags.GUICompatible))
Console.WriteLine("\tApplication uses Presentation Manager"); sb.AppendLine("\tApplication uses Presentation Manager");
if (header.os2_flags.HasFlag(OS2Flags.LongFilename)) if (header.os2_flags.HasFlag(OS2Flags.LongFilename))
Console.WriteLine("\tApplication supports long filenames"); sb.AppendLine("\tApplication supports long filenames");
if (header.os2_flags.HasFlag(OS2Flags.ProtectedMode2)) if (header.os2_flags.HasFlag(OS2Flags.ProtectedMode2))
Console.WriteLine("\tApplication uses OS/2 2.x protected mode"); sb.AppendLine("\tApplication uses OS/2 2.x protected mode");
if (header.os2_flags.HasFlag(OS2Flags.ProportionalFonts)) if (header.os2_flags.HasFlag(OS2Flags.ProportionalFonts))
Console.WriteLine("\tApplication uses OS/2 2.x proportional fonts"); sb.AppendLine("\tApplication uses OS/2 2.x proportional fonts");
if (header.os2_flags.HasFlag(OS2Flags.GangloadArea)) if (header.os2_flags.HasFlag(OS2Flags.GangloadArea))
Console.WriteLine("\tGangload area starts at {0} an runs for {1} bytes", header.return_thunks_offset, header.segment_reference_thunks); sb.AppendFormat("\tGangload area starts at {0} an runs for {1} bytes", header.return_thunks_offset, header.segment_reference_thunks).AppendLine();
else else
{ {
Console.WriteLine("\tReturn thunks are at: {0}", header.return_thunks_offset); sb.AppendFormat("\tReturn thunks are at: {0}", header.return_thunks_offset).AppendLine();
Console.WriteLine("\tSegment reference thunks are at: {0}", header.segment_reference_thunks); sb.AppendFormat("\tSegment reference thunks are at: {0}", header.segment_reference_thunks).AppendLine();
} }
} }
else if (header.target_os == TargetOS.Windows || header.target_os == TargetOS.Win32) else if (header.target_os == TargetOS.Windows || header.target_os == TargetOS.Win32)
{ {
if (header.target_os == TargetOS.Windows) if (header.target_os == TargetOS.Windows)
Console.WriteLine("\t16-bit Windows application"); sb.AppendLine("\t16-bit Windows application");
else if (header.target_os == TargetOS.Win32) else if (header.target_os == TargetOS.Win32)
Console.WriteLine("\t32-bit Windows application"); sb.AppendLine("\t32-bit Windows application");
Console.WriteLine("\tApplication requires Windows {0}.{1} to run", header.os_major, header.os_minor); sb.AppendFormat("\tApplication requires Windows {0}.{1} to run", header.os_major, header.os_minor).AppendLine();
if (header.application_flags.HasFlag(ApplicationFlags.FullScreen) && !header.application_flags.HasFlag(ApplicationFlags.GUICompatible)) if (header.application_flags.HasFlag(ApplicationFlags.FullScreen) && !header.application_flags.HasFlag(ApplicationFlags.GUICompatible))
Console.WriteLine("\tApplication is full screen, unaware of Windows"); sb.AppendLine("\tApplication is full screen, unaware of Windows");
else if (!header.application_flags.HasFlag(ApplicationFlags.FullScreen) && header.application_flags.HasFlag(ApplicationFlags.GUICompatible)) else if (!header.application_flags.HasFlag(ApplicationFlags.FullScreen) && header.application_flags.HasFlag(ApplicationFlags.GUICompatible))
Console.WriteLine("\tApplication is aware of Windows, but doesn't use it"); sb.AppendLine("\tApplication is aware of Windows, but doesn't use it");
else if (header.application_flags.HasFlag(ApplicationFlags.FullScreen) && header.application_flags.HasFlag(ApplicationFlags.GUICompatible)) else if (header.application_flags.HasFlag(ApplicationFlags.FullScreen) && header.application_flags.HasFlag(ApplicationFlags.GUICompatible))
Console.WriteLine("\tApplication uses Windows"); sb.AppendLine("\tApplication uses Windows");
Console.WriteLine("\tReturn thunks are at: {0}", header.return_thunks_offset); sb.AppendFormat("\tReturn thunks are at: {0}", header.return_thunks_offset).AppendLine();
Console.WriteLine("\tSegment reference thunks are at: {0}", header.segment_reference_thunks); sb.AppendFormat("\tSegment reference thunks are at: {0}", header.segment_reference_thunks).AppendLine();
} }
else if (header.target_os == TargetOS.DOS) else if (header.target_os == TargetOS.DOS)
{ {
Console.WriteLine("\tDOS application"); sb.AppendLine("\tDOS application");
Console.WriteLine("\tApplication requires DOS {0}.{1} to run", header.os_major, header.os_minor); sb.AppendFormat("\tApplication requires DOS {0}.{1} to run", header.os_major, header.os_minor).AppendLine();
if (header.application_flags.HasFlag(ApplicationFlags.FullScreen) && !header.application_flags.HasFlag(ApplicationFlags.GUICompatible)) if (header.application_flags.HasFlag(ApplicationFlags.FullScreen) && !header.application_flags.HasFlag(ApplicationFlags.GUICompatible))
Console.WriteLine("\tApplication is full screen, unaware of Windows"); sb.AppendLine("\tApplication is full screen, unaware of Windows");
else if (!header.application_flags.HasFlag(ApplicationFlags.FullScreen) && header.application_flags.HasFlag(ApplicationFlags.GUICompatible)) else if (!header.application_flags.HasFlag(ApplicationFlags.FullScreen) && header.application_flags.HasFlag(ApplicationFlags.GUICompatible))
Console.WriteLine("\tApplication is aware of Windows, but doesn't use it"); sb.AppendLine("\tApplication is aware of Windows, but doesn't use it");
else if (header.application_flags.HasFlag(ApplicationFlags.FullScreen) && header.application_flags.HasFlag(ApplicationFlags.GUICompatible)) else if (header.application_flags.HasFlag(ApplicationFlags.FullScreen) && header.application_flags.HasFlag(ApplicationFlags.GUICompatible))
Console.WriteLine("\tApplication uses Windows"); sb.AppendLine("\tApplication uses Windows");
Console.WriteLine("\tReturn thunks are at: {0}", header.return_thunks_offset); sb.AppendFormat("\tReturn thunks are at: {0}", header.return_thunks_offset).AppendLine();
Console.WriteLine("\tSegment reference thunks are at: {0}", header.segment_reference_thunks); sb.AppendFormat("\tSegment reference thunks are at: {0}", header.segment_reference_thunks).AppendLine();
} }
else if (header.target_os == TargetOS.Borland) else if (header.target_os == TargetOS.Borland)
{ {
Console.WriteLine("\tBorland Operating System Services application"); sb.AppendLine("\tBorland Operating System Services application");
Console.WriteLine("\tApplication requires DOS {0}.{1} to run", header.os_major, header.os_minor); sb.AppendFormat("\tApplication requires DOS {0}.{1} to run", header.os_major, header.os_minor).AppendLine();
if (header.application_flags.HasFlag(ApplicationFlags.FullScreen) && !header.application_flags.HasFlag(ApplicationFlags.GUICompatible)) if (header.application_flags.HasFlag(ApplicationFlags.FullScreen) && !header.application_flags.HasFlag(ApplicationFlags.GUICompatible))
Console.WriteLine("\tApplication is full screen, unaware of Windows"); sb.AppendLine("\tApplication is full screen, unaware of Windows");
else if (!header.application_flags.HasFlag(ApplicationFlags.FullScreen) && header.application_flags.HasFlag(ApplicationFlags.GUICompatible)) else if (!header.application_flags.HasFlag(ApplicationFlags.FullScreen) && header.application_flags.HasFlag(ApplicationFlags.GUICompatible))
Console.WriteLine("\tApplication is aware of Windows, but doesn't use it"); sb.AppendLine("\tApplication is aware of Windows, but doesn't use it");
else if (header.application_flags.HasFlag(ApplicationFlags.FullScreen) && header.application_flags.HasFlag(ApplicationFlags.GUICompatible)) else if (header.application_flags.HasFlag(ApplicationFlags.FullScreen) && header.application_flags.HasFlag(ApplicationFlags.GUICompatible))
Console.WriteLine("\tApplication uses Windows"); sb.AppendLine("\tApplication uses Windows");
Console.WriteLine("\tReturn thunks are at: {0}", header.return_thunks_offset); sb.AppendFormat("\tReturn thunks are at: {0}", header.return_thunks_offset).AppendLine();
Console.WriteLine("\tSegment reference thunks are at: {0}", header.segment_reference_thunks); sb.AppendFormat("\tSegment reference thunks are at: {0}", header.segment_reference_thunks).AppendLine();
} }
else else
{ {
Console.WriteLine("\tApplication for unknown OS {0}", (byte)header.target_os); sb.AppendFormat("\tApplication for unknown OS {0}", (byte)header.target_os).AppendLine();
Console.WriteLine("\tApplication requires OS {0}.{1} to run", header.os_major, header.os_minor); sb.AppendFormat("\tApplication requires OS {0}.{1} to run", header.os_major, header.os_minor).AppendLine();
Console.WriteLine("\tReturn thunks are at: {0}", header.return_thunks_offset); sb.AppendFormat("\tReturn thunks are at: {0}", header.return_thunks_offset).AppendLine();
Console.WriteLine("\tSegment reference thunks are at: {0}", header.segment_reference_thunks); sb.AppendFormat("\tSegment reference thunks are at: {0}", header.segment_reference_thunks).AppendLine();
} }
if (header.application_flags.HasFlag(ApplicationFlags.Errors)) if (header.application_flags.HasFlag(ApplicationFlags.Errors))
Console.WriteLine("\tExecutable has errors"); sb.AppendLine("\tExecutable has errors");
if (header.application_flags.HasFlag(ApplicationFlags.NonConforming)) if (header.application_flags.HasFlag(ApplicationFlags.NonConforming))
Console.WriteLine("\tExecutable is non conforming"); sb.AppendLine("\tExecutable is non conforming");
if (header.application_flags.HasFlag(ApplicationFlags.DLL)) if (header.application_flags.HasFlag(ApplicationFlags.DLL))
Console.WriteLine("\tExecutable is a dynamic library or a driver"); sb.AppendLine("\tExecutable is a dynamic library or a driver");
Console.WriteLine("\tMinimum code swap area: {0} bytes", header.minimum_swap_area); sb.AppendFormat("\tMinimum code swap area: {0} bytes", header.minimum_swap_area).AppendLine();
Console.WriteLine("\tFile alignment shift: {0}", 512 << header.alignment_shift); sb.AppendFormat("\tFile alignment shift: {0}", 512 << header.alignment_shift).AppendLine();
Console.WriteLine("\tInitial local heap should be {0} bytes", header.initial_heap); sb.AppendFormat("\tInitial local heap should be {0} bytes", header.initial_heap).AppendLine();
Console.WriteLine("\tInitial stack size should be {0} bytes", header.initial_stack); sb.AppendFormat("\tInitial stack size should be {0} bytes", header.initial_stack).AppendLine();
Console.WriteLine("\tCS:IP entry point: {0:X4}:{1:X4}", (header.entry_point & 0xFFFF0000) >> 16, header.entry_point & 0xFFFF); sb.AppendFormat("\tCS:IP entry point: {0:X4}:{1:X4}", (header.entry_point & 0xFFFF0000) >> 16, header.entry_point & 0xFFFF).AppendLine();
if (!header.application_flags.HasFlag(ApplicationFlags.DLL)) if (!header.application_flags.HasFlag(ApplicationFlags.DLL))
Console.WriteLine("\tSS:SP initial stack pointer: {0:X4}:{1:X4}", (header.stack_pointer & 0xFFFF0000) >> 16, header.stack_pointer & 0xFFFF); sb.AppendFormat("\tSS:SP initial stack pointer: {0:X4}:{1:X4}", (header.stack_pointer & 0xFFFF0000) >> 16, header.stack_pointer & 0xFFFF).AppendLine();
Console.WriteLine("\tEntry table starts at {0} and runs for {1} bytes", header.entry_table_offset, header.entry_table_length); sb.AppendFormat("\tEntry table starts at {0} and runs for {1} bytes", header.entry_table_offset, header.entry_table_length).AppendLine();
Console.WriteLine("\tSegment table starts at {0} and contain {1} segments", header.segment_table_offset, header.segment_count); sb.AppendFormat("\tSegment table starts at {0} and contain {1} segments", header.segment_table_offset, header.segment_count).AppendLine();
Console.WriteLine("\tModule reference table starts at {0} and contain {1} references", header.module_reference_offset, header.reference_count); sb.AppendFormat("\tModule reference table starts at {0} and contain {1} references", header.module_reference_offset, header.reference_count).AppendLine();
Console.WriteLine("\tNon-resident names table starts at {0} and runs for {1} bytes", header.nonresident_names_offset, header.nonresident_table_size); sb.AppendFormat("\tNon-resident names table starts at {0} and runs for {1} bytes", header.nonresident_names_offset, header.nonresident_table_size).AppendLine();
Console.WriteLine("\tResources table starts at {0} and contains {1} entries", header.resource_table_offset, header.resource_entries); sb.AppendFormat("\tResources table starts at {0} and contains {1} entries", header.resource_table_offset, header.resource_entries).AppendLine();
Console.WriteLine("\tResident names table starts at {0}", header.resident_names_offset); sb.AppendFormat("\tResident names table starts at {0}", header.resident_names_offset).AppendLine();
Console.WriteLine("\tImported names table starts at {0}", header.imported_names_offset); sb.AppendFormat("\tImported names table starts at {0}", header.imported_names_offset).AppendLine();
return sb.ToString();
} }
public static ResourceTable GetResources(FileStream exeFs, uint neStart, ushort tableOff) public static ResourceTable GetResources(FileStream exeFs, uint neStart, ushort tableOff)