This commit is contained in:
2018-02-25 03:08:28 +00:00
parent 9dd6c1efa0
commit cf1f1c0289
38 changed files with 3118 additions and 3138 deletions

View File

@@ -25,10 +25,9 @@
// THE SOFTWARE.
using System;
using System.IO;
using System.Runtime.InteropServices;
using System.Collections.Generic;
using System.Globalization;
using System.IO;
using System.Text;
using libexeinfo;
@@ -38,7 +37,7 @@ namespace exeinfo
{
public static void Main(string[] args)
{
if (args.Length != 1)
if(args.Length != 1)
{
Console.WriteLine("exeinfo version 0.1 © 2017 Natalia Portillo");
Console.WriteLine("Usage: exeinfo file.exe");
@@ -49,97 +48,100 @@ namespace exeinfo
bool recognized = false;
MZ mzExe = new MZ(exeFs);
NE neExe = new NE(exeFs);
AtariST stExe = new AtariST(exeFs);
LX lxExe = new LX(exeFs);
COFF coffExe = new COFF(exeFs);
PE peExe = new PE(exeFs);
MZ mzExe = new MZ(exeFs);
NE neExe = new NE(exeFs);
AtariST stExe = new AtariST(exeFs);
LX lxExe = new LX(exeFs);
COFF coffExe = new COFF(exeFs);
PE peExe = new PE(exeFs);
if (mzExe.IsMZ)
if(mzExe.IsMZ)
{
recognized = true;
Console.Write(mzExe.GetInfo());
}
if (neExe.IsNE)
if(neExe.IsNE)
{
recognized = true;
Console.Write(neExe.GetInfo());
if (neExe.Versions != null)
{
foreach (NE.Version vers in neExe.Versions)
if(neExe.Versions != null)
foreach(NE.Version vers in neExe.Versions)
{
Console.WriteLine("\tVersion resource {0}:", vers.Name);
Console.WriteLine("\t\tFile version: {0}", vers.FileVersion);
Console.WriteLine("\tVersion resource {0}:", vers.Name);
Console.WriteLine("\t\tFile version: {0}", vers.FileVersion);
Console.WriteLine("\t\tProduct version: {0}", vers.ProductVersion);
Console.WriteLine("\t\tFile type: {0}", NE.Version.TypeToString(vers.FileType));
if (vers.FileType == NE.VersionFileType.VFT_DRV)
Console.WriteLine("\t\tFile subtype: {0} driver", NE.Version.DriverToString(vers.FileSubtype));
else if (vers.FileType == NE.VersionFileType.VFT_DRV)
Console.WriteLine("\t\tFile type: {0}", NE.Version.TypeToString(vers.FileType));
if(vers.FileType == NE.VersionFileType.VFT_DRV)
Console.WriteLine("\t\tFile subtype: {0} driver",
NE.Version.DriverToString(vers.FileSubtype));
else if(vers.FileType == NE.VersionFileType.VFT_DRV)
Console.WriteLine("\t\tFile subtype: {0} font", NE.Version.FontToString(vers.FileSubtype));
else if (vers.FileSubtype > 0)
else if(vers.FileSubtype > 0)
Console.WriteLine("\t\tFile subtype: {0}", (uint)vers.FileSubtype);
Console.WriteLine("\t\tFile flags: {0}", vers.FileFlags);
Console.WriteLine("\t\tFile OS: {0}", NE.Version.OsToString(vers.FileOS));
Console.WriteLine("\t\tFile flags: {0}", vers.FileFlags);
Console.WriteLine("\t\tFile OS: {0}", NE.Version.OsToString(vers.FileOS));
foreach (KeyValuePair<string, Dictionary<string, string>> strByLang in vers.StringsByLanguage)
foreach(KeyValuePair<string, Dictionary<string, string>> strByLang in vers.StringsByLanguage)
{
string cultureName;
string encodingName;
try
{
cultureName = new CultureInfo(Convert.ToInt32(strByLang.Key.Substring(0, 4), 16)).DisplayName;
cultureName = new CultureInfo(Convert.ToInt32(strByLang.Key.Substring(0, 4), 16))
.DisplayName;
}
catch
{
cultureName = string.Format("unsupported culture 0x{0:X4}", Convert.ToInt32(strByLang.Key.Substring(0, 4), 16));
cultureName =
$"unsupported culture 0x{Convert.ToInt32(strByLang.Key.Substring(0, 4), 16):X4}";
}
try
{
encodingName = Encoding.GetEncoding(Convert.ToInt32(strByLang.Key.Substring(4), 16)).EncodingName;
encodingName = Encoding
.GetEncoding(Convert.ToInt32(strByLang.Key.Substring(4), 16))
.EncodingName;
}
catch
{
encodingName = string.Format("unsupported encoding 0x{0:X4}", Convert.ToInt32(strByLang.Key.Substring(4), 16));
encodingName =
$"unsupported encoding 0x{Convert.ToInt32(strByLang.Key.Substring(4), 16):X4}";
}
Console.WriteLine("\t\tStrings for {0} in codepage {1}:", cultureName, encodingName);
foreach (KeyValuePair<string, string> strings in strByLang.Value)
foreach(KeyValuePair<string, string> strings in strByLang.Value)
Console.WriteLine("\t\t\t{0}: {1}", strings.Key, strings.Value);
}
}
}
}
if (stExe.IsAtariST)
{
recognized = true;
if(stExe.IsAtariST)
{
recognized = true;
Console.Write(stExe.GetInfo());
}
}
if (lxExe.IsLX)
{
recognized = true;
Console.Write(lxExe.GetInfo());
}
if(lxExe.IsLX)
{
recognized = true;
Console.Write(lxExe.GetInfo());
}
if (coffExe.IsCOFF)
{
recognized = true;
Console.Write(coffExe.GetInfo());
}
if(coffExe.IsCOFF)
{
recognized = true;
Console.Write(coffExe.GetInfo());
}
if (peExe.IsPE)
{
recognized = true;
Console.Write(peExe.GetInfo());
}
if(peExe.IsPE)
{
recognized = true;
Console.Write(peExe.GetInfo());
}
if (!recognized)
Console.WriteLine("Executable format not recognized");
if(!recognized) Console.WriteLine("Executable format not recognized");
}
}
}

View File

@@ -1,15 +1,41 @@
using System;
//
// Program.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 Eto;
using Eto.Forms;
using Eto.Drawing;
namespace exeinfogui.Desktop
{
class Program
static class Program
{
[STAThread]
static void Main(string[] args)
{
new Application(Eto.Platform.Detect).Run(new MainForm());
new Application(Platform.Detect).Run(new MainForm());
}
}
}

View File

@@ -1,4 +1,30 @@
using AppKit;
//
// Program.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 AppKit;
using Eto.Forms;
namespace exeinfogui.XamMac

View File

@@ -1,3 +1,29 @@
//
// MainForm.xeto.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 Eto.Forms;
@@ -8,9 +34,9 @@ namespace exeinfogui
{
public class MainForm : Form
{
TextBox txtFile;
TextBox txtType;
TextBox txtFile;
TextArea txtInformation;
TextBox txtType;
public MainForm()
{
@@ -19,9 +45,9 @@ namespace exeinfogui
protected void OnBtnLoadClick(object sender, EventArgs e)
{
txtFile.Text = "";
txtType.Text = "";
txtInformation.Text ="";
txtFile.Text = "";
txtType.Text = "";
txtInformation.Text = "";
OpenFileDialog dlgOpen = new OpenFileDialog {Title = "Choose executable file", MultiSelect = false};
@@ -62,12 +88,12 @@ namespace exeinfogui
}
else if(stExe.IsAtariST)
{
txtType.Text = "Atari ST executable";
txtType.Text = "Atari ST executable";
txtInformation.Text = stExe.GetInfo();
}
else if(coffExe.IsCOFF)
{
txtType.Text = "Common Object File Format (COFF)";
txtType.Text = "Common Object File Format (COFF)";
txtInformation.Text = coffExe.GetInfo();
}
else

View File

@@ -23,84 +23,85 @@
// 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.IO;
using System.Runtime.InteropServices;
namespace libexeinfo
{
/// <summary>
/// Represents an Atari ST executable
/// Represents an Atari ST executable
/// </summary>
public partial class AtariST
{
/// <summary>
/// The <see cref="FileStream"/> that contains the executable represented by this instance
/// </summary>
public readonly FileStream BaseStream;
/// <summary>
/// Header for this executable
/// </summary>
public readonly AtariHeader Header;
/// <summary>
/// If true this instance correctly represents an Atari ST executable
/// </summary>
public readonly bool IsAtariST;
/// <summary>
/// The <see cref="FileStream" /> that contains the executable represented by this instance
/// </summary>
public readonly FileStream BaseStream;
/// <summary>
/// Header for this executable
/// </summary>
public readonly AtariHeader Header;
/// <summary>
/// If true this instance correctly represents an Atari ST executable
/// </summary>
public readonly bool IsAtariST;
/// <summary>
/// Initializes a new instance of the <see cref="T:libexeinfo.AtariST"/> class.
/// </summary>
/// <param name="path">Executable path.</param>
public AtariST(string path)
{
byte[] buffer = new byte[Marshal.SizeOf(typeof(AtariHeader))];
/// <summary>
/// Initializes a new instance of the <see cref="T:libexeinfo.AtariST" /> class.
/// </summary>
/// <param name="path">Executable path.</param>
public AtariST(string path)
{
byte[] buffer = new byte[Marshal.SizeOf(typeof(AtariHeader))];
BaseStream = File.Open(path, FileMode.Open, FileAccess.Read);
BaseStream.Position = 0;
BaseStream.Read(buffer, 0, buffer.Length);
Header = BigEndianMarshal.ByteArrayToStructureBigEndian<AtariHeader>(buffer);
IsAtariST = Header.signature == Signature;
}
BaseStream = File.Open(path, FileMode.Open, FileAccess.Read);
BaseStream.Position = 0;
BaseStream.Read(buffer, 0, buffer.Length);
Header = BigEndianMarshal.ByteArrayToStructureBigEndian<AtariHeader>(buffer);
IsAtariST = Header.signature == SIGNATURE;
}
/// <summary>
/// Initializes a new instance of the <see cref="T:libexeinfo.AtariST"/> class.
/// </summary>
/// <param name="stream">Stream containing the executable.</param>
public AtariST(FileStream stream)
{
byte[] buffer = new byte[Marshal.SizeOf(typeof(AtariHeader))];
/// <summary>
/// Initializes a new instance of the <see cref="T:libexeinfo.AtariST" /> class.
/// </summary>
/// <param name="stream">Stream containing the executable.</param>
public AtariST(FileStream stream)
{
byte[] buffer = new byte[Marshal.SizeOf(typeof(AtariHeader))];
BaseStream = stream;
BaseStream.Position = 0;
BaseStream.Read(buffer, 0, buffer.Length);
Header = BigEndianMarshal.ByteArrayToStructureBigEndian<AtariHeader>(buffer);
IsAtariST = Header.signature == Signature;
}
BaseStream = stream;
BaseStream.Position = 0;
BaseStream.Read(buffer, 0, buffer.Length);
Header = BigEndianMarshal.ByteArrayToStructureBigEndian<AtariHeader>(buffer);
IsAtariST = Header.signature == SIGNATURE;
}
/// <summary>
/// Identifies if the specified executable is a Atari ST executable
/// </summary>
/// <returns><c>true</c> if the specified executable is a Atari ST executable, <c>false</c> otherwise.</returns>
/// <param name="path">Executable path.</param>
public static bool Identify(string path)
{
FileStream exeFs = File.Open(path, FileMode.Open, FileAccess.Read);
return Identify(exeFs);
}
/// <summary>
/// Identifies if the specified executable is a Atari ST executable
/// </summary>
/// <returns><c>true</c> if the specified executable is a Atari ST executable, <c>false</c> otherwise.</returns>
/// <param name="path">Executable path.</param>
public static bool Identify(string path)
{
FileStream exeFs = File.Open(path, FileMode.Open, FileAccess.Read);
return Identify(exeFs);
}
/// <summary>
/// Identifies if the specified executable is a Atari ST executable
/// </summary>
/// <returns><c>true</c> if the specified executable is a Atari ST executable, <c>false</c> otherwise.</returns>
/// <param name="stream">Stream containing the executable.</param>
public static bool Identify(FileStream stream)
{
byte[] buffer = new byte[Marshal.SizeOf(typeof(AtariHeader))];
/// <summary>
/// Identifies if the specified executable is a Atari ST executable
/// </summary>
/// <returns><c>true</c> if the specified executable is a Atari ST executable, <c>false</c> otherwise.</returns>
/// <param name="stream">Stream containing the executable.</param>
public static bool Identify(FileStream stream)
{
byte[] buffer = new byte[Marshal.SizeOf(typeof(AtariHeader))];
stream.Position = 0;
stream.Read(buffer, 0, buffer.Length);
AtariHeader hdr = BigEndianMarshal.ByteArrayToStructureBigEndian<AtariHeader>(buffer);
stream.Position = 0;
stream.Read(buffer, 0, buffer.Length);
AtariHeader hdr = BigEndianMarshal.ByteArrayToStructureBigEndian<AtariHeader>(buffer);
return hdr.signature == Signature;
}
}
return hdr.signature == SIGNATURE;
}
}
}

View File

@@ -23,11 +23,11 @@
// 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;
namespace libexeinfo
{
public partial class AtariST
{
public const ushort Signature = 0x601A;
}
public partial class AtariST
{
const ushort SIGNATURE = 0x601A;
}
}

View File

@@ -29,30 +29,30 @@ using System.Text;
namespace libexeinfo
{
public partial class AtariST
{
/// <summary>
/// Gets a string with human readable information for a given Atari ST header
/// </summary>
/// <returns>Human readable information for given Atari ST header.</returns>
/// <param name="header">Atari ST executable header.</param>
public static string GetInfo(AtariHeader header)
{
{
/// <summary>
/// Gets a string with human readable information for a given Atari ST header
/// </summary>
/// <returns>Human readable information for given Atari ST header.</returns>
/// <param name="header">Atari ST executable header.</param>
public static string GetInfo(AtariHeader header)
{
StringBuilder sb = new StringBuilder();
sb.AppendLine("Atari ST executable:");
sb.AppendFormat("\t{0} bytes in text segment", header.text_len).AppendLine();
sb.AppendFormat("\t{0} bytes in data segment", header.data_len).AppendLine();
sb.AppendFormat("\t{0} bytes in BSS segment", header.bss_len).AppendLine();
sb.AppendFormat("\t{0} bytes in BSS segment", header.bss_len).AppendLine();
sb.AppendFormat("\t{0} bytes in symbol table", header.symb_len).AppendLine();
return sb.ToString();
}
return sb.ToString();
}
/// <summary>
/// Gets a string with human readable information for the Atari ST executable represented by this instance
/// </summary>
/// <returns>Human readable information for this instance.</returns>
public string GetInfo()
/// <summary>
/// Gets a string with human readable information for the Atari ST executable represented by this instance
/// </summary>
/// <returns>Human readable information for this instance.</returns>
public string GetInfo()
{
return GetInfo(Header);
}
}
}
}

View File

@@ -23,24 +23,24 @@
// 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.Runtime.InteropServices;
namespace libexeinfo
{
public partial class AtariST
{
[StructLayout(LayoutKind.Sequential, Pack = 1)]
public struct AtariHeader
{
[StructLayout(LayoutKind.Sequential, Pack = 1)]
public struct AtariHeader
{
public ushort signature;
public uint text_len;
public uint data_len;
public uint bss_len;
public uint symb_len;
public uint reserved;
public uint flags;
public uint text_len;
public uint data_len;
public uint bss_len;
public uint symb_len;
public uint reserved;
public uint flags;
public ushort absflags;
}
}
}
}

View File

@@ -23,6 +23,7 @@
// 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.Linq;
using System.Reflection;
@@ -33,8 +34,8 @@ namespace libexeinfo
public static class BigEndianMarshal
{
/// <summary>
/// Marshals a big endian structure from a byte array.
/// Nested structures are still marshalled as little endian.
/// Marshals a big endian structure from a byte array.
/// Nested structures are still marshalled as little endian.
/// </summary>
/// <returns>The structure.</returns>
/// <param name="bytes">Byte array.</param>
@@ -42,81 +43,80 @@ namespace libexeinfo
public static T ByteArrayToStructureBigEndian<T>(byte[] bytes) where T : struct
{
GCHandle ptr = GCHandle.Alloc(bytes, GCHandleType.Pinned);
T str = (T)Marshal.PtrToStructure(ptr.AddrOfPinnedObject(), typeof(T));
T str = (T)Marshal.PtrToStructure(ptr.AddrOfPinnedObject(), typeof(T));
ptr.Free();
return SwapStructureMembersEndian(str);
}
/// <summary>
/// Swaps endian of structure members that correspond to numerical types.
/// Does not traverse nested structures.
/// Swaps endian of structure members that correspond to numerical types.
/// Does not traverse nested structures.
/// </summary>
/// <returns>The structure with its members endian swapped.</returns>
/// <param name="str">The structure.</param>
/// <typeparam name="T">Structure type.</typeparam>
public static T SwapStructureMembersEndian<T>(T str) where T : struct
{
Type t = str.GetType();
Type t = str.GetType();
FieldInfo[] fieldInfo = t.GetFields();
foreach (FieldInfo fi in fieldInfo)
{
if (fi.FieldType == typeof(short))
foreach(FieldInfo fi in fieldInfo)
if(fi.FieldType == typeof(short))
{
short int16 = (short)fi.GetValue(str);
short int16 = (short)fi.GetValue(str);
byte[] int16_b = BitConverter.GetBytes(int16);
byte[] int16_r = int16_b.Reverse().ToArray();
fi.SetValueDirect(__makeref(str), BitConverter.ToInt16(int16_r, 0));
}
else if (fi.FieldType == typeof(int))
else if(fi.FieldType == typeof(int))
{
int int32 = (int)fi.GetValue(str);
int int32 = (int)fi.GetValue(str);
byte[] int32_b = BitConverter.GetBytes(int32);
byte[] int32_r = int32_b.Reverse().ToArray();
fi.SetValueDirect(__makeref(str), BitConverter.ToInt32(int32_r, 0));
}
else if (fi.FieldType == typeof(long))
else if(fi.FieldType == typeof(long))
{
long int64 = (long)fi.GetValue(str);
long int64 = (long)fi.GetValue(str);
byte[] int64_b = BitConverter.GetBytes(int64);
byte[] int64_r = int64_b.Reverse().ToArray();
fi.SetValueDirect(__makeref(str), BitConverter.ToInt64(int64_r, 0));
}
else if (fi.FieldType == typeof(ushort))
else if(fi.FieldType == typeof(ushort))
{
ushort uint16 = (ushort)fi.GetValue(str);
ushort uint16 = (ushort)fi.GetValue(str);
byte[] uint16_b = BitConverter.GetBytes(uint16);
byte[] uint16_r = uint16_b.Reverse().ToArray();
fi.SetValueDirect(__makeref(str), BitConverter.ToInt16(uint16_r, 0));
}
else if (fi.FieldType == typeof(uint))
else if(fi.FieldType == typeof(uint))
{
uint uint32 = (uint)fi.GetValue(str);
uint uint32 = (uint)fi.GetValue(str);
byte[] uint32_b = BitConverter.GetBytes(uint32);
byte[] uint32_r = uint32_b.Reverse().ToArray();
fi.SetValueDirect(__makeref(str), BitConverter.ToInt32(uint32_r, 0));
}
else if (fi.FieldType == typeof(ulong))
else if(fi.FieldType == typeof(ulong))
{
ulong uint64 = (ulong)fi.GetValue(str);
ulong uint64 = (ulong)fi.GetValue(str);
byte[] uint64_b = BitConverter.GetBytes(uint64);
byte[] uint64_r = uint64_b.Reverse().ToArray();
fi.SetValueDirect(__makeref(str), BitConverter.ToInt64(uint64_r, 0));
}
else if (fi.FieldType == typeof(float))
else if(fi.FieldType == typeof(float))
{
float flt = (float)fi.GetValue(str);
float flt = (float)fi.GetValue(str);
byte[] flt_b = BitConverter.GetBytes(flt);
byte[] flt_r = flt_b.Reverse().ToArray();
fi.SetValueDirect(__makeref(str), BitConverter.ToSingle(flt_r, 0));
}
else if (fi.FieldType == typeof(double))
else if(fi.FieldType == typeof(double))
{
double dbl = (double)fi.GetValue(str);
double dbl = (double)fi.GetValue(str);
byte[] dbl_b = BitConverter.GetBytes(dbl);
byte[] dbl_r = dbl_b.Reverse().ToArray();
fi.SetValueDirect(__makeref(str), BitConverter.ToDouble(dbl_r, 0));
}
}
return str;
}
}

View File

@@ -23,130 +23,136 @@
// 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
{
/// <summary>
/// Represents a Common Object File Format
/// </summary>
public partial class COFF
/// <summary>
/// Represents a Common Object File Format
/// </summary>
public partial class COFF
{
/// <summary>
/// The <see cref="FileStream"/> that contains the executable represented by this instance
/// </summary>
public readonly FileStream BaseStream;
/// <summary>
/// Header for this executable
/// </summary>
public readonly COFFHeader Header;
/// <summary>
/// If true this instance correctly represents a Common Object File Format
/// </summary>
public readonly bool IsCOFF;
public readonly bool IsBigEndian;
/// <summary>
/// The <see cref="FileStream" /> that contains the executable represented by this instance
/// </summary>
public readonly FileStream BaseStream;
/// <summary>
/// Header for this executable
/// </summary>
public readonly COFFHeader Header;
public readonly bool IsBigEndian;
/// <summary>
/// If true this instance correctly represents a Common Object File Format
/// </summary>
public readonly bool IsCOFF;
/// <summary>
/// Initializes a new instance of the <see cref="T:libexeinfo.COFF"/> class.
/// </summary>
/// <param name="path">Executable path.</param>
public COFF(string path)
/// <summary>
/// Initializes a new instance of the <see cref="T:libexeinfo.COFF" /> class.
/// </summary>
/// <param name="path">Executable path.</param>
public COFF(string path)
{
byte[] buffer = new byte[Marshal.SizeOf(typeof(COFFHeader))];
byte[] buffer = new byte[Marshal.SizeOf(typeof(COFFHeader))];
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 = (COFFHeader)Marshal.PtrToStructure(hdrPtr, typeof(COFFHeader));
Marshal.FreeHGlobal(hdrPtr);
IsCOFF = Header.optionalHeader.magic == STMAGIC || Header.optionalHeader.magic == OMAGIC || Header.optionalHeader.magic == JMAGIC ||
Header.optionalHeader.magic == DMAGIC || Header.optionalHeader.magic == ZMAGIC || Header.optionalHeader.magic == SHMAGIC;
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 = (COFFHeader)Marshal.PtrToStructure(hdrPtr, typeof(COFFHeader));
Marshal.FreeHGlobal(hdrPtr);
IsCOFF = Header.optionalHeader.magic == STMAGIC || Header.optionalHeader.magic == OMAGIC ||
Header.optionalHeader.magic == JMAGIC || Header.optionalHeader.magic == DMAGIC ||
Header.optionalHeader.magic == ZMAGIC || Header.optionalHeader.magic == SHMAGIC;
IsBigEndian = false;
if(!IsCOFF)
{
Header = SwapHeader(Header);
IsCOFF = Header.optionalHeader.magic == STMAGIC || Header.optionalHeader.magic == OMAGIC || Header.optionalHeader.magic == JMAGIC ||
Header.optionalHeader.magic == DMAGIC || Header.optionalHeader.magic == ZMAGIC || Header.optionalHeader.magic == SHMAGIC;
IsBigEndian = !IsCOFF;
}
}
Header = SwapHeader(Header);
IsCOFF = Header.optionalHeader.magic == STMAGIC || Header.optionalHeader.magic == OMAGIC ||
Header.optionalHeader.magic == JMAGIC || Header.optionalHeader.magic == DMAGIC ||
Header.optionalHeader.magic == ZMAGIC || Header.optionalHeader.magic == SHMAGIC;
IsBigEndian = !IsCOFF;
}
}
/// <summary>
/// Initializes a new instance of the <see cref="T:libexeinfo.COFF"/> class.
/// </summary>
/// <param name="stream">Stream containing the executable.</param>
public COFF(FileStream stream)
{
byte[] buffer = new byte[Marshal.SizeOf(typeof(COFFHeader))];
/// <summary>
/// Initializes a new instance of the <see cref="T:libexeinfo.COFF" /> class.
/// </summary>
/// <param name="stream">Stream containing the executable.</param>
public COFF(FileStream stream)
{
byte[] buffer = new byte[Marshal.SizeOf(typeof(COFFHeader))];
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 = (COFFHeader)Marshal.PtrToStructure(hdrPtr, typeof(COFFHeader));
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 = (COFFHeader)Marshal.PtrToStructure(hdrPtr, typeof(COFFHeader));
Marshal.FreeHGlobal(hdrPtr);
IsCOFF = Header.optionalHeader.magic == STMAGIC || Header.optionalHeader.magic == OMAGIC || Header.optionalHeader.magic == JMAGIC ||
Header.optionalHeader.magic == DMAGIC || Header.optionalHeader.magic == ZMAGIC || Header.optionalHeader.magic == SHMAGIC;
IsCOFF = Header.optionalHeader.magic == STMAGIC || Header.optionalHeader.magic == OMAGIC ||
Header.optionalHeader.magic == JMAGIC || Header.optionalHeader.magic == DMAGIC ||
Header.optionalHeader.magic == ZMAGIC || Header.optionalHeader.magic == SHMAGIC;
IsBigEndian = false;
if (!IsCOFF)
{
if(!IsCOFF)
{
Header = SwapHeader(Header);
IsCOFF = Header.optionalHeader.magic == STMAGIC || Header.optionalHeader.magic == OMAGIC || Header.optionalHeader.magic == JMAGIC ||
Header.optionalHeader.magic == DMAGIC || Header.optionalHeader.magic == ZMAGIC || Header.optionalHeader.magic == SHMAGIC;
IsBigEndian = !IsCOFF;
}
}
IsCOFF = Header.optionalHeader.magic == STMAGIC || Header.optionalHeader.magic == OMAGIC ||
Header.optionalHeader.magic == JMAGIC || Header.optionalHeader.magic == DMAGIC ||
Header.optionalHeader.magic == ZMAGIC || Header.optionalHeader.magic == SHMAGIC;
IsBigEndian = !IsCOFF;
}
}
/// <summary>
/// Identifies if the specified executable is a Common Object File Format
/// </summary>
/// <returns><c>true</c> if the specified executable is a Common Object File Format, <c>false</c> otherwise.</returns>
/// <param name="path">Executable path.</param>
public static bool Identify(string path)
/// <summary>
/// Identifies if the specified executable is a Common Object File Format
/// </summary>
/// <returns><c>true</c> if the specified executable is a Common Object File Format, <c>false</c> otherwise.</returns>
/// <param name="path">Executable path.</param>
public static bool Identify(string path)
{
FileStream exeFs = File.Open(path, FileMode.Open, FileAccess.Read);
FileStream exeFs = File.Open(path, FileMode.Open, FileAccess.Read);
return Identify(exeFs);
}
}
/// <summary>
/// Identifies if the specified executable is a Common Object File Format
/// </summary>
/// <returns><c>true</c> if the specified executable is a Common Object File Format, <c>false</c> otherwise.</returns>
/// <param name="stream">Stream containing the executable.</param>
public static bool Identify(FileStream stream)
{
byte[] buffer = new byte[Marshal.SizeOf(typeof(COFFHeader))];
/// <summary>
/// Identifies if the specified executable is a Common Object File Format
/// </summary>
/// <returns><c>true</c> if the specified executable is a Common Object File Format, <c>false</c> otherwise.</returns>
/// <param name="stream">Stream containing the executable.</param>
public static bool Identify(FileStream stream)
{
byte[] buffer = new byte[Marshal.SizeOf(typeof(COFFHeader))];
stream.Position = 0;
stream.Read(buffer, 0, buffer.Length);
IntPtr hdrPtr = Marshal.AllocHGlobal(buffer.Length);
Marshal.Copy(buffer, 0, hdrPtr, buffer.Length);
COFFHeader COFFHdr = (COFFHeader)Marshal.PtrToStructure(hdrPtr, typeof(COFFHeader));
Marshal.FreeHGlobal(hdrPtr);
stream.Read(buffer, 0, buffer.Length);
IntPtr hdrPtr = Marshal.AllocHGlobal(buffer.Length);
Marshal.Copy(buffer, 0, hdrPtr, buffer.Length);
COFFHeader COFFHdr = (COFFHeader)Marshal.PtrToStructure(hdrPtr, typeof(COFFHeader));
Marshal.FreeHGlobal(hdrPtr);
if (COFFHdr.optionalHeader.magic == STMAGIC || COFFHdr.optionalHeader.magic == OMAGIC || COFFHdr.optionalHeader.magic == JMAGIC ||
COFFHdr.optionalHeader.magic == DMAGIC || COFFHdr.optionalHeader.magic == ZMAGIC || COFFHdr.optionalHeader.magic == SHMAGIC)
return true;
if(COFFHdr.optionalHeader.magic == STMAGIC || COFFHdr.optionalHeader.magic == OMAGIC ||
COFFHdr.optionalHeader.magic == JMAGIC || COFFHdr.optionalHeader.magic == DMAGIC ||
COFFHdr.optionalHeader.magic == ZMAGIC || COFFHdr.optionalHeader.magic == SHMAGIC) return true;
COFFHdr = SwapHeader(COFFHdr);
return COFFHdr.optionalHeader.magic == STMAGIC || COFFHdr.optionalHeader.magic == OMAGIC || COFFHdr.optionalHeader.magic == JMAGIC ||
COFFHdr.optionalHeader.magic == DMAGIC || COFFHdr.optionalHeader.magic == ZMAGIC || COFFHdr.optionalHeader.magic == SHMAGIC;
}
COFFHdr = SwapHeader(COFFHdr);
return COFFHdr.optionalHeader.magic == STMAGIC || COFFHdr.optionalHeader.magic == OMAGIC ||
COFFHdr.optionalHeader.magic == JMAGIC || COFFHdr.optionalHeader.magic == DMAGIC ||
COFFHdr.optionalHeader.magic == ZMAGIC || COFFHdr.optionalHeader.magic == SHMAGIC;
}
static COFFHeader SwapHeader(COFFHeader header)
{
COFFHeader swapped = BigEndianMarshal.SwapStructureMembersEndian(header);
COFFHeader swapped = BigEndianMarshal.SwapStructureMembersEndian(header);
swapped.characteristics = (Characteristics)Swapping.Swap((ushort)header.characteristics);
swapped.machine = (MachineTypes)Swapping.Swap((ushort)header.machine);
swapped.optionalHeader = BigEndianMarshal.SwapStructureMembersEndian(header.optionalHeader);
swapped.machine = (MachineTypes)Swapping.Swap((ushort)header.machine);
swapped.optionalHeader = BigEndianMarshal.SwapStructureMembersEndian(header.optionalHeader);
return swapped;
}
}
}
}
}

View File

@@ -28,86 +28,86 @@ namespace libexeinfo
{
public partial class COFF
{
public const ushort STMAGIC = 0x101;
public const ushort OMAGIC = 0x104;
/// <summary>
/// dirty text and data image, can't share
/// </summary>
public const ushort JMAGIC = 0x107;
/// <summary>
/// dirty text segment, data aligned
/// </summary>
public const ushort DMAGIC = 0x108;
/// <summary>
/// The proper magic number for executables
/// </summary>
public const ushort ZMAGIC = 0x10b;
/// <summary>
/// shared library header
/// </summary>
public const ushort SHMAGIC = 0x123;
public const ushort STMAGIC = 0x101;
public const ushort OMAGIC = 0x104;
/// <summary>
/// dirty text and data image, can't share
/// </summary>
public const ushort JMAGIC = 0x107;
/// <summary>
/// dirty text segment, data aligned
/// </summary>
public const ushort DMAGIC = 0x108;
/// <summary>
/// The proper magic number for executables
/// </summary>
public const ushort ZMAGIC = 0x10b;
/// <summary>
/// shared library header
/// </summary>
public const ushort SHMAGIC = 0x123;
/// <summary>
/// Alpha architecture information
/// </summary>
public readonly string SectionAlpha = ".arch";
/// <summary>
/// Uninitialized data
/// </summary>
public readonly string SectionBss = ".bss";
/// <summary>
/// Initialized data
/// </summary>
public readonly string SectionData = ".data";
/// <summary>
/// The export data section, named .edata, contains information about
/// symbols that other images can access through dynamic linking.
/// Exports are generally found in DLLs, but DLLs can import symbols as
/// well.
/// </summary>
public readonly string SectionExport = ".edata";
/// <summary>
/// Import tables
/// </summary>
public readonly string SectionImport = ".idata";
/// <summary>
/// Exception information
/// </summary>
public readonly string SectionException = ".pdata";
/// <summary>
/// Read-only initialized data
/// </summary>
public readonly string SectionReadOnly = ".rdata";
/// <summary>
/// Image relocations
/// </summary>
public readonly string SectionRelocations = ".reloc";
/// <summary>
/// Resource directory
/// </summary>
public readonly string SectionResource = ".rsrc";
/// <summary>
/// Executable code
/// </summary>
public readonly string SectionExecutable = ".text";
/// <summary>
/// Thread-local storage
/// </summary>
public readonly string SectionTls = ".tls";
/// <summary>
/// Exception information
/// </summary>
public readonly string SectioneXception = ".xdata";
/// <summary>
/// Debug information
/// </summary>
public readonly string SectionDebug = ".debug";
/// <summary>
/// A .drectve section consists of a string of ASCII text. This string is
/// a series of linker options (each option containing hyphen, option
/// name, and any appropriate attribute) separated by spaces.The
/// .drectve section must not have relocations or line numbers.
/// </summary>
public readonly string SectionDirectives = ".drectve";
}
/// <summary>
/// Alpha architecture information
/// </summary>
public readonly string SectionAlpha = ".arch";
/// <summary>
/// Uninitialized data
/// </summary>
public readonly string SectionBss = ".bss";
/// <summary>
/// Initialized data
/// </summary>
public readonly string SectionData = ".data";
/// <summary>
/// Debug information
/// </summary>
public readonly string SectionDebug = ".debug";
/// <summary>
/// A .drectve section consists of a string of ASCII text. This string is
/// a series of linker options (each option containing hyphen, option
/// name, and any appropriate attribute) separated by spaces.The
/// .drectve section must not have relocations or line numbers.
/// </summary>
public readonly string SectionDirectives = ".drectve";
/// <summary>
/// Exception information
/// </summary>
public readonly string SectioneXception = ".xdata";
/// <summary>
/// Exception information
/// </summary>
public readonly string SectionException = ".pdata";
/// <summary>
/// Executable code
/// </summary>
public readonly string SectionExecutable = ".text";
/// <summary>
/// The export data section, named .edata, contains information about
/// symbols that other images can access through dynamic linking.
/// Exports are generally found in DLLs, but DLLs can import symbols as
/// well.
/// </summary>
public readonly string SectionExport = ".edata";
/// <summary>
/// Import tables
/// </summary>
public readonly string SectionImport = ".idata";
/// <summary>
/// Read-only initialized data
/// </summary>
public readonly string SectionReadOnly = ".rdata";
/// <summary>
/// Image relocations
/// </summary>
public readonly string SectionRelocations = ".reloc";
/// <summary>
/// Resource directory
/// </summary>
public readonly string SectionResource = ".rsrc";
/// <summary>
/// Thread-local storage
/// </summary>
public readonly string SectionTls = ".tls";
}
}

View File

@@ -25,391 +25,401 @@
// THE SOFTWARE.
using System;
namespace libexeinfo
{
public partial class COFF
{
/// <summary>
/// The Machine field has one of the following values that specifies its CPU type. An image file can be run only on the specified machine or on a system that emulates the specified machine.
/// </summary>
public enum MachineTypes : ushort
{
/// <summary>
/// The contents of this field are assumed to be applicable to any machine type
/// </summary>
IMAGE_FILE_MACHINE_UNKNOWN = 0x0,
/// <summary>
/// Alpha AXP
/// </summary>
IMAGE_FILE_MACHINE_ALPHA = 0x184,
/// <summary>
/// Alpha AXP 64-bit.
/// </summary>
IMAGE_FILE_MACHINE_ALPHA64 = 0x284,
/// <summary>
/// Matsushita AM33
/// </summary>
IMAGE_FILE_MACHINE_AM33 = 0x1d3,
/// <summary>
/// x64
/// </summary>
IMAGE_FILE_MACHINE_AMD64 = 0x8664,
/// <summary>
/// ARM little endian
/// </summary>
IMAGE_FILE_MACHINE_ARM = 0x1c0,
/// <summary>
/// ARM64 little endian
/// </summary>
IMAGE_FILE_MACHINE_ARM64 = 0xaa64,
/// <summary>
/// ARM Thumb-2 little endian
/// </summary>
IMAGE_FILE_MACHINE_ARMNT = 0x1c4,
/// <summary>
/// Clipper
/// </summary>
IMAGE_FILE_MACHINE_CLIPPER = 0x17f,
/// <summary>
/// EFI byte code
/// </summary>
IMAGE_FILE_MACHINE_EBC = 0xebc,
/// <summary>
/// Intel 386 or later processors and compatible processors
/// </summary>
IMAGE_FILE_MACHINE_I386 = 0x14c,
/// <summary>
/// Intel 386 or later processors and compatible processors, used by AIX
/// </summary>
IMAGE_FILE_MACHINE_I386_AIX = 0x175,
/// <summary>
/// Intel Itanium processor family
/// </summary>
IMAGE_FILE_MACHINE_IA64 = 0x200,
/// <summary>
/// Mitsubishi M32R little endian
/// </summary>
IMAGE_FILE_MACHINE_M32R = 0x9041,
/// <summary>
/// Motorola 68000 series
/// </summary>
IMAGE_FILE_MACHINE_M68K = 0x268,
/// <summary>
/// Motorola 68000 series
/// </summary>
IMAGE_FILE_MACHINE_M68K_OTHER = 0x150,
/// <summary>
/// MIPS16
/// </summary>
IMAGE_FILE_MACHINE_MIPS16 = 0x266,
/// <summary>
/// MIPS with FPU
/// </summary>
IMAGE_FILE_MACHINE_MIPSFPU = 0x366,
/// <summary>
/// MIPS16 with FPU
/// </summary>
IMAGE_FILE_MACHINE_MIPSFPU16 = 0x466,
/// <summary>
/// PowerPC little endian
/// </summary>
IMAGE_FILE_MACHINE_POWERPC = 0x1f0,
/// <summary>
/// Power PC with floating point support
/// </summary>
IMAGE_FILE_MACHINE_POWERPCFP = 0x1f1,
/// <summary>
/// MIPS big endian
/// </summary>
IMAGE_FILE_MACHINE_MIPSEB = 0x160,
/// <summary>
/// MIPS little endian
/// </summary>
IMAGE_FILE_MACHINE_R3000 = 0x162,
/// <summary>
/// MIPS little endian
/// </summary>
IMAGE_FILE_MACHINE_R4000 = 0x166,
/// <summary>
/// MIPS little endian
/// </summary>
IMAGE_FILE_MACHINE_R10000 = 0x168,
/// <summary>
/// RISC-V 32-bit address space
/// </summary>
IMAGE_FILE_MACHINE_RISCV32 = 0x5032,
/// <summary>
/// RISC-V 64-bit address space
/// </summary>
IMAGE_FILE_MACHINE_RISCV64 = 0x5064,
/// <summary>
/// RISC-V 128-bit address space
/// </summary>
IMAGE_FILE_MACHINE_RISCV128 = 0x5128,
/// <summary>
/// Hitachi SH3
/// </summary>
IMAGE_FILE_MACHINE_SH3 = 0x1a2,
/// <summary>
/// Hitachi SH3 DSP
/// </summary>
IMAGE_FILE_MACHINE_SH3DSP = 0x1a3,
/// <summary>
/// Hitachi SH4
/// </summary>
IMAGE_FILE_MACHINE_SH4 = 0x1a6,
/// <summary>
/// Hitachi SH5
/// </summary>
IMAGE_FILE_MACHINE_SH5 = 0x1a8,
/// <summary>
/// Thumb
/// </summary>
IMAGE_FILE_MACHINE_THUMB = 0x1c2,
/// <summary>
/// MIPS little-endian WCE v2
/// </summary>
IMAGE_FILE_MACHINE_WCEMIPSV2 = 0x169,
/// <summary>
/// WE32000
/// </summary>
IMAGE_FILE_MACHINE_WE32000 = 0x170
}
/// <summary>
/// The Characteristics field contains flags that indicate attributes of the object or image file.
/// </summary>
[Flags]
/// <summary>
/// The Characteristics field contains flags that indicate attributes of the object or image file.
/// </summary>
[Flags]
public enum Characteristics : ushort
{
/// <summary>
/// Image only, Windows CE, and Microsoft Windows NT and later. This indicates that the file does not contain base relocations and must therefore be loaded at its preferred base address. If the base address is not available, the loader reports an error. The default behavior of the linker is to strip base relocations from executable (EXE) files.
/// </summary>
IMAGE_FILE_RELOCS_STRIPPED = 0x0001,
/// <summary>
/// Image only. This indicates that the image file is valid and can be run. If this flag is not set, it indicates a linker error.
/// </summary>
IMAGE_FILE_EXECUTABLE_IMAGE = 0x0002,
/// <summary>
/// COFF line numbers have been removed. This flag is deprecated and should be zero.
/// </summary>
IMAGE_FILE_LINE_NUMS_STRIPPED = 0x0004,
/// <summary>
/// COFF symbol table entries for local symbols have been removed. This flag is deprecated and should be zero.
/// </summary>
IMAGE_FILE_LOCAL_SYMS_STRIPPED = 0x0008,
/// <summary>
/// Obsolete. Aggressively trim working set. This flag is deprecated for Windows 2000 and later and must be zero.
/// </summary>
IMAGE_FILE_AGGRESSIVE_WS_TRIM = 0x0010,
/// <summary>
/// Application can handle > 2-GB addresses.
/// </summary>
IMAGE_FILE_LARGE_ADDRESS_AWARE = 0x0020,
/// <summary>
/// Machine is based on a 16-bit-word architecture.
/// </summary>
IMAGE_FILE_16BIT_MACHINE = 0x0040,
/// <summary>
/// Little endian: the least significant bit (LSB) precedes the most significant bit (MSB) in memory. This flag is deprecated and should be zero.
/// </summary>
IMAGE_FILE_BYTES_REVERSED_LO = 0x0080,
/// <summary>
/// Machine is based on a 32-bit-word architecture.
/// </summary>
IMAGE_FILE_32BIT_MACHINE = 0x0100,
/// <summary>
/// Debugging information is removed from the image file.
/// </summary>
IMAGE_FILE_DEBUG_STRIPPED = 0x0200,
/// <summary>
/// If the image is on removable media, fully load it and copy it to the swap file.
/// </summary>
IMAGE_FILE_REMOVABLE_RUN_FROM_SWAP = 0x0400,
/// <summary>
/// If the image is on network media, fully load it and copy it to the swap file.
/// </summary>
IMAGE_FILE_NET_RUN_FROM_SWAP = 0x0800,
/// <summary>
/// The image file is a system file, not a user program.
/// </summary>
IMAGE_FILE_SYSTEM = 0x1000,
/// <summary>
/// The image file is a dynamic-link library (DLL). Such files are considered executable files for almost all purposes, although they cannot be directly run.
/// </summary>
IMAGE_FILE_DLL = 0x2000,
/// <summary>
/// The file should be run only on a uniprocessor machine.
/// </summary>
IMAGE_FILE_UP_SYSTEM_ONLY = 0x4000,
/// <summary>
/// Big endian: the MSB precedes the LSB in memory. This flag is deprecated and should be zero.
/// </summary>
IMAGE_FILE_BYTES_REVERSED_HI = 0x8000
/// <summary>
/// Image only, Windows CE, and Microsoft Windows NT and later. This indicates that the file does not contain base
/// relocations and must therefore be loaded at its preferred base address. If the base address is not available, the
/// loader reports an error. The default behavior of the linker is to strip base relocations from executable (EXE)
/// files.
/// </summary>
IMAGE_FILE_RELOCS_STRIPPED = 0x0001,
/// <summary>
/// Image only. This indicates that the image file is valid and can be run. If this flag is not set, it indicates a
/// linker error.
/// </summary>
IMAGE_FILE_EXECUTABLE_IMAGE = 0x0002,
/// <summary>
/// COFF line numbers have been removed. This flag is deprecated and should be zero.
/// </summary>
IMAGE_FILE_LINE_NUMS_STRIPPED = 0x0004,
/// <summary>
/// COFF symbol table entries for local symbols have been removed. This flag is deprecated and should be zero.
/// </summary>
IMAGE_FILE_LOCAL_SYMS_STRIPPED = 0x0008,
/// <summary>
/// Obsolete. Aggressively trim working set. This flag is deprecated for Windows 2000 and later and must be zero.
/// </summary>
IMAGE_FILE_AGGRESSIVE_WS_TRIM = 0x0010,
/// <summary>
/// Application can handle > 2-GB addresses.
/// </summary>
IMAGE_FILE_LARGE_ADDRESS_AWARE = 0x0020,
/// <summary>
/// Machine is based on a 16-bit-word architecture.
/// </summary>
IMAGE_FILE_16BIT_MACHINE = 0x0040,
/// <summary>
/// Little endian: the least significant bit (LSB) precedes the most significant bit (MSB) in memory. This flag is
/// deprecated and should be zero.
/// </summary>
IMAGE_FILE_BYTES_REVERSED_LO = 0x0080,
/// <summary>
/// Machine is based on a 32-bit-word architecture.
/// </summary>
IMAGE_FILE_32BIT_MACHINE = 0x0100,
/// <summary>
/// Debugging information is removed from the image file.
/// </summary>
IMAGE_FILE_DEBUG_STRIPPED = 0x0200,
/// <summary>
/// If the image is on removable media, fully load it and copy it to the swap file.
/// </summary>
IMAGE_FILE_REMOVABLE_RUN_FROM_SWAP = 0x0400,
/// <summary>
/// If the image is on network media, fully load it and copy it to the swap file.
/// </summary>
IMAGE_FILE_NET_RUN_FROM_SWAP = 0x0800,
/// <summary>
/// The image file is a system file, not a user program.
/// </summary>
IMAGE_FILE_SYSTEM = 0x1000,
/// <summary>
/// The image file is a dynamic-link library (DLL). Such files are considered executable files for almost all purposes,
/// although they cannot be directly run.
/// </summary>
IMAGE_FILE_DLL = 0x2000,
/// <summary>
/// The file should be run only on a uniprocessor machine.
/// </summary>
IMAGE_FILE_UP_SYSTEM_ONLY = 0x4000,
/// <summary>
/// Big endian: the MSB precedes the LSB in memory. This flag is deprecated and should be zero.
/// </summary>
IMAGE_FILE_BYTES_REVERSED_HI = 0x8000
}
/// <summary>
/// The Machine field has one of the following values that specifies its CPU type. An image file can be run only on the
/// specified machine or on a system that emulates the specified machine.
/// </summary>
public enum MachineTypes : ushort
{
/// <summary>
/// The contents of this field are assumed to be applicable to any machine type
/// </summary>
IMAGE_FILE_MACHINE_UNKNOWN = 0x0,
/// <summary>
/// Alpha AXP
/// </summary>
IMAGE_FILE_MACHINE_ALPHA = 0x184,
/// <summary>
/// Alpha AXP 64-bit.
/// </summary>
IMAGE_FILE_MACHINE_ALPHA64 = 0x284,
/// <summary>
/// Matsushita AM33
/// </summary>
IMAGE_FILE_MACHINE_AM33 = 0x1d3,
/// <summary>
/// x64
/// </summary>
IMAGE_FILE_MACHINE_AMD64 = 0x8664,
/// <summary>
/// ARM little endian
/// </summary>
IMAGE_FILE_MACHINE_ARM = 0x1c0,
/// <summary>
/// ARM64 little endian
/// </summary>
IMAGE_FILE_MACHINE_ARM64 = 0xaa64,
/// <summary>
/// ARM Thumb-2 little endian
/// </summary>
IMAGE_FILE_MACHINE_ARMNT = 0x1c4,
/// <summary>
/// Clipper
/// </summary>
IMAGE_FILE_MACHINE_CLIPPER = 0x17f,
/// <summary>
/// EFI byte code
/// </summary>
IMAGE_FILE_MACHINE_EBC = 0xebc,
/// <summary>
/// Intel 386 or later processors and compatible processors
/// </summary>
IMAGE_FILE_MACHINE_I386 = 0x14c,
/// <summary>
/// Intel 386 or later processors and compatible processors, used by AIX
/// </summary>
IMAGE_FILE_MACHINE_I386_AIX = 0x175,
/// <summary>
/// Intel Itanium processor family
/// </summary>
IMAGE_FILE_MACHINE_IA64 = 0x200,
/// <summary>
/// Mitsubishi M32R little endian
/// </summary>
IMAGE_FILE_MACHINE_M32R = 0x9041,
/// <summary>
/// Motorola 68000 series
/// </summary>
IMAGE_FILE_MACHINE_M68K = 0x268,
/// <summary>
/// Motorola 68000 series
/// </summary>
IMAGE_FILE_MACHINE_M68K_OTHER = 0x150,
/// <summary>
/// MIPS16
/// </summary>
IMAGE_FILE_MACHINE_MIPS16 = 0x266,
/// <summary>
/// MIPS with FPU
/// </summary>
IMAGE_FILE_MACHINE_MIPSFPU = 0x366,
/// <summary>
/// MIPS16 with FPU
/// </summary>
IMAGE_FILE_MACHINE_MIPSFPU16 = 0x466,
/// <summary>
/// PowerPC little endian
/// </summary>
IMAGE_FILE_MACHINE_POWERPC = 0x1f0,
/// <summary>
/// Power PC with floating point support
/// </summary>
IMAGE_FILE_MACHINE_POWERPCFP = 0x1f1,
/// <summary>
/// MIPS big endian
/// </summary>
IMAGE_FILE_MACHINE_MIPSEB = 0x160,
/// <summary>
/// MIPS little endian
/// </summary>
IMAGE_FILE_MACHINE_R3000 = 0x162,
/// <summary>
/// MIPS little endian
/// </summary>
IMAGE_FILE_MACHINE_R4000 = 0x166,
/// <summary>
/// MIPS little endian
/// </summary>
IMAGE_FILE_MACHINE_R10000 = 0x168,
/// <summary>
/// RISC-V 32-bit address space
/// </summary>
IMAGE_FILE_MACHINE_RISCV32 = 0x5032,
/// <summary>
/// RISC-V 64-bit address space
/// </summary>
IMAGE_FILE_MACHINE_RISCV64 = 0x5064,
/// <summary>
/// RISC-V 128-bit address space
/// </summary>
IMAGE_FILE_MACHINE_RISCV128 = 0x5128,
/// <summary>
/// Hitachi SH3
/// </summary>
IMAGE_FILE_MACHINE_SH3 = 0x1a2,
/// <summary>
/// Hitachi SH3 DSP
/// </summary>
IMAGE_FILE_MACHINE_SH3DSP = 0x1a3,
/// <summary>
/// Hitachi SH4
/// </summary>
IMAGE_FILE_MACHINE_SH4 = 0x1a6,
/// <summary>
/// Hitachi SH5
/// </summary>
IMAGE_FILE_MACHINE_SH5 = 0x1a8,
/// <summary>
/// Thumb
/// </summary>
IMAGE_FILE_MACHINE_THUMB = 0x1c2,
/// <summary>
/// MIPS little-endian WCE v2
/// </summary>
IMAGE_FILE_MACHINE_WCEMIPSV2 = 0x169,
/// <summary>
/// WE32000
/// </summary>
IMAGE_FILE_MACHINE_WE32000 = 0x170
}
public const uint IMAGE_SCN_ALIGN_MASK = 0x00F00000;
[Flags]
public enum SectionFlags : uint
{
/// <summary>
/// Reserved for future use.
/// </summary>
IMAGE_SCN_TYPE_DSECT=0x00000001,
/// <summary>
/// Reserved for future use.
/// </summary>
IMAGE_SCN_TYPE_NOLOAD=0x00000002,
/// <summary>
/// Reserved for future use.
/// </summary>
IMAGE_SCN_TYPE_GROUP=0x00000004,
/// <summary>
/// Section should not be padded to next boundary.
/// This is obsolete and replaced by <see cref="IMAGE_SCN_ALIGN_1BYTES"/>.
/// This is valid for object files only.
/// </summary>
IMAGE_SCN_TYPE_NO_PAD=0x00000008,
/// <summary>
/// Reserved for future use.
/// </summary>
IMAGE_SCN_TYPE_COPY=0x00000010,
/// <summary>
/// Section contains executable code.
/// </summary>
IMAGE_SCN_CNT_CODE=0x00000020,
/// <summary>
/// Section contains initialized data.
/// </summary>
IMAGE_SCN_CNT_INITIALIZED_DATA=0x00000040,
/// <summary>
/// Section contains uninitialized data.
/// </summary>
IMAGE_SCN_CNT_UNINITIALIZED_DATA=0x00000080,
/// <summary>
/// Reserved for future use.
/// </summary>
IMAGE_SCN_LNK_OTHER=0x00000100,
/// <summary>
/// Section contains comments or other information. The .drectve section has this type. This is valid for object files only.
/// </summary>
IMAGE_SCN_LNK_INFO=0x00000200,
/// <summary>
/// Reserved for future use.
/// </summary>
IMAGE_SCN_TYPE_OVER=0x00000400,
/// <summary>
/// Section will not become part of the image. This is valid for object files only.
/// </summary>
IMAGE_SCN_LNK_REMOVE=0x00000800,
/// <summary>
/// Section contains COMDAT data. This is valid for object files only.
/// </summary>
IMAGE_SCN_LNK_COMDAT=0x00001000,
/// <summary>
/// Reserved for future use.
/// </summary>
IMAGE_SCN_MEM_FARDATA=0x00008000,
/// <summary>
/// Reserved for future use.
/// </summary>
IMAGE_SCN_MEM_16BIT =0x00020000,
/// <summary>
/// Reserved for future use.
/// </summary>
IMAGE_SCN_MEM_LOCKED=0x00040000,
/// <summary>
/// Reserved for future use.
/// </summary>
IMAGE_SCN_MEM_PRELOAD=0x00080000,
/// <summary>
/// Align data on a 1-byte boundary. This is valid for object files only.
/// </summary>
IMAGE_SCN_ALIGN_1BYTES=0x00100000,
/// <summary>
/// Align data on a 2-byte boundary. This is valid for object files only.
/// </summary>
IMAGE_SCN_ALIGN_2BYTES=0x00200000,
/// <summary>
/// Align data on a 4-byte boundary. This is valid for object files only.
/// </summary>
IMAGE_SCN_ALIGN_4BYTES=0x00300000,
/// <summary>
/// Align data on a 8-byte boundary. This is valid for object files only.
/// </summary>
IMAGE_SCN_ALIGN_8BYTES=0x00400000,
/// <summary>
/// Align data on a 16-byte boundary. This is valid for object files only.
/// </summary>
IMAGE_SCN_ALIGN_16BYTES=0x00500000,
/// <summary>
/// Align data on a 32-byte boundary. This is valid for object files only.
/// </summary>
IMAGE_SCN_ALIGN_32BYTES=0x00600000,
/// <summary>
/// Align data on a 64-byte boundary. This is valid for object files only.
/// </summary>
IMAGE_SCN_ALIGN_64BYTES=0x00700000,
/// <summary>
/// Align data on a 128-byte boundary. This is valid for object files only.
/// </summary>
IMAGE_SCN_ALIGN_128BYTES=0x00800000,
/// <summary>
/// Align data on a 256-byte boundary. This is valid for object files only.
/// </summary>
IMAGE_SCN_ALIGN_256BYTES=0x00900000,
/// <summary>
/// Align data on a 512-byte boundary. This is valid for object files only.
/// </summary>
IMAGE_SCN_ALIGN_512BYTES=0x00A00000,
/// <summary>
/// Align data on a 1024-byte boundary. This is valid for object files only.
/// </summary>
IMAGE_SCN_ALIGN_1024BYTES=0x00B00000,
/// <summary>
/// Align data on a 2048-byte boundary. This is valid for object files only.
/// </summary>
IMAGE_SCN_ALIGN_2048BYTES=0x00C00000,
/// <summary>
/// Align data on a 4096-byte boundary. This is valid for object files only.
/// </summary>
IMAGE_SCN_ALIGN_4096BYTES=0x00D00000,
/// <summary>
/// Align data on a 8192-byte boundary. This is valid for object files only.
/// </summary>
IMAGE_SCN_ALIGN_8192BYTES=0x00E00000,
/// <summary>
/// Section contains extended relocations.
/// </summary>
IMAGE_SCN_LNK_NRELOC_OVFL=0x01000000,
/// <summary>
/// Section can be discarded as needed.
/// </summary>
IMAGE_SCN_MEM_DISCARDABLE=0x02000000,
/// <summary>
/// Section cannot be cached.
/// </summary>
IMAGE_SCN_MEM_NOT_CACHED=0x04000000,
/// <summary>
/// Section is not pageable.
/// </summary>
IMAGE_SCN_MEM_NOT_PAGED=0x08000000,
/// <summary>
/// Section can be shared in memory.
/// </summary>
IMAGE_SCN_MEM_SHARED=0x10000000,
/// <summary>
/// Section can be executed as code.
/// </summary>
IMAGE_SCN_MEM_EXECUTE=0x20000000,
/// <summary>
/// Section can be read.
/// </summary>
IMAGE_SCN_MEM_READ=0x40000000,
/// <summary>
/// Section can be written to.
/// </summary>
IMAGE_SCN_MEM_WRITE=0x80000000
/// <summary>
/// Reserved for future use.
/// </summary>
IMAGE_SCN_TYPE_DSECT = 0x00000001,
/// <summary>
/// Reserved for future use.
/// </summary>
IMAGE_SCN_TYPE_NOLOAD = 0x00000002,
/// <summary>
/// Reserved for future use.
/// </summary>
IMAGE_SCN_TYPE_GROUP = 0x00000004,
/// <summary>
/// Section should not be padded to next boundary.
/// This is obsolete and replaced by <see cref="IMAGE_SCN_ALIGN_1BYTES" />.
/// This is valid for object files only.
/// </summary>
IMAGE_SCN_TYPE_NO_PAD = 0x00000008,
/// <summary>
/// Reserved for future use.
/// </summary>
IMAGE_SCN_TYPE_COPY = 0x00000010,
/// <summary>
/// Section contains executable code.
/// </summary>
IMAGE_SCN_CNT_CODE = 0x00000020,
/// <summary>
/// Section contains initialized data.
/// </summary>
IMAGE_SCN_CNT_INITIALIZED_DATA = 0x00000040,
/// <summary>
/// Section contains uninitialized data.
/// </summary>
IMAGE_SCN_CNT_UNINITIALIZED_DATA = 0x00000080,
/// <summary>
/// Reserved for future use.
/// </summary>
IMAGE_SCN_LNK_OTHER = 0x00000100,
/// <summary>
/// Section contains comments or other information. The .drectve section has this type. This is valid for object files
/// only.
/// </summary>
IMAGE_SCN_LNK_INFO = 0x00000200,
/// <summary>
/// Reserved for future use.
/// </summary>
IMAGE_SCN_TYPE_OVER = 0x00000400,
/// <summary>
/// Section will not become part of the image. This is valid for object files only.
/// </summary>
IMAGE_SCN_LNK_REMOVE = 0x00000800,
/// <summary>
/// Section contains COMDAT data. This is valid for object files only.
/// </summary>
IMAGE_SCN_LNK_COMDAT = 0x00001000,
/// <summary>
/// Reserved for future use.
/// </summary>
IMAGE_SCN_MEM_FARDATA = 0x00008000,
/// <summary>
/// Reserved for future use.
/// </summary>
IMAGE_SCN_MEM_16BIT = 0x00020000,
/// <summary>
/// Reserved for future use.
/// </summary>
IMAGE_SCN_MEM_LOCKED = 0x00040000,
/// <summary>
/// Reserved for future use.
/// </summary>
IMAGE_SCN_MEM_PRELOAD = 0x00080000,
/// <summary>
/// Align data on a 1-byte boundary. This is valid for object files only.
/// </summary>
IMAGE_SCN_ALIGN_1BYTES = 0x00100000,
/// <summary>
/// Align data on a 2-byte boundary. This is valid for object files only.
/// </summary>
IMAGE_SCN_ALIGN_2BYTES = 0x00200000,
/// <summary>
/// Align data on a 4-byte boundary. This is valid for object files only.
/// </summary>
IMAGE_SCN_ALIGN_4BYTES = 0x00300000,
/// <summary>
/// Align data on a 8-byte boundary. This is valid for object files only.
/// </summary>
IMAGE_SCN_ALIGN_8BYTES = 0x00400000,
/// <summary>
/// Align data on a 16-byte boundary. This is valid for object files only.
/// </summary>
IMAGE_SCN_ALIGN_16BYTES = 0x00500000,
/// <summary>
/// Align data on a 32-byte boundary. This is valid for object files only.
/// </summary>
IMAGE_SCN_ALIGN_32BYTES = 0x00600000,
/// <summary>
/// Align data on a 64-byte boundary. This is valid for object files only.
/// </summary>
IMAGE_SCN_ALIGN_64BYTES = 0x00700000,
/// <summary>
/// Align data on a 128-byte boundary. This is valid for object files only.
/// </summary>
IMAGE_SCN_ALIGN_128BYTES = 0x00800000,
/// <summary>
/// Align data on a 256-byte boundary. This is valid for object files only.
/// </summary>
IMAGE_SCN_ALIGN_256BYTES = 0x00900000,
/// <summary>
/// Align data on a 512-byte boundary. This is valid for object files only.
/// </summary>
IMAGE_SCN_ALIGN_512BYTES = 0x00A00000,
/// <summary>
/// Align data on a 1024-byte boundary. This is valid for object files only.
/// </summary>
IMAGE_SCN_ALIGN_1024BYTES = 0x00B00000,
/// <summary>
/// Align data on a 2048-byte boundary. This is valid for object files only.
/// </summary>
IMAGE_SCN_ALIGN_2048BYTES = 0x00C00000,
/// <summary>
/// Align data on a 4096-byte boundary. This is valid for object files only.
/// </summary>
IMAGE_SCN_ALIGN_4096BYTES = 0x00D00000,
/// <summary>
/// Align data on a 8192-byte boundary. This is valid for object files only.
/// </summary>
IMAGE_SCN_ALIGN_8192BYTES = 0x00E00000,
/// <summary>
/// Section contains extended relocations.
/// </summary>
IMAGE_SCN_LNK_NRELOC_OVFL = 0x01000000,
/// <summary>
/// Section can be discarded as needed.
/// </summary>
IMAGE_SCN_MEM_DISCARDABLE = 0x02000000,
/// <summary>
/// Section cannot be cached.
/// </summary>
IMAGE_SCN_MEM_NOT_CACHED = 0x04000000,
/// <summary>
/// Section is not pageable.
/// </summary>
IMAGE_SCN_MEM_NOT_PAGED = 0x08000000,
/// <summary>
/// Section can be shared in memory.
/// </summary>
IMAGE_SCN_MEM_SHARED = 0x10000000,
/// <summary>
/// Section can be executed as code.
/// </summary>
IMAGE_SCN_MEM_EXECUTE = 0x20000000,
/// <summary>
/// Section can be read.
/// </summary>
IMAGE_SCN_MEM_READ = 0x40000000,
/// <summary>
/// Section can be written to.
/// </summary>
IMAGE_SCN_MEM_WRITE = 0x80000000
}
public const uint IMAGE_SCN_ALIGN_MASK = 0x00F00000;
}
}

View File

@@ -25,97 +25,99 @@
// THE SOFTWARE.
using System;
using System.Collections.Generic;
using System.IO;
using System.Text;
namespace libexeinfo
{
public partial class COFF
{
public partial class COFF
{
public static string GetInfo(COFFHeader header)
{
DateTime epoch = new DateTime(1970, 1, 1);
StringBuilder sb = new StringBuilder();
sb.AppendLine("Common Object File Format (COFF):");
switch (header.optionalHeader.magic)
{
case JMAGIC:
sb.AppendLine("\tExecutable is dirty and unshareable");
break;
case DMAGIC:
sb.AppendLine("\tExecutable is dirty, data is aligned");
break;
case ZMAGIC:
sb.AppendLine("\tNormal executable");
break;
case SHMAGIC:
sb.AppendLine("\tShared library");
break;
{
DateTime epoch = new DateTime(1970, 1, 1);
StringBuilder sb = new StringBuilder();
sb.AppendLine("Common Object File Format (COFF):");
switch(header.optionalHeader.magic)
{
case JMAGIC:
sb.AppendLine("\tExecutable is dirty and unshareable");
break;
case DMAGIC:
sb.AppendLine("\tExecutable is dirty, data is aligned");
break;
case ZMAGIC:
sb.AppendLine("\tNormal executable");
break;
case SHMAGIC:
sb.AppendLine("\tShared library");
break;
case PE.PE32Plus:
sb.AppendLine("\tPE32+ executable");
break;
default:
sb.AppendFormat("\tUnknown executable type with magic {0}", header.optionalHeader.magic).AppendLine();
break;
}
sb.AppendLine("\tPE32+ executable");
break;
default:
sb.AppendFormat("\tUnknown executable type with magic {0}", header.optionalHeader.magic)
.AppendLine();
break;
}
sb.AppendFormat("\tMachine: {0}", MachineTypeToString(header.machine)).AppendLine();
if (header.characteristics.HasFlag(Characteristics.IMAGE_FILE_RELOCS_STRIPPED))
sb.AppendLine("\tExecutable contains no relocations.");
if (header.characteristics.HasFlag(Characteristics.IMAGE_FILE_EXECUTABLE_IMAGE))
sb.AppendLine("\tExecutable is valid.");
else
sb.AppendLine("\tExecutable is invalid, contains errors or has not been linked correctly.");
if (!header.characteristics.HasFlag(Characteristics.IMAGE_FILE_LINE_NUMS_STRIPPED))
sb.AppendLine("\tExecutable contains line numbers.");
if (!header.characteristics.HasFlag(Characteristics.IMAGE_FILE_LOCAL_SYMS_STRIPPED))
sb.AppendLine("\tExecutable contains debug symbols.");
if (header.characteristics.HasFlag(Characteristics.IMAGE_FILE_AGGRESSIVE_WS_TRIM))
sb.AppendLine("\tWorking set should be aggressively trimmed");
if (header.characteristics.HasFlag(Characteristics.IMAGE_FILE_LARGE_ADDRESS_AWARE))
sb.AppendLine("\tExecutable can handle addresses bigger than 2GiB");
if (header.characteristics.HasFlag(Characteristics.IMAGE_FILE_16BIT_MACHINE))
sb.AppendLine("\tExecutable is for a 16-bit per word machine");
if (header.characteristics.HasFlag(Characteristics.IMAGE_FILE_BYTES_REVERSED_LO))
sb.AppendLine("\tExecutable is little-endian");
if (header.characteristics.HasFlag(Characteristics.IMAGE_FILE_32BIT_MACHINE))
sb.AppendLine("\tExecutable is for a 32-bit per word machine");
if (header.characteristics.HasFlag(Characteristics.IMAGE_FILE_DEBUG_STRIPPED))
sb.AppendLine("\tDebug information has been removed");
if (header.characteristics.HasFlag(Characteristics.IMAGE_FILE_REMOVABLE_RUN_FROM_SWAP))
sb.AppendLine("\tWhen executable is run from removable media, it should be copied to swap and run from there");
if (header.characteristics.HasFlag(Characteristics.IMAGE_FILE_NET_RUN_FROM_SWAP))
sb.AppendLine("\tWhen executable is run from a network share, it should be copied to swap and run from there");
if (header.characteristics.HasFlag(Characteristics.IMAGE_FILE_SYSTEM))
sb.AppendLine("\tExecutable is a system file");
if (header.characteristics.HasFlag(Characteristics.IMAGE_FILE_DLL))
sb.AppendLine("\tExecutable is a dynamically linkable library");
if (header.characteristics.HasFlag(Characteristics.IMAGE_FILE_UP_SYSTEM_ONLY))
sb.AppendLine("\tExecutable can only run on uniprocessor machines");
if (header.characteristics.HasFlag(Characteristics.IMAGE_FILE_BYTES_REVERSED_HI))
sb.AppendLine("\tExecutable is big-endian");
if(header.characteristics.HasFlag(Characteristics.IMAGE_FILE_RELOCS_STRIPPED))
sb.AppendLine("\tExecutable contains no relocations.");
if(header.characteristics.HasFlag(Characteristics.IMAGE_FILE_EXECUTABLE_IMAGE))
sb.AppendLine("\tExecutable is valid.");
else sb.AppendLine("\tExecutable is invalid, contains errors or has not been linked correctly.");
if(!header.characteristics.HasFlag(Characteristics.IMAGE_FILE_LINE_NUMS_STRIPPED))
sb.AppendLine("\tExecutable contains line numbers.");
if(!header.characteristics.HasFlag(Characteristics.IMAGE_FILE_LOCAL_SYMS_STRIPPED))
sb.AppendLine("\tExecutable contains debug symbols.");
if(header.characteristics.HasFlag(Characteristics.IMAGE_FILE_AGGRESSIVE_WS_TRIM))
sb.AppendLine("\tWorking set should be aggressively trimmed");
if(header.characteristics.HasFlag(Characteristics.IMAGE_FILE_LARGE_ADDRESS_AWARE))
sb.AppendLine("\tExecutable can handle addresses bigger than 2GiB");
if(header.characteristics.HasFlag(Characteristics.IMAGE_FILE_16BIT_MACHINE))
sb.AppendLine("\tExecutable is for a 16-bit per word machine");
if(header.characteristics.HasFlag(Characteristics.IMAGE_FILE_BYTES_REVERSED_LO))
sb.AppendLine("\tExecutable is little-endian");
if(header.characteristics.HasFlag(Characteristics.IMAGE_FILE_32BIT_MACHINE))
sb.AppendLine("\tExecutable is for a 32-bit per word machine");
if(header.characteristics.HasFlag(Characteristics.IMAGE_FILE_DEBUG_STRIPPED))
sb.AppendLine("\tDebug information has been removed");
if(header.characteristics.HasFlag(Characteristics.IMAGE_FILE_REMOVABLE_RUN_FROM_SWAP))
sb.AppendLine("\tWhen executable is run from removable media, it should be copied to swap and run from there");
if(header.characteristics.HasFlag(Characteristics.IMAGE_FILE_NET_RUN_FROM_SWAP))
sb.AppendLine("\tWhen executable is run from a network share, it should be copied to swap and run from there");
if(header.characteristics.HasFlag(Characteristics.IMAGE_FILE_SYSTEM))
sb.AppendLine("\tExecutable is a system file");
if(header.characteristics.HasFlag(Characteristics.IMAGE_FILE_DLL))
sb.AppendLine("\tExecutable is a dynamically linkable library");
if(header.characteristics.HasFlag(Characteristics.IMAGE_FILE_UP_SYSTEM_ONLY))
sb.AppendLine("\tExecutable can only run on uniprocessor machines");
if(header.characteristics.HasFlag(Characteristics.IMAGE_FILE_BYTES_REVERSED_HI))
sb.AppendLine("\tExecutable is big-endian");
sb.AppendFormat("\tExecutable has {0} sections", header.numberOfSections).AppendLine();
sb.AppendFormat("\tExecutable has {0} sections", header.numberOfSections).AppendLine();
sb.AppendFormat("\tExecutable was linked on {0}", epoch.AddSeconds(header.timeDateStamp)).AppendLine();
sb.AppendFormat("\tSymbol table starts at {0} and contains {1} symbols", header.pointerToSymbolTable, header.numberOfSymbols).AppendLine();
sb.AppendFormat("\tOptional header has {0} bytes", header.sizeOfOptionalHeader).AppendLine();
sb.AppendFormat("\tSymbol table starts at {0} and contains {1} symbols", header.pointerToSymbolTable,
header.numberOfSymbols).AppendLine();
sb.AppendFormat("\tOptional header has {0} bytes", header.sizeOfOptionalHeader).AppendLine();
if(header.optionalHeader.majorLinkerVersion > 0 || header.optionalHeader.minorLinkerVersion > 0)
sb.AppendFormat("\tLinker version: {0}.{1}", header.optionalHeader.majorLinkerVersion, header.optionalHeader.minorLinkerVersion).AppendLine();
sb.AppendFormat("\tCode has {0} bytes", header.optionalHeader.sizeOfCode).AppendLine();
sb.AppendFormat("\tInitialized data has {0} bytes", header.optionalHeader.sizeOfInitializedData).AppendLine();
sb.AppendFormat("\tUninitialized data has {0} bytes", header.optionalHeader.sizeOfUninitializedData).AppendLine();
sb.AppendFormat("\tAddress point starts at {0}", header.optionalHeader.addressOfEntryPoint).AppendLine();
sb.AppendFormat("\tCode starts at {0}", header.optionalHeader.baseOfCode).AppendLine();
if (header.optionalHeader.magic != PE.PE32Plus)
sb.AppendFormat("\tLinker version: {0}.{1}", header.optionalHeader.majorLinkerVersion,
header.optionalHeader.minorLinkerVersion).AppendLine();
sb.AppendFormat("\tCode has {0} bytes", header.optionalHeader.sizeOfCode).AppendLine();
sb.AppendFormat("\tInitialized data has {0} bytes", header.optionalHeader.sizeOfInitializedData)
.AppendLine();
sb.AppendFormat("\tUninitialized data has {0} bytes", header.optionalHeader.sizeOfUninitializedData)
.AppendLine();
sb.AppendFormat("\tAddress point starts at {0}", header.optionalHeader.addressOfEntryPoint).AppendLine();
sb.AppendFormat("\tCode starts at {0}", header.optionalHeader.baseOfCode).AppendLine();
if(header.optionalHeader.magic != PE.PE32Plus)
sb.AppendFormat("\tData starts at {0}", header.optionalHeader.baseOfData).AppendLine();
return sb.ToString();
}
return sb.ToString();
}
public string GetInfo()
{
return GetInfo(Header);
}
}
}
}

View File

@@ -30,76 +30,44 @@ namespace libexeinfo
{
public static string MachineTypeToString(MachineTypes machine)
{
switch (machine)
switch(machine)
{
case MachineTypes.IMAGE_FILE_MACHINE_UNKNOWN:
return "Any machine";
case MachineTypes.IMAGE_FILE_MACHINE_ALPHA:
return "Alpha AXP";
case MachineTypes.IMAGE_FILE_MACHINE_ALPHA64:
return "Alpha AXP 64-bit.";
case MachineTypes.IMAGE_FILE_MACHINE_AM33:
return "Matsushita AM33";
case MachineTypes.IMAGE_FILE_MACHINE_AMD64:
return "AMD64 / EM64T";
case MachineTypes.IMAGE_FILE_MACHINE_ARM:
return "ARM little endian";
case MachineTypes.IMAGE_FILE_MACHINE_ARM64:
return "ARM64 little endian";
case MachineTypes.IMAGE_FILE_MACHINE_ARMNT:
return "ARM Thumb-2 little endian";
case MachineTypes.IMAGE_FILE_MACHINE_EBC:
return "EFI byte code";
case MachineTypes.IMAGE_FILE_MACHINE_UNKNOWN: return "Any machine";
case MachineTypes.IMAGE_FILE_MACHINE_ALPHA: return "Alpha AXP";
case MachineTypes.IMAGE_FILE_MACHINE_ALPHA64: return "Alpha AXP 64-bit.";
case MachineTypes.IMAGE_FILE_MACHINE_AM33: return "Matsushita AM33";
case MachineTypes.IMAGE_FILE_MACHINE_AMD64: return "AMD64 / EM64T";
case MachineTypes.IMAGE_FILE_MACHINE_ARM: return "ARM little endian";
case MachineTypes.IMAGE_FILE_MACHINE_ARM64: return "ARM64 little endian";
case MachineTypes.IMAGE_FILE_MACHINE_ARMNT: return "ARM Thumb-2 little endian";
case MachineTypes.IMAGE_FILE_MACHINE_EBC: return "EFI byte code";
case MachineTypes.IMAGE_FILE_MACHINE_I386:
case MachineTypes.IMAGE_FILE_MACHINE_I386_AIX:
return "Intel 386 or later processors and compatible processors";
case MachineTypes.IMAGE_FILE_MACHINE_IA64:
return "Intel Itanium processor family";
case MachineTypes.IMAGE_FILE_MACHINE_M32R:
return "Mitsubishi M32R little endian";
case MachineTypes.IMAGE_FILE_MACHINE_IA64: return "Intel Itanium processor family";
case MachineTypes.IMAGE_FILE_MACHINE_M32R: return "Mitsubishi M32R little endian";
case MachineTypes.IMAGE_FILE_MACHINE_M68K:
case MachineTypes.IMAGE_FILE_MACHINE_M68K_OTHER:
return "Motorola 68000 series";
case MachineTypes.IMAGE_FILE_MACHINE_MIPS16:
return "MIPS16";
case MachineTypes.IMAGE_FILE_MACHINE_MIPSFPU:
return "MIPS with FPU";
case MachineTypes.IMAGE_FILE_MACHINE_MIPSFPU16:
return "MIPS16 with FPU";
case MachineTypes.IMAGE_FILE_MACHINE_POWERPC:
return "PowerPC little endian";
case MachineTypes.IMAGE_FILE_MACHINE_POWERPCFP:
return "PowerPC with floating point support";
case MachineTypes.IMAGE_FILE_MACHINE_MIPSEB:
return "MIPS R3000 or later (big endian)";
case MachineTypes.IMAGE_FILE_MACHINE_R3000:
return "MIPS R3000 or later (little endian)";
case MachineTypes.IMAGE_FILE_MACHINE_R4000:
return "MIPS R4000 or later (little endian)";
case MachineTypes.IMAGE_FILE_MACHINE_R10000:
return "MIPS R10000 or later (little endian)";
case MachineTypes.IMAGE_FILE_MACHINE_RISCV32:
return "RISC-V 32-bit address space";
case MachineTypes.IMAGE_FILE_MACHINE_RISCV64:
return "RISC-V 64-bit address space";
case MachineTypes.IMAGE_FILE_MACHINE_RISCV128:
return "RISC-V 128-bit address space";
case MachineTypes.IMAGE_FILE_MACHINE_SH3:
return "Hitachi SH3";
case MachineTypes.IMAGE_FILE_MACHINE_SH3DSP:
return "Hitachi SH3 DSP";
case MachineTypes.IMAGE_FILE_MACHINE_SH4:
return "Hitachi SH4";
case MachineTypes.IMAGE_FILE_MACHINE_SH5:
return "Hitachi SH5";
case MachineTypes.IMAGE_FILE_MACHINE_THUMB:
return "ARM Thumb";
case MachineTypes.IMAGE_FILE_MACHINE_WCEMIPSV2:
return "MIPS little-endian WCE v2";
case MachineTypes.IMAGE_FILE_MACHINE_CLIPPER:
return "Clipper";
case MachineTypes.IMAGE_FILE_MACHINE_WE32000:
return "WE32000 series";
case MachineTypes.IMAGE_FILE_MACHINE_M68K_OTHER: return "Motorola 68000 series";
case MachineTypes.IMAGE_FILE_MACHINE_MIPS16: return "MIPS16";
case MachineTypes.IMAGE_FILE_MACHINE_MIPSFPU: return "MIPS with FPU";
case MachineTypes.IMAGE_FILE_MACHINE_MIPSFPU16: return "MIPS16 with FPU";
case MachineTypes.IMAGE_FILE_MACHINE_POWERPC: return "PowerPC little endian";
case MachineTypes.IMAGE_FILE_MACHINE_POWERPCFP: return "PowerPC with floating point support";
case MachineTypes.IMAGE_FILE_MACHINE_MIPSEB: return "MIPS R3000 or later (big endian)";
case MachineTypes.IMAGE_FILE_MACHINE_R3000: return "MIPS R3000 or later (little endian)";
case MachineTypes.IMAGE_FILE_MACHINE_R4000: return "MIPS R4000 or later (little endian)";
case MachineTypes.IMAGE_FILE_MACHINE_R10000: return "MIPS R10000 or later (little endian)";
case MachineTypes.IMAGE_FILE_MACHINE_RISCV32: return "RISC-V 32-bit address space";
case MachineTypes.IMAGE_FILE_MACHINE_RISCV64: return "RISC-V 64-bit address space";
case MachineTypes.IMAGE_FILE_MACHINE_RISCV128: return "RISC-V 128-bit address space";
case MachineTypes.IMAGE_FILE_MACHINE_SH3: return "Hitachi SH3";
case MachineTypes.IMAGE_FILE_MACHINE_SH3DSP: return "Hitachi SH3 DSP";
case MachineTypes.IMAGE_FILE_MACHINE_SH4: return "Hitachi SH4";
case MachineTypes.IMAGE_FILE_MACHINE_SH5: return "Hitachi SH5";
case MachineTypes.IMAGE_FILE_MACHINE_THUMB: return "ARM Thumb";
case MachineTypes.IMAGE_FILE_MACHINE_WCEMIPSV2: return "MIPS little-endian WCE v2";
case MachineTypes.IMAGE_FILE_MACHINE_CLIPPER: return "Clipper";
case MachineTypes.IMAGE_FILE_MACHINE_WE32000: return "WE32000 series";
default:
return string.Format("Unknown machine type with code {0}", (ushort)machine);
}

View File

@@ -24,7 +24,6 @@
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
// THE SOFTWARE.
using System;
using System.Runtime.InteropServices;
namespace libexeinfo
@@ -32,151 +31,161 @@ namespace libexeinfo
// TODO: Tru64 COFF is slightly different
public partial class COFF
{
/// <summary>
/// At the beginning of an object file, or immediately after the signature of an image file, is a standard COFF file header in the following format. Note that the Windows loader limits the number of sections to 96.
/// </summary>
[StructLayout(LayoutKind.Sequential/*, Pack = 2*/)]
public struct COFFHeader
{
/// <summary>
/// The number that identifies the type of target machine. For more information, <see cref="MachineTypes"/>.
/// </summary>
public MachineTypes machine;
/// <summary>
/// The number of sections. This indicates the size of the section table, which immediately follows the headers.
/// </summary>
public ushort numberOfSections;
/// <summary>
/// The low 32 bits of the number of seconds since 00:00 January 1, 1970 (a C run-time time_t value), that indicates when the file was created.
/// </summary>
public uint timeDateStamp;
/// <summary>
/// The file offset of the COFF symbol table, or zero if no COFF symbol table is present. This value should be zero for an image because COFF debugging information is deprecated.
/// </summary>
public uint pointerToSymbolTable;
/// <summary>
/// The number of entries in the symbol table. This data can be used to locate the string table, which immediately follows the symbol table. This value should be zero for an image because COFF debugging information is deprecated.
/// </summary>
public uint numberOfSymbols;
public ushort sizeOfOptionalHeader;
/// <summary>
/// The flags that indicate the attributes of the file. For specific flag values, <see cref="Characteristics"/>
/// </summary>
public Characteristics characteristics;
public OptionalHeader optionalHeader;
}
/// <summary>
/// The first eight fields of the optional header are standard fields that are defined for every implementation of COFF. These fields contain general information that is useful for loading and running an executable file. They are unchanged for the PE32+ format.
/// </summary>
[StructLayout(LayoutKind.Sequential/*, Pack = 2*/)]
public struct OptionalHeader
/// <summary>
/// At the beginning of an object file, or immediately after the signature of an image file, is a standard COFF file
/// header in the following format. Note that the Windows loader limits the number of sections to 96.
/// </summary>
[StructLayout(LayoutKind.Sequential /*, Pack = 2*/)]
public struct COFFHeader
{
/// <summary>
/// The unsigned integer that identifies the state of the image file. The most common number is 0x10B, which identifies it as a normal executable file. 0x107 identifies it as a ROM image, and 0x20B identifies it as a PE32+ executable.
/// </summary>
public ushort magic;
/// <summary>
/// The linker major version number.
/// </summary>
public byte majorLinkerVersion;
/// <summary>
/// The linker minor version number.
/// </summary>
public byte minorLinkerVersion;
/// <summary>
/// The size of the code (text) section, or the sum of all code sections if there are multiple sections.
/// </summary>
public uint sizeOfCode;
/// <summary>
/// The size of the initialized data section, or the sum of all such sections if there are multiple data sections.
/// </summary>
public uint sizeOfInitializedData;
/// <summary>
/// The size of the uninitialized data section (BSS), or the sum of all such sections if there are multiple BSS sections.
/// </summary>
public uint sizeOfUninitializedData;
/// <summary>
/// The address of the entry point relative to the image base when the executable file is loaded into memory. For program images, this is the starting address. For device drivers, this is the address of the initialization function. An entry point is optional for DLLs. When no entry point is present, this field must be zero.
/// </summary>
public uint addressOfEntryPoint;
/// <summary>
/// The address that is relative to the image base of the beginning-of-code section when it is loaded into memory.
/// </summary>
public uint baseOfCode;
/// <summary>
/// PE32 contains this additional field, which is absent in PE32+, following BaseOfCode.<br/>
/// The address that is relative to the image base of the beginning-of-data section when it is loaded into memory.
/// </summary>
public uint baseOfData;
/// <summary>
/// The number that identifies the type of target machine. For more information, <see cref="MachineTypes" />.
/// </summary>
public MachineTypes machine;
/// <summary>
/// The number of sections. This indicates the size of the section table, which immediately follows the headers.
/// </summary>
public ushort numberOfSections;
/// <summary>
/// The low 32 bits of the number of seconds since 00:00 January 1, 1970 (a C run-time time_t value), that indicates
/// when the file was created.
/// </summary>
public uint timeDateStamp;
/// <summary>
/// The file offset of the COFF symbol table, or zero if no COFF symbol table is present. This value should be zero for
/// an image because COFF debugging information is deprecated.
/// </summary>
public uint pointerToSymbolTable;
/// <summary>
/// The number of entries in the symbol table. This data can be used to locate the string table, which immediately
/// follows the symbol table. This value should be zero for an image because COFF debugging information is deprecated.
/// </summary>
public uint numberOfSymbols;
public ushort sizeOfOptionalHeader;
/// <summary>
/// The flags that indicate the attributes of the file. For specific flag values, <see cref="Characteristics" />
/// </summary>
public Characteristics characteristics;
public OptionalHeader optionalHeader;
}
[StructLayout(LayoutKind.Sequential/*, Pack = 2*/)]
/// <summary>
/// The first eight fields of the optional header are standard fields that are defined for every implementation of
/// COFF. These fields contain general information that is useful for loading and running an executable file. They are
/// unchanged for the PE32+ format.
/// </summary>
[StructLayout(LayoutKind.Sequential /*, Pack = 2*/)]
public struct OptionalHeader
{
/// <summary>
/// The unsigned integer that identifies the state of the image file. The most common number is 0x10B, which identifies
/// it as a normal executable file. 0x107 identifies it as a ROM image, and 0x20B identifies it as a PE32+ executable.
/// </summary>
public ushort magic;
/// <summary>
/// The linker major version number.
/// </summary>
public byte majorLinkerVersion;
/// <summary>
/// The linker minor version number.
/// </summary>
public byte minorLinkerVersion;
/// <summary>
/// The size of the code (text) section, or the sum of all code sections if there are multiple sections.
/// </summary>
public uint sizeOfCode;
/// <summary>
/// The size of the initialized data section, or the sum of all such sections if there are multiple data sections.
/// </summary>
public uint sizeOfInitializedData;
/// <summary>
/// The size of the uninitialized data section (BSS), or the sum of all such sections if there are multiple BSS
/// sections.
/// </summary>
public uint sizeOfUninitializedData;
/// <summary>
/// The address of the entry point relative to the image base when the executable file is loaded into memory. For
/// program images, this is the starting address. For device drivers, this is the address of the initialization
/// function. An entry point is optional for DLLs. When no entry point is present, this field must be zero.
/// </summary>
public uint addressOfEntryPoint;
/// <summary>
/// The address that is relative to the image base of the beginning-of-code section when it is loaded into memory.
/// </summary>
public uint baseOfCode;
/// <summary>
/// PE32 contains this additional field, which is absent in PE32+, following BaseOfCode.<br />
/// The address that is relative to the image base of the beginning-of-data section when it is loaded into memory.
/// </summary>
public uint baseOfData;
}
[StructLayout(LayoutKind.Sequential /*, Pack = 2*/)]
public struct SectionHeader
{
/// <summary>
/// An 8-byte, null-padded ASCII string. There is no terminating null if the string is
/// exactly eight characters long. For longer names, this field contains a slash(/)
/// followed by ASCII representation of a decimal number: this number is an offset into
/// the string table.Executable images do not use a string table and do not support
/// section names longer than eight characters. Long names in object files will be
/// truncated if emitted to an executable file.
/// </summary>
[MarshalAs(UnmanagedType.ByValTStr, SizeConst = 8)]
/// <summary>
/// An 8-byte, null-padded ASCII string. There is no terminating null if the string is
/// exactly eight characters long. For longer names, this field contains a slash(/)
/// followed by ASCII representation of a decimal number: this number is an offset into
/// the string table.Executable images do not use a string table and do not support
/// section names longer than eight characters. Long names in object files will be
/// truncated if emitted to an executable file.
/// </summary>
[MarshalAs(UnmanagedType.ByValTStr, SizeConst = 8)]
public string name;
/// <summary>
/// Total size of the section when loaded into memory. If this value is greater than
/// <see cref="sizeOfRawData"/>, the section is zero-padded.This field is valid only
/// for executable images and should be set to 0 for object files.
/// </summary>
public uint virtualSize;
/// <summary>
/// For executable images this is the address of the first byte of the section, when
/// loaded into memory, relative to the image base. For object files, this field is
/// the address of the first byte before relocation is applied; for simplicity,
/// compilers should set this to zero. Otherwise, it is an arbitrary value that is
/// subtracted from offsets during relocation.
/// </summary>
public uint virtualAddress;
/// <summary>
/// Size of the section (object file) or size of the initialized data on disk(image
/// files). For executable image, this must be a multiple of FileAlignment from the
/// optional header.If this is less than <see cref="virtualSize"/> the remainder of
/// the section is zero filled. Because this field is rounded while the
/// <see cref="virtualSize"/> field is not it is possible for this to be greater
/// than <see cref="virtualSize"/> as well.When a section contains only
/// uninitialized data, this field should be 0.
/// </summary>
public uint sizeOfRawData;
/// <summary>
/// File pointer to sectionís first page within the COFF file. For executable images,
/// this must be a multiple of FileAlignment from the optional header. For object
/// files, the value should be aligned on a four-byte boundary for best performance.
/// When a section contains only uninitialized data, this field should be 0.
/// </summary>
public uint pointerToRawData;
/// <summary>
/// File pointer to beginning of relocation entries for the section. Set to 0 for
/// executable images or if there are no relocations.
/// </summary>
public uint pointerToRelocations;
/// <summary>
/// File pointer to beginning of line-number entries for the section.Set to 0 if
/// there are no COFF line numbers.
/// </summary>
public uint pointerToLineNumbers;
/// <summary>
/// Number of relocation entries for the section. Set to 0 for executable images.
/// </summary>
public ushort numberOfRelocations;
/// <summary>
/// Number of line-number entries for the section.
/// </summary>
public ushort numberOfLineNumbers;
/// <summary>
/// Flags describing sectionís characteristics. <see cref="SectionFlags"/>
/// </summary>
public SectionFlags characteristics;
/// <summary>
/// Total size of the section when loaded into memory. If this value is greater than
/// <see cref="sizeOfRawData" />, the section is zero-padded.This field is valid only
/// for executable images and should be set to 0 for object files.
/// </summary>
public uint virtualSize;
/// <summary>
/// For executable images this is the address of the first byte of the section, when
/// loaded into memory, relative to the image base. For object files, this field is
/// the address of the first byte before relocation is applied; for simplicity,
/// compilers should set this to zero. Otherwise, it is an arbitrary value that is
/// subtracted from offsets during relocation.
/// </summary>
public uint virtualAddress;
/// <summary>
/// Size of the section (object file) or size of the initialized data on disk(image
/// files). For executable image, this must be a multiple of FileAlignment from the
/// optional header.If this is less than <see cref="virtualSize" /> the remainder of
/// the section is zero filled. Because this field is rounded while the
/// <see cref="virtualSize" /> field is not it is possible for this to be greater
/// than <see cref="virtualSize" /> as well.When a section contains only
/// uninitialized data, this field should be 0.
/// </summary>
public uint sizeOfRawData;
/// <summary>
/// File pointer to sectionís first page within the COFF file. For executable images,
/// this must be a multiple of FileAlignment from the optional header. For object
/// files, the value should be aligned on a four-byte boundary for best performance.
/// When a section contains only uninitialized data, this field should be 0.
/// </summary>
public uint pointerToRawData;
/// <summary>
/// File pointer to beginning of relocation entries for the section. Set to 0 for
/// executable images or if there are no relocations.
/// </summary>
public uint pointerToRelocations;
/// <summary>
/// File pointer to beginning of line-number entries for the section.Set to 0 if
/// there are no COFF line numbers.
/// </summary>
public uint pointerToLineNumbers;
/// <summary>
/// Number of relocation entries for the section. Set to 0 for executable images.
/// </summary>
public ushort numberOfRelocations;
/// <summary>
/// Number of line-number entries for the section.
/// </summary>
public ushort numberOfLineNumbers;
/// <summary>
/// Flags describing sectionís characteristics. <see cref="SectionFlags" />
/// </summary>
public SectionFlags characteristics;
}
}
}
}

View File

@@ -24,18 +24,17 @@
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
// THE SOFTWARE.
using System;
namespace libexeinfo
{
public partial class LX
{
/// <summary>
/// Linear Executable signature, "LE"
/// </summary>
public const ushort Signature16 = 0x454C;
/// <summary>
/// Linear eXecutable signature, "LX"
/// </summary>
public const ushort Signature = 0x584C;
}
/// <summary>
/// Linear Executable signature, "LE"
/// </summary>
public const ushort Signature16 = 0x454C;
/// <summary>
/// Linear eXecutable signature, "LX"
/// </summary>
public const ushort Signature = 0x584C;
}
}

View File

@@ -25,90 +25,91 @@
// THE SOFTWARE.
using System;
namespace libexeinfo
{
public partial class LX
{
/// <summary>
/// Executable module flags.
/// </summary>
[Flags]
/// <summary>
/// Executable module flags.
/// </summary>
[Flags]
public enum ModuleFlags : uint
{
/// <summary>
/// Per-Process Library Initialization
/// </summary>
PerProcessLibrary = 0x04,
/// <summary>
/// Internal fixups for the module have been applied
/// </summary>
InternalFixups = 0x10,
/// <summary>
/// External fixups for the module have been applied
/// </summary>
ExternalFixups = 0x20,
/// <summary>
/// Incompatible with Presentation Manager
/// </summary>
PMIncompatible = 0x100,
/// <summary>
/// Compatible with Presentation Manager
/// </summary>
PMCompatible = 0x200,
/// <summary>
/// Uses Presentation Manager
/// </summary>
UsesPM = 0x300,
/// <summary>
/// Module is not loadable. Contains errors or is being linked
/// </summary>
NotLoadable = 0x2000,
/// <summary>
/// Library module
/// </summary>
Library = 0x8000,
/// <summary>
/// Protected Memory Library module
/// </summary>
ProtectedMemoryLibrary = 0x18000,
/// <summary>
/// Physical Device Driver module
/// </summary>
PhysicalDeviceDriver = 0x20000,
/// <summary>
/// Virtual Device Driver module
/// </summary>
VirtualDeviceDriver = 0x28000,
/// <summary>
/// Per-process Library Termination
/// </summary>
PerProcessTermination = 0x40000000
}
/// <summary>
/// Per-Process Library Initialization
/// </summary>
PerProcessLibrary = 0x04,
/// <summary>
/// Internal fixups for the module have been applied
/// </summary>
InternalFixups = 0x10,
/// <summary>
/// External fixups for the module have been applied
/// </summary>
ExternalFixups = 0x20,
/// <summary>
/// Incompatible with Presentation Manager
/// </summary>
PMIncompatible = 0x100,
/// <summary>
/// Compatible with Presentation Manager
/// </summary>
PMCompatible = 0x200,
/// <summary>
/// Uses Presentation Manager
/// </summary>
UsesPM = 0x300,
/// <summary>
/// Module is not loadable. Contains errors or is being linked
/// </summary>
NotLoadable = 0x2000,
/// <summary>
/// Library module
/// </summary>
Library = 0x8000,
/// <summary>
/// Protected Memory Library module
/// </summary>
ProtectedMemoryLibrary = 0x18000,
/// <summary>
/// Physical Device Driver module
/// </summary>
PhysicalDeviceDriver = 0x20000,
/// <summary>
/// Virtual Device Driver module
/// </summary>
VirtualDeviceDriver = 0x28000,
/// <summary>
/// Per-process Library Termination
/// </summary>
PerProcessTermination = 0x40000000
}
public enum TargetCpu : ushort
{
Unknown = 0,
i286 = 1,
i386 = 2,
i486 = 3,
i286 = 1,
i386 = 2,
i486 = 3,
Pentium = 4,
i860 = 0x20,
N11 = 0x21,
MIPS1 = 0x40,
MIPS2 = 0x41,
MIPS3 = 0x42
}
i860 = 0x20,
N11 = 0x21,
MIPS1 = 0x40,
MIPS2 = 0x41,
MIPS3 = 0x42
}
/// <summary>
/// Target operating system.
/// </summary>
public enum TargetOS : ushort
/// <summary>
/// Target operating system.
/// </summary>
public enum TargetOS : ushort
{
Unknown = 0,
OS2 = 1,
OS2 = 1,
Windows = 2,
DOS = 3,
Win32 = 4
DOS = 3,
Win32 = 4
}
}
}

View File

@@ -24,158 +24,173 @@
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
// THE SOFTWARE.
using System;
using System.Collections.Generic;
using System.IO;
using System.Text;
namespace libexeinfo
{
public partial class LX
{
public static string GetInfo(LXHeader header)
{
public partial class LX
{
public static string GetInfo(LXHeader header)
{
StringBuilder sb = new StringBuilder();
if(header.signature == Signature16)
sb.AppendLine("Linear Executable (LE):");
if(header.signature == Signature16) sb.AppendLine("Linear Executable (LE):");
else sb.AppendLine("Linear eXecutable (LX):");
if(header.os_type == TargetOS.OS2)
{
sb.AppendLine("\tOS/2 application");
if(header.module_flags.HasFlag(ModuleFlags.PMIncompatible) &&
!header.module_flags.HasFlag(ModuleFlags.PMCompatible))
sb.AppendLine("\tApplication is full screen, unaware of Presentation Manager");
else if(!header.module_flags.HasFlag(ModuleFlags.PMIncompatible) &&
header.module_flags.HasFlag(ModuleFlags.PMCompatible))
sb.AppendLine("\tApplication is aware of Presentation Manager, but doesn't use it");
else if(header.module_flags.HasFlag(ModuleFlags.PMIncompatible) &&
header.module_flags.HasFlag(ModuleFlags.PMCompatible))
sb.AppendLine("\tApplication uses Presentation Manager");
}
else if(header.os_type == TargetOS.Windows || header.os_type == TargetOS.Win32 ||
header.os_type == TargetOS.Unknown)
{
if(header.os_type == TargetOS.Windows || header.os_type == TargetOS.Unknown)
sb.AppendLine("\t16-bit Windows application");
else if(header.os_type == TargetOS.Win32)
sb.AppendLine("\t32-bit Windows application");
if(header.module_flags.HasFlag(ModuleFlags.PMIncompatible) &&
!header.module_flags.HasFlag(ModuleFlags.PMCompatible))
sb.AppendLine("\tApplication is full screen, unaware of Windows");
else if(!header.module_flags.HasFlag(ModuleFlags.PMIncompatible) &&
header.module_flags.HasFlag(ModuleFlags.PMCompatible))
sb.AppendLine("\tApplication is aware of Windows, but doesn't use it");
else if(header.module_flags.HasFlag(ModuleFlags.PMIncompatible) &&
header.module_flags.HasFlag(ModuleFlags.PMCompatible))
sb.AppendLine("\tApplication uses Windows");
}
else if(header.os_type == TargetOS.DOS)
{
sb.AppendLine("\tDOS application");
if(header.module_flags.HasFlag(ModuleFlags.PMIncompatible) &&
!header.module_flags.HasFlag(ModuleFlags.PMCompatible))
sb.AppendLine("\tApplication is full screen, unaware of Windows");
else if(!header.module_flags.HasFlag(ModuleFlags.PMIncompatible) &&
header.module_flags.HasFlag(ModuleFlags.PMCompatible))
sb.AppendLine("\tApplication is aware of Windows, but doesn't use it");
else if(header.module_flags.HasFlag(ModuleFlags.PMIncompatible) &&
header.module_flags.HasFlag(ModuleFlags.PMCompatible))
sb.AppendLine("\tApplication uses Windows");
}
else
sb.AppendLine("Linear eXecutable (LX):");
sb.AppendFormat("\tApplication for unknown OS {0}", (ushort)header.os_type).AppendLine();
if (header.os_type == TargetOS.OS2)
{
sb.AppendLine("\tOS/2 application");
if (header.module_flags.HasFlag(ModuleFlags.PMIncompatible) && !header.module_flags.HasFlag(ModuleFlags.PMCompatible))
sb.AppendLine("\tApplication is full screen, unaware of Presentation Manager");
else if (!header.module_flags.HasFlag(ModuleFlags.PMIncompatible) && header.module_flags.HasFlag(ModuleFlags.PMCompatible))
sb.AppendLine("\tApplication is aware of Presentation Manager, but doesn't use it");
else if (header.module_flags.HasFlag(ModuleFlags.PMIncompatible) && header.module_flags.HasFlag(ModuleFlags.PMCompatible))
sb.AppendLine("\tApplication uses Presentation Manager");
}
else if (header.os_type == TargetOS.Windows || header.os_type == TargetOS.Win32 || header.os_type == TargetOS.Unknown)
{
if (header.os_type == TargetOS.Windows || header.os_type == TargetOS.Unknown)
sb.AppendLine("\t16-bit Windows application");
else if (header.os_type == TargetOS.Win32)
sb.AppendLine("\t32-bit Windows application");
if (header.module_flags.HasFlag(ModuleFlags.PMIncompatible) && !header.module_flags.HasFlag(ModuleFlags.PMCompatible))
sb.AppendLine("\tApplication is full screen, unaware of Windows");
else if (!header.module_flags.HasFlag(ModuleFlags.PMIncompatible) && header.module_flags.HasFlag(ModuleFlags.PMCompatible))
sb.AppendLine("\tApplication is aware of Windows, but doesn't use it");
else if (header.module_flags.HasFlag(ModuleFlags.PMIncompatible) && header.module_flags.HasFlag(ModuleFlags.PMCompatible))
sb.AppendLine("\tApplication uses Windows");
}
else if (header.os_type == TargetOS.DOS)
{
sb.AppendLine("\tDOS application");
if (header.module_flags.HasFlag(ModuleFlags.PMIncompatible) && !header.module_flags.HasFlag(ModuleFlags.PMCompatible))
sb.AppendLine("\tApplication is full screen, unaware of Windows");
else if (!header.module_flags.HasFlag(ModuleFlags.PMIncompatible) && header.module_flags.HasFlag(ModuleFlags.PMCompatible))
sb.AppendLine("\tApplication is aware of Windows, but doesn't use it");
else if (header.module_flags.HasFlag(ModuleFlags.PMIncompatible) && header.module_flags.HasFlag(ModuleFlags.PMCompatible))
sb.AppendLine("\tApplication uses Windows");
}
else
{
sb.AppendFormat("\tApplication for unknown OS {0}", (ushort)header.os_type).AppendLine();
}
sb.AppendFormat("\tByte ordering: {0}", header.byte_order == 1 ? "Big-endian" : "Little-Endian").AppendLine();
sb.AppendFormat("\tWord ordering: {0}", header.word_order == 1 ? "Big-endian" : "Little-Endian").AppendLine();
sb.AppendFormat("\tFormat level: {0}", header.format_level).AppendLine();
sb.AppendFormat("\tExecutable version: {0}", header.module_version).AppendLine();
sb.AppendFormat("\tByte ordering: {0}", header.byte_order == 1 ? "Big-endian" : "Little-Endian")
.AppendLine();
sb.AppendFormat("\tWord ordering: {0}", header.word_order == 1 ? "Big-endian" : "Little-Endian")
.AppendLine();
sb.AppendFormat("\tFormat level: {0}", header.format_level).AppendLine();
sb.AppendFormat("\tExecutable version: {0}", header.module_version).AppendLine();
switch(header.cpu_type)
{
case TargetCpu.i286:
sb.AppendLine("\tExecutable requires at least an 80286 processor to run.");
break;
case TargetCpu.i386:
sb.AppendLine("\tExecutable requires at least an 80386 processor to run.");
break;
case TargetCpu.i486:
sb.AppendLine("\tExecutable requires at least an 80486 processor to run.");
break;
case TargetCpu.i386:
sb.AppendLine("\tExecutable requires at least an 80386 processor to run.");
break;
case TargetCpu.i486:
sb.AppendLine("\tExecutable requires at least an 80486 processor to run.");
break;
case TargetCpu.Pentium:
sb.AppendLine("\tExecutable requires at least a Pentium processor to run.");
break;
sb.AppendLine("\tExecutable requires at least a Pentium processor to run.");
break;
case TargetCpu.i860:
sb.AppendLine("\tExecutable requires at least an Intel 860 processor to run.");
break;
sb.AppendLine("\tExecutable requires at least an Intel 860 processor to run.");
break;
case TargetCpu.N11:
sb.AppendLine("\tExecutable requires at least an Intel N11 processor to run.");
break;
sb.AppendLine("\tExecutable requires at least an Intel N11 processor to run.");
break;
case TargetCpu.MIPS1:
sb.AppendLine("\tExecutable requires at least a MIPS I processor to run.");
break;
case TargetCpu.MIPS2:
sb.AppendLine("\tExecutable requires at least a MIPS II processor to run.");
break;
case TargetCpu.MIPS3:
sb.AppendLine("\tExecutable requires at least a MIPS III processor to run.");
break;
break;
case TargetCpu.MIPS2:
sb.AppendLine("\tExecutable requires at least a MIPS II processor to run.");
break;
case TargetCpu.MIPS3:
sb.AppendLine("\tExecutable requires at least a MIPS III processor to run.");
break;
default:
sb.AppendFormat("\tExecutable requires unknown cpu with type code {0} to run.", (ushort)header.cpu_type).AppendLine();
break;
}
sb.AppendFormat("\tExecutable requires unknown cpu with type code {0} to run.",
(ushort)header.cpu_type).AppendLine();
break;
}
if (header.module_flags.HasFlag(ModuleFlags.PerProcessLibrary))
if(header.module_flags.HasFlag(ModuleFlags.PerProcessLibrary))
sb.AppendLine("Library should be initialized per-process.");
if (header.module_flags.HasFlag(ModuleFlags.PerProcessTermination))
sb.AppendLine("Library should be terminated per-process.");
if (header.module_flags.HasFlag(ModuleFlags.InternalFixups))
sb.AppendLine("Internal fixups have been applied.");
if (header.module_flags.HasFlag(ModuleFlags.ExternalFixups))
sb.AppendLine("External fixups have been applied.");
if (header.module_flags.HasFlag(ModuleFlags.NotLoadable))
sb.AppendLine("Executable is not loadable, it contains errors or is still being linked.");
if(header.module_flags.HasFlag(ModuleFlags.PerProcessTermination))
sb.AppendLine("Library should be terminated per-process.");
if(header.module_flags.HasFlag(ModuleFlags.InternalFixups))
sb.AppendLine("Internal fixups have been applied.");
if(header.module_flags.HasFlag(ModuleFlags.ExternalFixups))
sb.AppendLine("External fixups have been applied.");
if(header.module_flags.HasFlag(ModuleFlags.NotLoadable))
sb.AppendLine("Executable is not loadable, it contains errors or is still being linked.");
if(header.module_flags.HasFlag(ModuleFlags.VirtualDeviceDriver))
sb.AppendLine("Executable is a driver for a virtual device.");
else if (header.module_flags.HasFlag(ModuleFlags.PhysicalDeviceDriver))
sb.AppendLine("Executable is a driver for a physical device.");
else if (header.module_flags.HasFlag(ModuleFlags.ProtectedMemoryLibrary))
sb.AppendLine("Executable is a protected mode library.");
else if (header.module_flags.HasFlag(ModuleFlags.Library))
sb.AppendLine("Executable is a library.");
else if(header.module_flags.HasFlag(ModuleFlags.PhysicalDeviceDriver))
sb.AppendLine("Executable is a driver for a physical device.");
else if(header.module_flags.HasFlag(ModuleFlags.ProtectedMemoryLibrary))
sb.AppendLine("Executable is a protected mode library.");
else if(header.module_flags.HasFlag(ModuleFlags.Library))
sb.AppendLine("Executable is a library.");
sb.AppendFormat("\tThis executable contains {0} pages", header.module_pages_no).AppendLine();
sb.AppendFormat("\tObject number to which the Entry Address is relative: {0}", header.eip_object).AppendLine();
sb.AppendFormat("\tEntry address of module: {0:X8}h", header.eip).AppendLine();
sb.AppendFormat("\tObject number to which the ESP is relative: {0}", header.esp_object).AppendLine();
sb.AppendFormat("\tStarting stack address of module: {0:X8}h", header.esp).AppendLine();
sb.AppendFormat("\tOne page is {0} bytes", header.page_size).AppendLine();
sb.AppendFormat("\tShift left bits for page offsets: {0}", header.page_off_shift).AppendLine();
sb.AppendFormat("\tTotal size of the fixup information: {0}", header.fixup_size).AppendLine();
sb.AppendFormat("\tChecksum for fixup information: 0x{0:X8}", header.fixup_checksum).AppendLine();
sb.AppendFormat("\tMemory resident tables are {0} bytes long", header.loader_size).AppendLine();
sb.AppendFormat("\tChecksum for loader section: 0x{0:X8}", header.loader_checksum).AppendLine();
sb.AppendFormat("\tObject table starts at {0} and contains {1} objects", header.obj_table_off, header.obj_no).AppendLine();
sb.AppendFormat("\tObject page table starts at {0}", header.obj_page_table_off).AppendLine();
sb.AppendFormat("\tObject iterated pages starts at {0}", header.obj_iter_pages_off).AppendLine();
sb.AppendFormat("\tResources table starts at {0} and contains {1} entries", header.resource_table_off, header.resource_entries).AppendLine();
sb.AppendFormat("\tResident name table starts at {0}", header.resident_names_off).AppendLine();
sb.AppendFormat("\tEntry table starts at {0}", header.entry_table_off).AppendLine();
sb.AppendFormat("\tModule format directives table starts at {0} and contains {1} entries", header.directives_off, header.directives_no).AppendLine();
sb.AppendFormat("\tFixup page table starts at {0}", header.fixup_page_table_off).AppendLine();
sb.AppendFormat("\tFixup record table starts at {0}", header.fixup_record_table_off).AppendLine();
sb.AppendFormat("\tImport module name table starts at {0} and contains {1} entries", header.import_module_table_off, header.import_module_entries).AppendLine();
sb.AppendFormat("\tImport procedure name table starts at {0}", header.import_proc_table_off).AppendLine();
sb.AppendFormat("\tPer-page checksum table starts at {0}", header.perpage_checksum_off).AppendLine();
sb.AppendFormat("\tData pages start at {0}", header.data_pages_off).AppendLine();
sb.AppendFormat("\t{0} pages to preload in this executable", header.preload_pages_no).AppendLine();
sb.AppendFormat("\tNon-resident names table starts at {0} and runs for {1} bytes", header.nonresident_name_table_off, header.nonresident_name_table_len).AppendLine();
sb.AppendFormat("\tNon-resident name table checksum: 0x{0:X8}", header.nonresident_name_table_checksum).AppendLine();
sb.AppendFormat("\tThe auto data segment object number: {0}", header.auto_ds_obj_no).AppendLine();
sb.AppendFormat("\tDebug information starts at {0} and is {1} bytes", header.debug_info_off, header.debug_info_len).AppendLine();
sb.AppendFormat("\tInstance pages in preload section: {0}", header.instance_preload_no).AppendLine();
sb.AppendFormat("\tInstance pages in demand section: {0}", header.instance_demand_no).AppendLine();
sb.AppendFormat("\tHeap size added to the auto ds object: {0}", header.heap_size).AppendLine();
return sb.ToString();
}
sb.AppendFormat("\tThis executable contains {0} pages", header.module_pages_no)
.AppendLine();
sb.AppendFormat("\tObject number to which the Entry Address is relative: {0}", header.eip_object)
.AppendLine();
sb.AppendFormat("\tEntry address of module: {0:X8}h", header.eip).AppendLine();
sb.AppendFormat("\tObject number to which the ESP is relative: {0}", header.esp_object).AppendLine();
sb.AppendFormat("\tStarting stack address of module: {0:X8}h", header.esp).AppendLine();
sb.AppendFormat("\tOne page is {0} bytes", header.page_size).AppendLine();
sb.AppendFormat("\tShift left bits for page offsets: {0}", header.page_off_shift).AppendLine();
sb.AppendFormat("\tTotal size of the fixup information: {0}", header.fixup_size).AppendLine();
sb.AppendFormat("\tChecksum for fixup information: 0x{0:X8}", header.fixup_checksum).AppendLine();
sb.AppendFormat("\tMemory resident tables are {0} bytes long", header.loader_size).AppendLine();
sb.AppendFormat("\tChecksum for loader section: 0x{0:X8}", header.loader_checksum).AppendLine();
sb.AppendFormat("\tObject table starts at {0} and contains {1} objects", header.obj_table_off,
header.obj_no).AppendLine();
sb.AppendFormat("\tObject page table starts at {0}", header.obj_page_table_off).AppendLine();
sb.AppendFormat("\tObject iterated pages starts at {0}", header.obj_iter_pages_off).AppendLine();
sb.AppendFormat("\tResources table starts at {0} and contains {1} entries", header.resource_table_off,
header.resource_entries).AppendLine();
sb.AppendFormat("\tResident name table starts at {0}", header.resident_names_off).AppendLine();
sb.AppendFormat("\tEntry table starts at {0}", header.entry_table_off).AppendLine();
sb.AppendFormat("\tModule format directives table starts at {0} and contains {1} entries",
header.directives_off, header.directives_no).AppendLine();
sb.AppendFormat("\tFixup page table starts at {0}", header.fixup_page_table_off).AppendLine();
sb.AppendFormat("\tFixup record table starts at {0}", header.fixup_record_table_off).AppendLine();
sb.AppendFormat("\tImport module name table starts at {0} and contains {1} entries",
header.import_module_table_off, header.import_module_entries).AppendLine();
sb.AppendFormat("\tImport procedure name table starts at {0}", header.import_proc_table_off).AppendLine();
sb.AppendFormat("\tPer-page checksum table starts at {0}", header.perpage_checksum_off).AppendLine();
sb.AppendFormat("\tData pages start at {0}", header.data_pages_off).AppendLine();
sb.AppendFormat("\t{0} pages to preload in this executable", header.preload_pages_no).AppendLine();
sb.AppendFormat("\tNon-resident names table starts at {0} and runs for {1} bytes",
header.nonresident_name_table_off, header.nonresident_name_table_len).AppendLine();
sb.AppendFormat("\tNon-resident name table checksum: 0x{0:X8}", header.nonresident_name_table_checksum)
.AppendLine();
sb.AppendFormat("\tThe auto data segment object number: {0}", header.auto_ds_obj_no).AppendLine();
sb.AppendFormat("\tDebug information starts at {0} and is {1} bytes", header.debug_info_off,
header.debug_info_len).AppendLine();
sb.AppendFormat("\tInstance pages in preload section: {0}", header.instance_preload_no).AppendLine();
sb.AppendFormat("\tInstance pages in demand section: {0}", header.instance_demand_no).AppendLine();
sb.AppendFormat("\tHeap size added to the auto ds object: {0}", header.heap_size).AppendLine();
return sb.ToString();
}
public string GetInfo()
{
return GetInfo(Header);
}
}
}
}

View File

@@ -23,6 +23,7 @@
// 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;
@@ -30,127 +31,119 @@ using System.Runtime.InteropServices;
namespace libexeinfo
{
/// <summary>
/// Represents a Microsoft/IBM Linear EXecutable
/// Represents a Microsoft/IBM Linear EXecutable
/// </summary>
// TODO: Big-endian (really needed?)
// TODO: Big-endian (really needed?)
public partial class LX
{
/// <summary>
/// The <see cref="FileStream"/> that contains the executable represented by this instance
/// </summary>
public readonly FileStream BaseStream;
/// <summary>
/// Header for this executable
/// </summary>
public readonly LXHeader Header;
/// <summary>
/// If true this instance correctly represents a Microsoft/IBM Linear EXecutable
/// </summary>
public readonly bool IsLX;
public readonly MZ BaseExecutable;
public readonly MZ BaseExecutable;
/// <summary>
/// The <see cref="FileStream" /> that contains the executable represented by this instance
/// </summary>
public readonly FileStream BaseStream;
/// <summary>
/// Header for this executable
/// </summary>
public readonly LXHeader Header;
/// <summary>
/// If true this instance correctly represents a Microsoft/IBM Linear EXecutable
/// </summary>
public readonly bool IsLX;
/// <summary>
/// Initializes a new instance of the <see cref="T:libexeinfo.NE"/> class.
/// </summary>
/// <param name="path">Executable path.</param>
public LX(string path)
{
IsLX = false;
BaseStream = File.Open(path, FileMode.Open, FileAccess.Read);
/// <summary>
/// Initializes a new instance of the <see cref="T:libexeinfo.NE" /> class.
/// </summary>
/// <param name="path">Executable path.</param>
public LX(string path)
{
IsLX = false;
BaseStream = File.Open(path, FileMode.Open, FileAccess.Read);
BaseExecutable = new MZ(BaseStream);
if(BaseExecutable.IsMZ)
{
if(BaseExecutable.Header.new_offset < BaseStream.Length)
if(BaseExecutable.Header.new_offset < BaseStream.Length)
{
BaseStream.Seek(BaseExecutable.Header.new_offset, SeekOrigin.Begin);
byte[] buffer = new byte[Marshal.SizeOf(typeof(LXHeader))];
BaseStream.Read(buffer, 0, buffer.Length);
IntPtr hdrPtr = Marshal.AllocHGlobal(buffer.Length);
Marshal.Copy(buffer, 0, hdrPtr, buffer.Length);
BaseStream.Seek(BaseExecutable.Header.new_offset, SeekOrigin.Begin);
byte[] buffer = new byte[Marshal.SizeOf(typeof(LXHeader))];
BaseStream.Read(buffer, 0, buffer.Length);
IntPtr hdrPtr = Marshal.AllocHGlobal(buffer.Length);
Marshal.Copy(buffer, 0, hdrPtr, buffer.Length);
Header = (LXHeader)Marshal.PtrToStructure(hdrPtr, typeof(LXHeader));
Marshal.FreeHGlobal(hdrPtr);
Marshal.FreeHGlobal(hdrPtr);
IsLX = Header.signature == Signature || Header.signature == Signature16;
}
}
}
}
}
/// <summary>
/// Initializes a new instance of the <see cref="T:libexeinfo.NE"/> class.
/// </summary>
/// <param name="stream">Stream containing the executable.</param>
public LX(FileStream stream)
{
IsLX = false;
BaseStream = stream;
BaseExecutable = new MZ(BaseStream);
if (BaseExecutable.IsMZ)
{
if (BaseExecutable.Header.new_offset < BaseStream.Length)
{
BaseStream.Seek(BaseExecutable.Header.new_offset, SeekOrigin.Begin);
byte[] buffer = new byte[Marshal.SizeOf(typeof(LXHeader))];
BaseStream.Read(buffer, 0, buffer.Length);
IntPtr hdrPtr = Marshal.AllocHGlobal(buffer.Length);
Marshal.Copy(buffer, 0, hdrPtr, buffer.Length);
Header = (LXHeader)Marshal.PtrToStructure(hdrPtr, typeof(LXHeader));
Marshal.FreeHGlobal(hdrPtr);
IsLX = Header.signature == Signature || Header.signature == Signature16;
}
}
}
/// <summary>
/// Initializes a new instance of the <see cref="T:libexeinfo.NE" /> class.
/// </summary>
/// <param name="stream">Stream containing the executable.</param>
public LX(FileStream stream)
{
IsLX = false;
BaseStream = stream;
BaseExecutable = new MZ(BaseStream);
if(BaseExecutable.IsMZ)
if(BaseExecutable.Header.new_offset < BaseStream.Length)
{
BaseStream.Seek(BaseExecutable.Header.new_offset, SeekOrigin.Begin);
byte[] buffer = new byte[Marshal.SizeOf(typeof(LXHeader))];
BaseStream.Read(buffer, 0, buffer.Length);
IntPtr hdrPtr = Marshal.AllocHGlobal(buffer.Length);
Marshal.Copy(buffer, 0, hdrPtr, buffer.Length);
Header = (LXHeader)Marshal.PtrToStructure(hdrPtr, typeof(LXHeader));
Marshal.FreeHGlobal(hdrPtr);
IsLX = Header.signature == Signature || Header.signature == Signature16;
}
}
/// <summary>
/// Identifies if the specified executable is a Microsoft/IBM Linear EXecutable
/// </summary>
/// <returns><c>true</c> if the specified executable is a Microsoft/IBM Linear EXecutable, <c>false</c> otherwise.</returns>
/// <param name="path">Executable path.</param>
public static bool Identify(string path)
{
FileStream BaseStream = File.Open(path, FileMode.Open, FileAccess.Read);
MZ BaseExecutable = new MZ(BaseStream);
if (BaseExecutable.IsMZ)
{
if (BaseExecutable.Header.new_offset < BaseStream.Length)
{
BaseStream.Seek(BaseExecutable.Header.new_offset, SeekOrigin.Begin);
byte[] buffer = new byte[Marshal.SizeOf(typeof(LXHeader))];
BaseStream.Read(buffer, 0, buffer.Length);
IntPtr hdrPtr = Marshal.AllocHGlobal(buffer.Length);
Marshal.Copy(buffer, 0, hdrPtr, buffer.Length);
LXHeader Header = (LXHeader)Marshal.PtrToStructure(hdrPtr, typeof(LXHeader));
Marshal.FreeHGlobal(hdrPtr);
return Header.signature == Signature || Header.signature == Signature16;
}
}
/// <summary>
/// Identifies if the specified executable is a Microsoft/IBM Linear EXecutable
/// </summary>
/// <returns><c>true</c> if the specified executable is a Microsoft/IBM Linear EXecutable, <c>false</c> otherwise.</returns>
/// <param name="path">Executable path.</param>
public static bool Identify(string path)
{
FileStream BaseStream = File.Open(path, FileMode.Open, FileAccess.Read);
MZ BaseExecutable = new MZ(BaseStream);
if(BaseExecutable.IsMZ)
if(BaseExecutable.Header.new_offset < BaseStream.Length)
{
BaseStream.Seek(BaseExecutable.Header.new_offset, SeekOrigin.Begin);
byte[] buffer = new byte[Marshal.SizeOf(typeof(LXHeader))];
BaseStream.Read(buffer, 0, buffer.Length);
IntPtr hdrPtr = Marshal.AllocHGlobal(buffer.Length);
Marshal.Copy(buffer, 0, hdrPtr, buffer.Length);
LXHeader Header = (LXHeader)Marshal.PtrToStructure(hdrPtr, typeof(LXHeader));
Marshal.FreeHGlobal(hdrPtr);
return Header.signature == Signature || Header.signature == Signature16;
}
return false;
}
}
/// <summary>
/// Identifies if the specified executable is a Microsoft/IBM Linear EXecutable
/// </summary>
/// <returns><c>true</c> if the specified executable is a Microsoft/IBM Linear EXecutable, <c>false</c> otherwise.</returns>
/// <param name="stream">Stream containing the executable.</param>
public static bool Identify(FileStream stream)
{
FileStream BaseStream = stream;
MZ BaseExecutable = new MZ(BaseStream);
if (BaseExecutable.IsMZ)
{
if (BaseExecutable.Header.new_offset < BaseStream.Length)
{
BaseStream.Seek(BaseExecutable.Header.new_offset, SeekOrigin.Begin);
byte[] buffer = new byte[Marshal.SizeOf(typeof(LXHeader))];
BaseStream.Read(buffer, 0, buffer.Length);
IntPtr hdrPtr = Marshal.AllocHGlobal(buffer.Length);
Marshal.Copy(buffer, 0, hdrPtr, buffer.Length);
LXHeader Header = (LXHeader)Marshal.PtrToStructure(hdrPtr, typeof(LXHeader));
Marshal.FreeHGlobal(hdrPtr);
return Header.signature == Signature || Header.signature == Signature16;
}
}
/// <summary>
/// Identifies if the specified executable is a Microsoft/IBM Linear EXecutable
/// </summary>
/// <returns><c>true</c> if the specified executable is a Microsoft/IBM Linear EXecutable, <c>false</c> otherwise.</returns>
/// <param name="stream">Stream containing the executable.</param>
public static bool Identify(FileStream stream)
{
FileStream BaseStream = stream;
MZ BaseExecutable = new MZ(BaseStream);
if(BaseExecutable.IsMZ)
if(BaseExecutable.Header.new_offset < BaseStream.Length)
{
BaseStream.Seek(BaseExecutable.Header.new_offset, SeekOrigin.Begin);
byte[] buffer = new byte[Marshal.SizeOf(typeof(LXHeader))];
BaseStream.Read(buffer, 0, buffer.Length);
IntPtr hdrPtr = Marshal.AllocHGlobal(buffer.Length);
Marshal.Copy(buffer, 0, hdrPtr, buffer.Length);
LXHeader Header = (LXHeader)Marshal.PtrToStructure(hdrPtr, typeof(LXHeader));
Marshal.FreeHGlobal(hdrPtr);
return Header.signature == Signature || Header.signature == Signature16;
}
return false;
}
}
return false;
}
}
}

View File

@@ -24,7 +24,6 @@
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
// THE SOFTWARE.
using System;
using System.Runtime.InteropServices;
namespace libexeinfo
@@ -32,195 +31,195 @@ namespace libexeinfo
public partial class LX
{
/// <summary>
/// Header for a Microsoft Linear Executable and IBM Linear eXecutable
/// Header for a Microsoft Linear Executable and IBM Linear eXecutable
/// </summary>
[StructLayout(LayoutKind.Sequential/*, Pack = 2*/)]
[StructLayout(LayoutKind.Sequential /*, Pack = 2*/)]
public struct LXHeader
{
/// <summary>
/// Executable signature
/// Executable signature
/// </summary>
public ushort signature;
/// <summary>
/// Byte ordering
/// Byte ordering
/// </summary>
public byte byte_order;
/// <summary>
/// Word ordering
/// Word ordering
/// </summary>
public byte word_order;
/// <summary>
/// Format level, should be 0
/// Format level, should be 0
/// </summary>
public uint format_level;
/// <summary>
/// Type of CPU required by this executable to run
/// Type of CPU required by this executable to run
/// </summary>
public TargetCpu cpu_type;
/// <summary>
/// Type of operating system requires by this executable to run
/// Type of operating system requires by this executable to run
/// </summary>
public TargetOS os_type;
/// <summary>
/// Executable version
/// Executable version
/// </summary>
public uint module_version;
/// <summary>
/// Executable flags
/// Executable flags
/// </summary>
public ModuleFlags module_flags;
/// <summary>
/// Pages contained in this module
/// Pages contained in this module
/// </summary>
public uint module_pages_no;
/// <summary>
/// Object number to which the Entry Address is relative
/// Object number to which the Entry Address is relative
/// </summary>
public uint eip_object;
/// <summary>
/// Entry address of module
/// Entry address of module
/// </summary>
public uint eip;
/// <summary>
/// Object number to which the ESP is relative
/// Object number to which the ESP is relative
/// </summary>
public uint esp_object;
/// <summary>
/// Starting stack address of module
/// Starting stack address of module
/// </summary>
public uint esp;
/// <summary>
/// Size of one page
/// Size of one page
/// </summary>
public uint page_size;
/// <summary>
/// Shift left bits for page offsets
/// Shift left bits for page offsets
/// </summary>
public uint page_off_shift;
/// <summary>
/// Total size of the fixup information
/// Total size of the fixup information
/// </summary>
public uint fixup_size;
/// <summary>
/// Checksum for fixup information
/// Checksum for fixup information
/// </summary>
public uint fixup_checksum;
/// <summary>
/// Size of memory resident tables
/// Size of memory resident tables
/// </summary>
public uint loader_size;
/// <summary>
/// Checksum for loader section
/// Checksum for loader section
/// </summary>
public uint loader_checksum;
/// <summary>
/// Object table offset
/// Object table offset
/// </summary>
public uint obj_table_off;
/// <summary>
/// Object table count
/// Object table count
/// </summary>
public uint obj_no;
/// <summary>
/// Object page table offset
/// Object page table offset
/// </summary>
public uint obj_page_table_off;
/// <summary>
/// Object iterated pages offset
/// Object iterated pages offset
/// </summary>
public uint obj_iter_pages_off;
/// <summary>
/// Resource table offset
/// Resource table offset
/// </summary>
public uint resource_table_off;
/// <summary>
/// Entries in resource table
/// Entries in resource table
/// </summary>
public uint resource_entries;
/// <summary>
/// Resident name table offset
/// Resident name table offset
/// </summary>
public uint resident_names_off;
/// <summary>
/// Entry table offset
/// Entry table offset
/// </summary>
public uint entry_table_off;
/// <summary>
/// Module format directives table offset
/// Module format directives table offset
/// </summary>
public uint directives_off;
/// <summary>
/// Entries in module format directives table
/// Entries in module format directives table
/// </summary>
public uint directives_no;
/// <summary>
/// Fixup page table offset
/// Fixup page table offset
/// </summary>
public uint fixup_page_table_off;
/// <summary>
/// Fixup record table offset
/// Fixup record table offset
/// </summary>
public uint fixup_record_table_off;
/// <summary>
/// Import module name table offset
/// Import module name table offset
/// </summary>
public uint import_module_table_off;
/// <summary>
/// Entries in the import module name table
/// Entries in the import module name table
/// </summary>
public uint import_module_entries;
/// <summary>
/// Import procedure name table offset
/// Import procedure name table offset
/// </summary>
public uint import_proc_table_off;
/// <summary>
/// Per-page checksum table offset
/// Per-page checksum table offset
/// </summary>
public uint perpage_checksum_off;
/// <summary>
/// Data pages offset
/// Data pages offset
/// </summary>
public uint data_pages_off;
/// <summary>
/// Number of preload pages for this module
/// Number of preload pages for this module
/// </summary>
public uint preload_pages_no;
/// <summary>
/// Non-resident name table offset
/// Non-resident name table offset
/// </summary>
public uint nonresident_name_table_off;
/// <summary>
/// Number of bytes in the non-resident name table
/// Number of bytes in the non-resident name table
/// </summary>
public uint nonresident_name_table_len;
/// <summary>
/// Non-resident name table checksum
/// Non-resident name table checksum
/// </summary>
public uint nonresident_name_table_checksum;
/// <summary>
/// The auto data segment object number
/// The auto data segment object number
/// </summary>
public uint auto_ds_obj_no;
/// <summary>
/// Debug information offset
/// Debug information offset
/// </summary>
public uint debug_info_off;
/// <summary>
/// Debug information length
/// Debug information length
/// </summary>
public uint debug_info_len;
/// <summary>
/// Instance pages in preload section
/// Instance pages in preload section
/// </summary>
public uint instance_preload_no;
/// <summary>
/// Instance pages in demand section
/// Instance pages in demand section
/// </summary>
public uint instance_demand_no;
/// <summary>
/// Heap size added to the auto ds object
/// Heap size added to the auto ds object
/// </summary>
public uint heap_size;
}
}
}
}

View File

@@ -24,14 +24,13 @@
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
// THE SOFTWARE.
using System;
namespace libexeinfo
{
public partial class MZ
{
/// <summary>
/// MZ executable signature, "MZ"
/// </summary>
public const ushort Signature = 0x5A4D;
}
/// <summary>
/// MZ executable signature, "MZ"
/// </summary>
public const ushort Signature = 0x5A4D;
}
}

View File

@@ -29,42 +29,45 @@ using System.Text;
namespace libexeinfo
{
public partial class MZ
{
/// <summary>
/// Gets a string with human readable information for a given MZ header
/// </summary>
/// <returns>Human readable information for given MZ header.</returns>
/// <param name="header">MZ executable header.</param>
public static string GetInfo(MZHeader header)
{
{
/// <summary>
/// Gets a string with human readable information for a given MZ header
/// </summary>
/// <returns>Human readable information for given MZ header.</returns>
/// <param name="header">MZ executable header.</param>
public static string GetInfo(MZHeader header)
{
StringBuilder sb = new StringBuilder();
sb.AppendLine("DOS MZ executable:");
sb.AppendFormat("\tBlocks in file: {0}", header.blocks_in_file).AppendLine();
sb.AppendFormat("\t{0} bytes used in last block", header.bytes_in_last_block == 0 ? 512 : header.bytes_in_last_block).AppendLine();
sb.AppendFormat("\t{0} relocations present after the header", header.num_relocs).AppendLine();
sb.AppendFormat("\t{0} paragraphs in header", header.header_paragraphs).AppendLine();
sb.AppendFormat("\t{0} paragraphs of additional memory required", header.min_extra_paragraphs).AppendLine();
sb.AppendFormat("\t{0} paragraphs of additional memory requested", header.max_extra_paragraphs).AppendLine();
sb.AppendFormat("\tSegment address for SS: {0:X4}h", header.ss).AppendLine();
sb.AppendFormat("\tInitial value of SP: {0:X4}h", header.sp).AppendLine();
sb.AppendFormat("\tInitial value of IP: {0:X4}h", header.ip).AppendLine();
sb.AppendFormat("\tInitial value of CS: {0:X4}h", header.cs).AppendLine();
sb.AppendFormat("\tOffset to relocation table: {0}", header.reloc_table_offset).AppendLine();
sb.AppendFormat("\tFile contains {0} overlays", header.overlay_number).AppendLine();
sb.AppendFormat("\tFile checksum: 0x{0:X4}", header.checksum).AppendLine();
sb.AppendFormat("\tOEM ID: {0}", header.oem_id).AppendLine();
sb.AppendFormat("\tOEM information: 0x{0:X4}", header.oem_info).AppendLine();
sb.AppendFormat("\tOffset to new header: {0}", header.new_offset).AppendLine();
sb.AppendFormat("\t{0} bytes used in last block",
header.bytes_in_last_block == 0 ? 512 : header.bytes_in_last_block).AppendLine();
sb.AppendFormat("\t{0} relocations present after the header", header.num_relocs).AppendLine();
sb.AppendFormat("\t{0} paragraphs in header", header.header_paragraphs).AppendLine();
sb.AppendFormat("\t{0} paragraphs of additional memory required", header.min_extra_paragraphs)
.AppendLine();
sb.AppendFormat("\t{0} paragraphs of additional memory requested", header.max_extra_paragraphs)
.AppendLine();
sb.AppendFormat("\tSegment address for SS: {0:X4}h", header.ss).AppendLine();
sb.AppendFormat("\tInitial value of SP: {0:X4}h", header.sp).AppendLine();
sb.AppendFormat("\tInitial value of IP: {0:X4}h", header.ip).AppendLine();
sb.AppendFormat("\tInitial value of CS: {0:X4}h", header.cs).AppendLine();
sb.AppendFormat("\tOffset to relocation table: {0}", header.reloc_table_offset).AppendLine();
sb.AppendFormat("\tFile contains {0} overlays", header.overlay_number).AppendLine();
sb.AppendFormat("\tFile checksum: 0x{0:X4}", header.checksum).AppendLine();
sb.AppendFormat("\tOEM ID: {0}", header.oem_id).AppendLine();
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();
}
}
/// <summary>
/// Gets a string with human readable information for the MZ executable represented by this instance
/// </summary>
/// <returns>Human readable information for this instance.</returns>
public string GetInfo()
/// <summary>
/// Gets a string with human readable information for the MZ executable represented by this instance
/// </summary>
/// <returns>Human readable information for this instance.</returns>
public string GetInfo()
{
return GetInfo(Header);
}
}
}
}

View File

@@ -23,94 +23,95 @@
// 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
{
/// <summary>
/// Represents a DOS relocatable executable
/// </summary>
public partial class MZ
/// <summary>
/// Represents a DOS relocatable executable
/// </summary>
public partial class MZ
{
/// <summary>
/// The <see cref="FileStream"/> that contains the executable represented by this instance
/// </summary>
public readonly FileStream BaseStream;
/// <summary>
/// Header for this executable
/// </summary>
public readonly MZHeader Header;
/// <summary>
/// If true this instance correctly represents a DOS relocatable executable
/// </summary>
public readonly bool IsMZ;
/// <summary>
/// The <see cref="FileStream" /> that contains the executable represented by this instance
/// </summary>
public readonly FileStream BaseStream;
/// <summary>
/// Header for this executable
/// </summary>
public readonly MZHeader Header;
/// <summary>
/// If true this instance correctly represents a DOS relocatable executable
/// </summary>
public readonly bool IsMZ;
/// <summary>
/// Initializes a new instance of the <see cref="T:libexeinfo.MZ"/> class.
/// </summary>
/// <param name="path">Executable path.</param>
public MZ(string path)
/// <summary>
/// Initializes a new instance of the <see cref="T:libexeinfo.MZ" /> class.
/// </summary>
/// <param name="path">Executable path.</param>
public MZ(string path)
{
byte[] buffer = new byte[Marshal.SizeOf(typeof(MZHeader))];
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);
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;
}
}
/// <summary>
/// Initializes a new instance of the <see cref="T:libexeinfo.MZ"/> class.
/// </summary>
/// <param name="stream">Stream containing the executable.</param>
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;
}
/// <summary>
/// Identifies if the specified executable is a DOS relocatable executable
/// </summary>
/// <returns><c>true</c> if the specified executable is a DOS relocatable executable, <c>false</c> otherwise.</returns>
/// <param name="path">Executable path.</param>
public static bool Identify(string path)
/// <summary>
/// Initializes a new instance of the <see cref="T:libexeinfo.MZ" /> class.
/// </summary>
/// <param name="stream">Stream containing the executable.</param>
public MZ(FileStream stream)
{
FileStream exeFs = File.Open(path, FileMode.Open, FileAccess.Read);
return Identify(exeFs);
}
byte[] buffer = new byte[Marshal.SizeOf(typeof(MZHeader))];
/// <summary>
/// Identifies if the specified executable is a DOS relocatable executable
/// </summary>
/// <returns><c>true</c> if the specified executable is a DOS relocatable executable, <c>false</c> otherwise.</returns>
/// <param name="stream">Stream containing the executable.</param>
public static bool Identify(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;
}
/// <summary>
/// Identifies if the specified executable is a DOS relocatable executable
/// </summary>
/// <returns><c>true</c> if the specified executable is a DOS relocatable executable, <c>false</c> otherwise.</returns>
/// <param name="path">Executable path.</param>
public static bool Identify(string path)
{
FileStream exeFs = File.Open(path, FileMode.Open, FileAccess.Read);
return Identify(exeFs);
}
/// <summary>
/// Identifies if the specified executable is a DOS relocatable executable
/// </summary>
/// <returns><c>true</c> if the specified executable is a DOS relocatable executable, <c>false</c> otherwise.</returns>
/// <param name="stream">Stream containing the executable.</param>
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);
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
{
/// <summary>
/// Header of a DOS relocatable executable
/// Header of a DOS relocatable executable
/// </summary>
[StructLayout(LayoutKind.Sequential, Pack = 1)]
public struct MZHeader
@@ -52,15 +52,15 @@ namespace libexeinfo
public ushort overlay_number;
[MarshalAs(UnmanagedType.ByValArray, SizeConst = 4)]
public ushort[] reserved;
public ushort oem_id;
public ushort oem_info;
public ushort oem_id;
public ushort oem_info;
[MarshalAs(UnmanagedType.ByValArray, SizeConst = 10)]
public ushort[] reserved2;
public uint new_offset;
public uint new_offset;
}
/// <summary>
/// Entry in the relocation table
/// Entry in the relocation table
/// </summary>
[StructLayout(LayoutKind.Sequential, Pack = 1)]
public struct RelocationTableEntry

View File

@@ -24,89 +24,60 @@
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
// THE SOFTWARE.
using System;
using System.ComponentModel;
namespace libexeinfo
{
public partial class NE
{
/// <summary>
/// New Executable signature, "NE"
/// </summary>
public const ushort Signature = 0x454E;
/// <summary>
/// Signature for a <see cref="FixedFileInfo"/>
/// </summary>
public static readonly string FixedFileInfoSig = "VS_VERSION_INFO";
/// <summary>
/// Signature for list of name=value strings inside a version resource
/// </summary>
public static readonly string StringFileInfo = "StringFileInfo";
/// <summary>
/// New Executable signature, "NE"
/// </summary>
public const ushort Signature = 0x454E;
/// <summary>
/// Signature for a <see cref="FixedFileInfo" />
/// </summary>
public static readonly string FixedFileInfoSig = "VS_VERSION_INFO";
/// <summary>
/// Signature for list of name=value strings inside a version resource
/// </summary>
public static readonly string StringFileInfo = "StringFileInfo";
/// <summary>
/// Gets the name of a resource type according to its identifier
/// </summary>
/// <returns>The resource type name.</returns>
/// <param name="id">Resource type identifier.</param>
public static string ResourceIdToName(ushort id)
{
switch (id & 0x7FFF)
{
case (int)ResourceTypes.RT_ACCELERATOR:
return "RT_ACCELERATOR";
case (int)ResourceTypes.RT_ANICURSOR:
return "RT_ANICURSOR";
case (int)ResourceTypes.RT_ANIICON:
return "RT_ANIICON";
case (int)ResourceTypes.RT_BITMAP:
return "RT_BITMAP";
case (int)ResourceTypes.RT_CURSOR:
return "RT_CURSOR";
case (int)ResourceTypes.RT_DIALOG:
return "RT_DIALOG";
case (int)ResourceTypes.RT_DIALOGEX:
return "RT_DIALOGEX";
case (int)ResourceTypes.RT_DLGINCLUDE:
return "RT_DLGINCLUDE";
case (int)ResourceTypes.RT_DLGINIT:
return "RT_DLGINIT";
case (int)ResourceTypes.RT_FONT:
return "RT_FONT";
case (int)ResourceTypes.RT_FONTDIR:
return "RT_FONTDIR";
case (int)ResourceTypes.RT_GROUP_CURSOR:
return "RT_GROUP_CURSOR";
case (int)ResourceTypes.RT_GROUP_ICON:
return "RT_GROUP_ICON";
case (int)ResourceTypes.RT_HTML:
return "RT_HTML";
case (int)ResourceTypes.RT_ICON:
return "RT_ICON";
case (int)ResourceTypes.RT_MANIFEST:
return "RT_MANIFEST";
case (int)ResourceTypes.RT_MENU:
return "RT_MENU";
case (int)ResourceTypes.RT_MENUEX:
return "RT_MENUEX";
case (int)ResourceTypes.RT_MESSAGETABLE:
return "RT_MESSAGETABLE";
case (int)ResourceTypes.RT_NEWBITMAP:
return "RT_NEWBITMAP";
case (int)ResourceTypes.RT_PLUGPLAY:
return "RT_PLUGPLAY";
case (int)ResourceTypes.RT_RCDATA:
return "RT_RCDATA";
case (int)ResourceTypes.RT_STRING:
return "RT_STRING";
case (int)ResourceTypes.RT_TOOLBAR:
return "RT_TOOLBAR";
case (int)ResourceTypes.RT_VERSION:
return "RT_VERSION";
case (int)ResourceTypes.RT_VXD:
return "RT_VXD";
default:
return string.Format("{0}", id & 0x7FFF);
}
}
}
/// <summary>
/// Gets the name of a resource type according to its identifier
/// </summary>
/// <returns>The resource type name.</returns>
/// <param name="id">Resource type identifier.</param>
public static string ResourceIdToName(ushort id)
{
switch(id & 0x7FFF)
{
case (int)ResourceTypes.RT_ACCELERATOR: return "RT_ACCELERATOR";
case (int)ResourceTypes.RT_ANICURSOR: return "RT_ANICURSOR";
case (int)ResourceTypes.RT_ANIICON: return "RT_ANIICON";
case (int)ResourceTypes.RT_BITMAP: return "RT_BITMAP";
case (int)ResourceTypes.RT_CURSOR: return "RT_CURSOR";
case (int)ResourceTypes.RT_DIALOG: return "RT_DIALOG";
case (int)ResourceTypes.RT_DIALOGEX: return "RT_DIALOGEX";
case (int)ResourceTypes.RT_DLGINCLUDE: return "RT_DLGINCLUDE";
case (int)ResourceTypes.RT_DLGINIT: return "RT_DLGINIT";
case (int)ResourceTypes.RT_FONT: return "RT_FONT";
case (int)ResourceTypes.RT_FONTDIR: return "RT_FONTDIR";
case (int)ResourceTypes.RT_GROUP_CURSOR: return "RT_GROUP_CURSOR";
case (int)ResourceTypes.RT_GROUP_ICON: return "RT_GROUP_ICON";
case (int)ResourceTypes.RT_HTML: return "RT_HTML";
case (int)ResourceTypes.RT_ICON: return "RT_ICON";
case (int)ResourceTypes.RT_MANIFEST: return "RT_MANIFEST";
case (int)ResourceTypes.RT_MENU: return "RT_MENU";
case (int)ResourceTypes.RT_MENUEX: return "RT_MENUEX";
case (int)ResourceTypes.RT_MESSAGETABLE: return "RT_MESSAGETABLE";
case (int)ResourceTypes.RT_NEWBITMAP: return "RT_NEWBITMAP";
case (int)ResourceTypes.RT_PLUGPLAY: return "RT_PLUGPLAY";
case (int)ResourceTypes.RT_RCDATA: return "RT_RCDATA";
case (int)ResourceTypes.RT_STRING: return "RT_STRING";
case (int)ResourceTypes.RT_TOOLBAR: return "RT_TOOLBAR";
case (int)ResourceTypes.RT_VERSION: return "RT_VERSION";
case (int)ResourceTypes.RT_VXD: return "RT_VXD";
default: return string.Format("{0}", id & 0x7FFF);
}
}
}
}

View File

@@ -25,195 +25,196 @@
// THE SOFTWARE.
using System;
namespace libexeinfo
{
public partial class NE
{
/// <summary>
/// Program flags.
/// </summary>
[Flags]
public enum ProgramFlags : byte
{
NoDGroup = 0,
SingleDGroup = 1,
MultipleDGroup = 2,
GlobalInit = 1 << 2,
ProtectedMode = 1 << 3,
i86 = 1 << 4,
i286 = 1 << 5,
i386 = 1 << 6,
i87 = 1 << 7
}
/// <summary>
/// Target operating system.
/// </summary>
public enum TargetOS : byte
{
Unknown = 0,
OS2 = 1,
Windows = 2,
DOS = 3,
Win32 = 4,
Borland = 5
}
/// <summary>
/// Application flags.
/// Application flags.
/// </summary>
[Flags]
public enum ApplicationFlags : byte
{
FullScreen = 1,
FullScreen = 1,
GUICompatible = 2,
Errors = 1 << 5,
Errors = 1 << 5,
NonConforming = 1 << 6,
DLL = 1 << 7
DLL = 1 << 7
}
/// <summary>
/// OS/2 flags.
/// OS/2 flags.
/// </summary>
[Flags]
public enum OS2Flags : byte
{
LongFilename = 1 << 0,
ProtectedMode2 = 1 << 1,
LongFilename = 1 << 0,
ProtectedMode2 = 1 << 1,
ProportionalFonts = 1 << 2,
GangloadArea = 1 << 3,
GangloadArea = 1 << 3
}
/// <summary>
/// Resource flags.
/// Program flags.
/// </summary>
[Flags]
public enum ProgramFlags : byte
{
NoDGroup = 0,
SingleDGroup = 1,
MultipleDGroup = 2,
GlobalInit = 1 << 2,
ProtectedMode = 1 << 3,
i86 = 1 << 4,
i286 = 1 << 5,
i386 = 1 << 6,
i87 = 1 << 7
}
/// <summary>
/// Resource flags.
/// </summary>
[Flags]
public enum ResourceFlags : ushort
{
Moveable = 0x10,
Pure = 0x20,
Preload = 0x40
Pure = 0x20,
Preload = 0x40
}
/// <summary>
/// Resource types.
/// Resource types.
/// </summary>
public enum ResourceTypes : ushort
{
RT_ACCELERATOR = 9,
RT_ANICURSOR = 21,
RT_ANIICON = 22,
RT_BITMAP = 2,
RT_CURSOR = 1,
RT_DIALOG = 5,
RT_DIALOGEX = 18,
RT_DLGINCLUDE = 17,
RT_DLGINIT = 240,
RT_FONT = 8,
RT_FONTDIR = 7,
RT_ACCELERATOR = 9,
RT_ANICURSOR = 21,
RT_ANIICON = 22,
RT_BITMAP = 2,
RT_CURSOR = 1,
RT_DIALOG = 5,
RT_DIALOGEX = 18,
RT_DLGINCLUDE = 17,
RT_DLGINIT = 240,
RT_FONT = 8,
RT_FONTDIR = 7,
RT_GROUP_CURSOR = 12,
RT_GROUP_ICON = 13,
RT_HTML = 23,
RT_ICON = 3,
RT_MANIFEST = 24,
RT_MENU = 4,
RT_MENUEX = 15,
RT_GROUP_ICON = 13,
RT_HTML = 23,
RT_ICON = 3,
RT_MANIFEST = 24,
RT_MENU = 4,
RT_MENUEX = 15,
RT_MESSAGETABLE = 11,
RT_NEWBITMAP = RT_NEW | RT_BITMAP,
RT_PLUGPLAY = 19,
RT_RCDATA = 10,
RT_STRING = 6,
RT_TOOLBAR = 241,
RT_VERSION = 16,
RT_VXD = 20,
RT_NEW = 0x2000,
RT_NEWBITMAP = RT_NEW | RT_BITMAP,
RT_PLUGPLAY = 19,
RT_RCDATA = 10,
RT_STRING = 6,
RT_TOOLBAR = 241,
RT_VERSION = 16,
RT_VXD = 20,
RT_NEW = 0x2000
}
/// <summary>
/// Version file flags.
/// Target operating system.
/// </summary>
public enum TargetOS : byte
{
Unknown = 0,
OS2 = 1,
Windows = 2,
DOS = 3,
Win32 = 4,
Borland = 5
}
/// <summary>
/// Version file flags.
/// </summary>
[Flags]
public enum VersionFileFlags : uint
{
VS_FF_DEBUG = 0x00000001,
VS_FF_DEBUG = 0x00000001,
VS_FF_INFOINFERRED = 0x00000010,
VS_FF_PATCHED = 0x00000004,
VS_FF_PRERELEASE = 0x00000002,
VS_FF_PATCHED = 0x00000004,
VS_FF_PRERELEASE = 0x00000002,
VS_FF_PRIVATEBUILD = 0x00000008,
VS_FF_SPECIALBUILD = 0x00000020,
VS_FF_SPECIALBUILD = 0x00000020
}
/// <summary>
/// Version file operating system.
/// Version file operating system.
/// </summary>
public enum VersionFileOS : uint
{
VOS_DOS = 0x00010000,
VOS_NT = 0x00040000,
VOS_DOS = 0x00010000,
VOS_NT = 0x00040000,
VOS_WINDOWS16 = 0x00000001,
VOS_WINDOWS32 = 0x00000004,
VOS_OS216 = 0x00020000,
VOS_OS232 = 0x00030000,
VOS_PM16 = 0x00000002,
VOS_PM32 = 0x00000003,
VOS_UNKNOWN = 0x00000000,
VOS_OS216 = 0x00020000,
VOS_OS232 = 0x00030000,
VOS_PM16 = 0x00000002,
VOS_PM32 = 0x00000003,
VOS_UNKNOWN = 0x00000000,
// Combinations, some have no sense
VOS_DOS_NT = 0x00050000,
VOS_DOS_WINDOWS16 = 0x00010001,
VOS_DOS_WINDOWS32 = 0x00010004,
VOS_DOS_PM16 = 0x00010002,
VOS_DOS_PM32 = 0x00010003,
VOS_NT_WINDOWS16 = 0x00040001,
VOS_NT_WINDOWS32 = 0x00040004,
VOS_NT_PM16 = 0x00040002,
VOS_NT_PM32 = 0x00040003,
VOS_DOS_NT = 0x00050000,
VOS_DOS_WINDOWS16 = 0x00010001,
VOS_DOS_WINDOWS32 = 0x00010004,
VOS_DOS_PM16 = 0x00010002,
VOS_DOS_PM32 = 0x00010003,
VOS_NT_WINDOWS16 = 0x00040001,
VOS_NT_WINDOWS32 = 0x00040004,
VOS_NT_PM16 = 0x00040002,
VOS_NT_PM32 = 0x00040003,
VOS_OS216_WINDOWS16 = 0x00020001,
VOS_OS216_WINDOWS32 = 0x00020004,
VOS_OS216_PM16 = 0x00020002,
VOS_OS216_PM32 = 0x00020003,
VOS_OS216_PM16 = 0x00020002,
VOS_OS216_PM32 = 0x00020003,
VOS_OS232_WINDOWS16 = 0x00030001,
VOS_OS232_WINDOWS32 = 0x00030004,
VOS_OS232_PM16 = 0x00030002,
VOS_OS232_PM32 = 0x00030003,
VOS_OS232_PM16 = 0x00030002,
VOS_OS232_PM32 = 0x00030003
}
/// <summary>
/// Version file type.
/// </summary>
public enum VersionFileType : uint
{
VFT_APP = 0x00000001,
VFT_DLL = 0x00000002,
VFT_DRV = 0x00000003,
VFT_FONT = 0x00000004,
VFT_STATIC_LIB = 0x00000007,
VFT_UNKNOWN = 0x00000000,
VFT_VXD = 0x00000005,
}
/// <summary>
/// Version file subtype.
/// Version file subtype.
/// </summary>
public enum VersionFileSubtype : uint
{
VFT2_UNKNOWN = 0x00000000,
// Drivers
VFT2_DRV_COMM = 0x0000000A,
VFT2_DRV_DISPLAY = 0x00000004,
VFT2_DRV_INSTALLABLE = 0x00000008,
VFT2_DRV_KEYBOARD = 0x00000002,
VFT2_DRV_LANGUAGE = 0x00000003,
VFT2_DRV_MOUSE = 0x00000005,
VFT2_DRV_NETWORK = 0x00000006,
VFT2_DRV_PRINTER = 0x00000001,
VFT2_DRV_SOUND = 0x00000009,
VFT2_DRV_SYSTEM = 0x00000007,
VFT2_DRV_COMM = 0x0000000A,
VFT2_DRV_DISPLAY = 0x00000004,
VFT2_DRV_INSTALLABLE = 0x00000008,
VFT2_DRV_KEYBOARD = 0x00000002,
VFT2_DRV_LANGUAGE = 0x00000003,
VFT2_DRV_MOUSE = 0x00000005,
VFT2_DRV_NETWORK = 0x00000006,
VFT2_DRV_PRINTER = 0x00000001,
VFT2_DRV_SOUND = 0x00000009,
VFT2_DRV_SYSTEM = 0x00000007,
VFT2_DRV_VERSIONED_PRINTER = 0x0000000C,
// Fonts
VFT2_FONT_RASTER = 0x00000001,
VFT2_FONT_RASTER = 0x00000001,
VFT2_FONT_TRUETYPE = 0x00000003,
VFT2_FONT_VECTOR = 0x00000002,
VFT2_FONT_VECTOR = 0x00000002
}
/// <summary>
/// Version file type.
/// </summary>
public enum VersionFileType : uint
{
VFT_APP = 0x00000001,
VFT_DLL = 0x00000002,
VFT_DRV = 0x00000003,
VFT_FONT = 0x00000004,
VFT_STATIC_LIB = 0x00000007,
VFT_UNKNOWN = 0x00000000,
VFT_VXD = 0x00000005
}
}
}

View File

@@ -31,140 +31,166 @@ using System.Text;
namespace libexeinfo
{
public partial class NE
{
public static string GetInfo(NEHeader header)
{
public partial class NE
{
public static string GetInfo(NEHeader header)
{
StringBuilder sb = new StringBuilder();
sb.AppendLine("New Executable (NE):");
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))
sb.AppendLine("\tApplication uses a single shared DGroup");
else if (!header.program_flags.HasFlag(ProgramFlags.SingleDGroup) && header.program_flags.HasFlag(ProgramFlags.MultipleDGroup))
sb.AppendLine("\tApplication uses a multiple DGroup");
else if (header.program_flags.HasFlag(ProgramFlags.SingleDGroup) && header.program_flags.HasFlag(ProgramFlags.MultipleDGroup))
sb.AppendLine("\tApplication indicates an incorrect DGroup value");
else if (!header.program_flags.HasFlag(ProgramFlags.SingleDGroup) && !header.program_flags.HasFlag(ProgramFlags.MultipleDGroup))
sb.AppendLine("\tApplication does not use DGroup");
if (header.program_flags.HasFlag(ProgramFlags.GlobalInit))
sb.AppendLine("\tApplication uses global initialization");
if (header.program_flags.HasFlag(ProgramFlags.ProtectedMode))
sb.AppendLine("\tApplication uses protected mode");
if (header.program_flags.HasFlag(ProgramFlags.i86))
sb.AppendLine("\tApplication uses 8086 instructions");
if (header.program_flags.HasFlag(ProgramFlags.i286))
sb.AppendLine("\tApplication uses 80286 instructions");
if (header.program_flags.HasFlag(ProgramFlags.i386))
sb.AppendLine("\tApplication uses 80386 instructions");
if (header.program_flags.HasFlag(ProgramFlags.i87))
sb.AppendLine("\tApplication uses floating point instructions");
sb.AppendLine("New Executable (NE):");
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))
sb.AppendLine("\tApplication uses a single shared DGroup");
else if(!header.program_flags.HasFlag(ProgramFlags.SingleDGroup) &&
header.program_flags.HasFlag(ProgramFlags.MultipleDGroup))
sb.AppendLine("\tApplication uses a multiple DGroup");
else if(header.program_flags.HasFlag(ProgramFlags.SingleDGroup) &&
header.program_flags.HasFlag(ProgramFlags.MultipleDGroup))
sb.AppendLine("\tApplication indicates an incorrect DGroup value");
else if(!header.program_flags.HasFlag(ProgramFlags.SingleDGroup) &&
!header.program_flags.HasFlag(ProgramFlags.MultipleDGroup))
sb.AppendLine("\tApplication does not use DGroup");
if(header.program_flags.HasFlag(ProgramFlags.GlobalInit))
sb.AppendLine("\tApplication uses global initialization");
if(header.program_flags.HasFlag(ProgramFlags.ProtectedMode))
sb.AppendLine("\tApplication uses protected mode");
if(header.program_flags.HasFlag(ProgramFlags.i86)) sb.AppendLine("\tApplication uses 8086 instructions");
if(header.program_flags.HasFlag(ProgramFlags.i286)) sb.AppendLine("\tApplication uses 80286 instructions");
if(header.program_flags.HasFlag(ProgramFlags.i386)) sb.AppendLine("\tApplication uses 80386 instructions");
if(header.program_flags.HasFlag(ProgramFlags.i87))
sb.AppendLine("\tApplication uses floating point instructions");
if (header.target_os == TargetOS.OS2)
{
sb.AppendLine("\tOS/2 application");
if(header.target_os == TargetOS.OS2)
{
sb.AppendLine("\tOS/2 application");
if(header.os_major > 0)
sb.AppendFormat("\tApplication requires OS/2 {0}.{1} to run", header.os_major, header.os_minor).AppendLine();
sb.AppendFormat("\tApplication requires OS/2 {0}.{1} to run", header.os_major, header.os_minor)
.AppendLine();
else sb.AppendLine("\tApplication requires OS/2 1.0 to run");
if(header.application_flags.HasFlag(ApplicationFlags.FullScreen) &&
!header.application_flags.HasFlag(ApplicationFlags.GUICompatible))
sb.AppendLine("\tApplication is full screen, unaware of Presentation Manager");
else if(!header.application_flags.HasFlag(ApplicationFlags.FullScreen) &&
header.application_flags.HasFlag(ApplicationFlags.GUICompatible))
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))
sb.AppendLine("\tApplication uses Presentation Manager");
if(header.os2_flags.HasFlag(OS2Flags.LongFilename))
sb.AppendLine("\tApplication supports long filenames");
if(header.os2_flags.HasFlag(OS2Flags.ProtectedMode2))
sb.AppendLine("\tApplication uses OS/2 2.x protected mode");
if(header.os2_flags.HasFlag(OS2Flags.ProportionalFonts))
sb.AppendLine("\tApplication uses OS/2 2.x proportional fonts");
if(header.os2_flags.HasFlag(OS2Flags.GangloadArea))
sb.AppendFormat("\tGangload area starts at {0} an runs for {1} bytes", header.return_thunks_offset,
header.segment_reference_thunks).AppendLine();
else
sb.AppendLine("\tApplication requires OS/2 1.0 to run");
if (header.application_flags.HasFlag(ApplicationFlags.FullScreen) && !header.application_flags.HasFlag(ApplicationFlags.GUICompatible))
sb.AppendLine("\tApplication is full screen, unaware of Presentation Manager");
else if (!header.application_flags.HasFlag(ApplicationFlags.FullScreen) && header.application_flags.HasFlag(ApplicationFlags.GUICompatible))
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))
sb.AppendLine("\tApplication uses Presentation Manager");
if (header.os2_flags.HasFlag(OS2Flags.LongFilename))
sb.AppendLine("\tApplication supports long filenames");
if (header.os2_flags.HasFlag(OS2Flags.ProtectedMode2))
sb.AppendLine("\tApplication uses OS/2 2.x protected mode");
if (header.os2_flags.HasFlag(OS2Flags.ProportionalFonts))
sb.AppendLine("\tApplication uses OS/2 2.x proportional fonts");
if (header.os2_flags.HasFlag(OS2Flags.GangloadArea))
sb.AppendFormat("\tGangload area starts at {0} an runs for {1} bytes", header.return_thunks_offset, header.segment_reference_thunks).AppendLine();
else
{
sb.AppendFormat("\tReturn thunks are at: {0}", header.return_thunks_offset).AppendLine();
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 || header.target_os == TargetOS.Unknown)
{
if (header.target_os == TargetOS.Windows || header.target_os == TargetOS.Unknown)
sb.AppendLine("\t16-bit Windows application");
else if (header.target_os == TargetOS.Win32)
sb.AppendLine("\t32-bit Windows application");
{
sb.AppendFormat("\tReturn thunks are at: {0}", header.return_thunks_offset).AppendLine();
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 ||
header.target_os == TargetOS.Unknown)
{
if(header.target_os == TargetOS.Windows || header.target_os == TargetOS.Unknown)
sb.AppendLine("\t16-bit Windows application");
else if(header.target_os == TargetOS.Win32)
sb.AppendLine("\t32-bit Windows application");
if(header.os_major > 0)
sb.AppendFormat("\tApplication requires Windows {0}.{1} to run", header.os_major, header.os_minor).AppendLine();
sb.AppendFormat("\tApplication requires Windows {0}.{1} to run", header.os_major, header.os_minor)
.AppendLine();
else if(header.target_os == TargetOS.Windows)
sb.AppendLine("\tApplication requires Windows 2.0 to run");
else if (header.target_os == TargetOS.Unknown)
sb.AppendLine("\tApplication requires Windows 1.0 to run");
if (header.application_flags.HasFlag(ApplicationFlags.FullScreen) && !header.application_flags.HasFlag(ApplicationFlags.GUICompatible))
sb.AppendLine("\tApplication is full screen, unaware of Windows");
else if (!header.application_flags.HasFlag(ApplicationFlags.FullScreen) && header.application_flags.HasFlag(ApplicationFlags.GUICompatible))
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))
sb.AppendLine("\tApplication uses Windows");
sb.AppendFormat("\tReturn thunks are at: {0}", header.return_thunks_offset).AppendLine();
sb.AppendFormat("\tSegment reference thunks are at: {0}", header.segment_reference_thunks).AppendLine();
}
else if (header.target_os == TargetOS.DOS)
{
sb.AppendLine("\tDOS application");
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))
sb.AppendLine("\tApplication is full screen, unaware of Windows");
else if (!header.application_flags.HasFlag(ApplicationFlags.FullScreen) && header.application_flags.HasFlag(ApplicationFlags.GUICompatible))
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))
sb.AppendLine("\tApplication uses Windows");
sb.AppendFormat("\tReturn thunks are at: {0}", header.return_thunks_offset).AppendLine();
sb.AppendFormat("\tSegment reference thunks are at: {0}", header.segment_reference_thunks).AppendLine();
}
else if (header.target_os == TargetOS.Borland)
{
sb.AppendLine("\tBorland Operating System Services application");
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))
sb.AppendLine("\tApplication is full screen, unaware of Windows");
else if (!header.application_flags.HasFlag(ApplicationFlags.FullScreen) && header.application_flags.HasFlag(ApplicationFlags.GUICompatible))
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))
sb.AppendLine("\tApplication uses Windows");
sb.AppendFormat("\tReturn thunks are at: {0}", header.return_thunks_offset).AppendLine();
sb.AppendFormat("\tSegment reference thunks are at: {0}", header.segment_reference_thunks).AppendLine();
}
else
{
sb.AppendFormat("\tApplication for unknown OS {0}", (byte)header.target_os).AppendLine();
sb.AppendFormat("\tApplication requires OS {0}.{1} to run", header.os_major, header.os_minor).AppendLine();
sb.AppendFormat("\tReturn thunks are at: {0}", header.return_thunks_offset).AppendLine();
sb.AppendFormat("\tSegment reference thunks are at: {0}", header.segment_reference_thunks).AppendLine();
}
else if(header.target_os == TargetOS.Unknown)
sb.AppendLine("\tApplication requires Windows 1.0 to run");
if(header.application_flags.HasFlag(ApplicationFlags.FullScreen) &&
!header.application_flags.HasFlag(ApplicationFlags.GUICompatible))
sb.AppendLine("\tApplication is full screen, unaware of Windows");
else if(!header.application_flags.HasFlag(ApplicationFlags.FullScreen) &&
header.application_flags.HasFlag(ApplicationFlags.GUICompatible))
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))
sb.AppendLine("\tApplication uses Windows");
sb.AppendFormat("\tReturn thunks are at: {0}", header.return_thunks_offset).AppendLine();
sb.AppendFormat("\tSegment reference thunks are at: {0}", header.segment_reference_thunks).AppendLine();
}
else if(header.target_os == TargetOS.DOS)
{
sb.AppendLine("\tDOS application");
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))
sb.AppendLine("\tApplication is full screen, unaware of Windows");
else if(!header.application_flags.HasFlag(ApplicationFlags.FullScreen) &&
header.application_flags.HasFlag(ApplicationFlags.GUICompatible))
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))
sb.AppendLine("\tApplication uses Windows");
sb.AppendFormat("\tReturn thunks are at: {0}", header.return_thunks_offset).AppendLine();
sb.AppendFormat("\tSegment reference thunks are at: {0}", header.segment_reference_thunks).AppendLine();
}
else if(header.target_os == TargetOS.Borland)
{
sb.AppendLine("\tBorland Operating System Services application");
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))
sb.AppendLine("\tApplication is full screen, unaware of Windows");
else if(!header.application_flags.HasFlag(ApplicationFlags.FullScreen) &&
header.application_flags.HasFlag(ApplicationFlags.GUICompatible))
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))
sb.AppendLine("\tApplication uses Windows");
sb.AppendFormat("\tReturn thunks are at: {0}", header.return_thunks_offset).AppendLine();
sb.AppendFormat("\tSegment reference thunks are at: {0}", header.segment_reference_thunks).AppendLine();
}
else
{
sb.AppendFormat("\tApplication for unknown OS {0}", (byte)header.target_os).AppendLine();
sb.AppendFormat("\tApplication requires OS {0}.{1} to run", header.os_major, header.os_minor)
.AppendLine();
sb.AppendFormat("\tReturn thunks are at: {0}", header.return_thunks_offset).AppendLine();
sb.AppendFormat("\tSegment reference thunks are at: {0}", header.segment_reference_thunks).AppendLine();
}
if (header.application_flags.HasFlag(ApplicationFlags.Errors))
sb.AppendLine("\tExecutable has errors");
if (header.application_flags.HasFlag(ApplicationFlags.NonConforming))
sb.AppendLine("\tExecutable is non conforming");
if (header.application_flags.HasFlag(ApplicationFlags.DLL))
sb.AppendLine("\tExecutable is a dynamic library or a driver");
if(header.application_flags.HasFlag(ApplicationFlags.Errors)) sb.AppendLine("\tExecutable has errors");
if(header.application_flags.HasFlag(ApplicationFlags.NonConforming))
sb.AppendLine("\tExecutable is non conforming");
if(header.application_flags.HasFlag(ApplicationFlags.DLL))
sb.AppendLine("\tExecutable is a dynamic library or a driver");
sb.AppendFormat("\tMinimum code swap area: {0} bytes", header.minimum_swap_area).AppendLine();
sb.AppendFormat("\tFile alignment shift: {0}", 512 << header.alignment_shift).AppendLine();
sb.AppendFormat("\tInitial local heap should be {0} bytes", header.initial_heap).AppendLine();
sb.AppendFormat("\tInitial stack size should be {0} bytes", header.initial_stack).AppendLine();
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))
sb.AppendFormat("\tSS:SP initial stack pointer: {0:X4}:{1:X4}", (header.stack_pointer & 0xFFFF0000) >> 16, header.stack_pointer & 0xFFFF).AppendLine();
sb.AppendFormat("\tEntry table starts at {0} and runs for {1} bytes", header.entry_table_offset, header.entry_table_length).AppendLine();
sb.AppendFormat("\tSegment table starts at {0} and contain {1} segments", header.segment_table_offset, header.segment_count).AppendLine();
sb.AppendFormat("\tModule reference table starts at {0} and contain {1} references", header.module_reference_offset, header.reference_count).AppendLine();
sb.AppendFormat("\tNon-resident names table starts at {0} and runs for {1} bytes", header.nonresident_names_offset, header.nonresident_table_size).AppendLine();
sb.AppendFormat("\tResources table starts at {0} and contains {1} entries", header.resource_table_offset, header.resource_entries).AppendLine();
sb.AppendFormat("\tResident names table starts at {0}", header.resident_names_offset).AppendLine();
sb.AppendFormat("\tImported names table starts at {0}", header.imported_names_offset).AppendLine();
sb.AppendFormat("\tMinimum code swap area: {0} bytes", header.minimum_swap_area).AppendLine();
sb.AppendFormat("\tFile alignment shift: {0}", 512 << header.alignment_shift).AppendLine();
sb.AppendFormat("\tInitial local heap should be {0} bytes", header.initial_heap).AppendLine();
sb.AppendFormat("\tInitial stack size should be {0} bytes", header.initial_stack).AppendLine();
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))
sb.AppendFormat("\tSS:SP initial stack pointer: {0:X4}:{1:X4}",
(header.stack_pointer & 0xFFFF0000) >> 16, header.stack_pointer & 0xFFFF).AppendLine();
sb.AppendFormat("\tEntry table starts at {0} and runs for {1} bytes", header.entry_table_offset,
header.entry_table_length).AppendLine();
sb.AppendFormat("\tSegment table starts at {0} and contain {1} segments", header.segment_table_offset,
header.segment_count).AppendLine();
sb.AppendFormat("\tModule reference table starts at {0} and contain {1} references",
header.module_reference_offset, header.reference_count).AppendLine();
sb.AppendFormat("\tNon-resident names table starts at {0} and runs for {1} bytes",
header.nonresident_names_offset, header.nonresident_table_size).AppendLine();
sb.AppendFormat("\tResources table starts at {0} and contains {1} entries", header.resource_table_offset,
header.resource_entries).AppendLine();
sb.AppendFormat("\tResident names table starts at {0}", header.resident_names_offset).AppendLine();
sb.AppendFormat("\tImported names table starts at {0}", header.imported_names_offset).AppendLine();
return sb.ToString();
}
}
public string GetInfo()
{
@@ -172,92 +198,92 @@ namespace libexeinfo
}
public static ResourceTable GetResources(FileStream stream, uint neStart, ushort tableOff)
{
long oldPosition = stream.Position;
byte[] DW = new byte[2];
byte[] DD = new byte[4];
{
long oldPosition = stream.Position;
byte[] DW = new byte[2];
byte[] DD = new byte[4];
stream.Position = neStart + tableOff;
ResourceTable table = new ResourceTable();
stream.Read(DW, 0, 2);
table.alignment_shift = BitConverter.ToUInt16(DW, 0);
stream.Position = neStart + tableOff;
ResourceTable table = new ResourceTable();
stream.Read(DW, 0, 2);
table.alignment_shift = BitConverter.ToUInt16(DW, 0);
List<ResourceType> types = new List<ResourceType>();
List<ResourceType> types = new List<ResourceType>();
while (true)
{
ResourceType type = new ResourceType();
stream.Read(DW, 0, 2);
type.id = BitConverter.ToUInt16(DW, 0);
if (type.id == 0)
break;
while(true)
{
ResourceType type = new ResourceType();
stream.Read(DW, 0, 2);
type.id = BitConverter.ToUInt16(DW, 0);
if(type.id == 0) break;
stream.Read(DW, 0, 2);
type.count = BitConverter.ToUInt16(DW, 0);
stream.Read(DD, 0, 4);
type.reserved = BitConverter.ToUInt32(DD, 0);
stream.Read(DW, 0, 2);
type.count = BitConverter.ToUInt16(DW, 0);
stream.Read(DD, 0, 4);
type.reserved = BitConverter.ToUInt32(DD, 0);
type.resources = new Resource[type.count];
for (int i = 0; i < type.count; i++)
{
type.resources[i] = new Resource();
stream.Read(DW, 0, 2);
type.resources[i].dataOffset = BitConverter.ToUInt16(DW, 0);
stream.Read(DW, 0, 2);
type.resources[i].length = BitConverter.ToUInt16(DW, 0);
stream.Read(DW, 0, 2);
type.resources[i].flags = (ResourceFlags)BitConverter.ToUInt16(DW, 0);
stream.Read(DW, 0, 2);
type.resources[i].id = BitConverter.ToUInt16(DW, 0);
stream.Read(DD, 0, 4);
type.resources[i].reserved = BitConverter.ToUInt32(DD, 0);
}
type.resources = new Resource[type.count];
for(int i = 0; i < type.count; i++)
{
type.resources[i] = new Resource();
stream.Read(DW, 0, 2);
type.resources[i].dataOffset = BitConverter.ToUInt16(DW, 0);
stream.Read(DW, 0, 2);
type.resources[i].length = BitConverter.ToUInt16(DW, 0);
stream.Read(DW, 0, 2);
type.resources[i].flags = (ResourceFlags)BitConverter.ToUInt16(DW, 0);
stream.Read(DW, 0, 2);
type.resources[i].id = BitConverter.ToUInt16(DW, 0);
stream.Read(DD, 0, 4);
type.resources[i].reserved = BitConverter.ToUInt32(DD, 0);
}
types.Add(type);
System.Console.WriteLine("{0}", stream.Position);
}
types.Add(type);
Console.WriteLine("{0}", stream.Position);
}
table.types = types.ToArray();
table.types = types.ToArray();
for (int t = 0; t < table.types.Length; t++)
{
if ((table.types[t].id & 0x8000) == 0)
{
byte len;
byte[] str;
stream.Position = neStart + tableOff + table.types[t].id;
len = (byte)stream.ReadByte();
str = new byte[len];
stream.Read(str, 0, len);
table.types[t].name = Encoding.ASCII.GetString(str);
}
else
table.types[t].name = ResourceIdToName(table.types[t].id);
for(int t = 0; t < table.types.Length; t++)
{
if((table.types[t].id & 0x8000) == 0)
{
byte len;
byte[] str;
stream.Position = neStart + tableOff + table.types[t].id;
len = (byte)stream.ReadByte();
str = new byte[len];
stream.Read(str, 0, len);
table.types[t].name = Encoding.ASCII.GetString(str);
}
else table.types[t].name = ResourceIdToName(table.types[t].id);
for (int r = 0; r < table.types[t].resources.Length; r++)
{
if ((table.types[t].resources[r].id & 0x8000) == 0)
{
byte len;
byte[] str;
stream.Position = neStart + tableOff + table.types[t].resources[r].id;
len = (byte)stream.ReadByte();
str = new byte[len];
stream.Read(str, 0, len);
table.types[t].resources[r].name = Encoding.ASCII.GetString(str);
}
else
table.types[t].resources[r].name = string.Format("{0}", table.types[t].resources[r].id & 0x7FFF);
for(int r = 0; r < table.types[t].resources.Length; r++)
{
if((table.types[t].resources[r].id & 0x8000) == 0)
{
byte len;
byte[] str;
stream.Position = neStart + tableOff + table.types[t].resources[r].id;
len = (byte)stream.ReadByte();
str = new byte[len];
stream.Read(str, 0, len);
table.types[t].resources[r].name = Encoding.ASCII.GetString(str);
}
else
table.types[t].resources[r].name =
string.Format("{0}", table.types[t].resources[r].id & 0x7FFF);
table.types[t].resources[r].data = new byte[table.types[t].resources[r].length * (1 << table.alignment_shift)];
stream.Position = table.types[t].resources[r].dataOffset * (1 << table.alignment_shift);
stream.Read(table.types[t].resources[r].data, 0, table.types[t].resources[r].data.Length);
}
}
table.types[t].resources[r].data =
new byte[table.types[t].resources[r].length * (1 << table.alignment_shift)];
stream.Position = table.types[t].resources[r].dataOffset * (1 << table.alignment_shift);
stream.Read(table.types[t].resources[r].data, 0, table.types[t].resources[r].data.Length);
}
}
stream.Position = oldPosition;
stream.Position = oldPosition;
return table;
}
}
return table;
}
}
}

View File

@@ -23,6 +23,7 @@
// 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;
@@ -30,144 +31,138 @@ using System.Runtime.InteropServices;
namespace libexeinfo
{
/// <summary>
/// Represents a Microsoft New Executable
/// Represents a Microsoft New Executable
/// </summary>
public partial class NE
{
/// <summary>
/// The <see cref="FileStream"/> that contains the executable represented by this instance
/// </summary>
public readonly FileStream BaseStream;
/// <summary>
/// Header for this executable
/// </summary>
public readonly NEHeader Header;
/// <summary>
/// If true this instance correctly represents a Microsoft New Executable
/// </summary>
public readonly bool IsNE;
public readonly MZ BaseExecutable;
public readonly MZ BaseExecutable;
/// <summary>
/// The <see cref="FileStream" /> that contains the executable represented by this instance
/// </summary>
public readonly FileStream BaseStream;
/// <summary>
/// Header for this executable
/// </summary>
public readonly NEHeader Header;
/// <summary>
/// If true this instance correctly represents a Microsoft New Executable
/// </summary>
public readonly bool IsNE;
public readonly ResourceTable Resources;
public readonly Version[] Versions;
public readonly Version[] Versions;
/// <summary>
/// Initializes a new instance of the <see cref="T:libexeinfo.NE"/> class.
/// </summary>
/// <param name="path">Executable path.</param>
public NE(string path)
{
IsNE = false;
BaseStream = File.Open(path, FileMode.Open, FileAccess.Read);
/// <summary>
/// Initializes a new instance of the <see cref="T:libexeinfo.NE" /> class.
/// </summary>
/// <param name="path">Executable path.</param>
public NE(string path)
{
IsNE = false;
BaseStream = File.Open(path, FileMode.Open, FileAccess.Read);
BaseExecutable = new MZ(BaseStream);
if(BaseExecutable.IsMZ)
{
if(BaseExecutable.Header.new_offset < BaseStream.Length)
if(BaseExecutable.Header.new_offset < BaseStream.Length)
{
BaseStream.Seek(BaseExecutable.Header.new_offset, SeekOrigin.Begin);
byte[] buffer = new byte[Marshal.SizeOf(typeof(NEHeader))];
BaseStream.Read(buffer, 0, buffer.Length);
IntPtr hdrPtr = Marshal.AllocHGlobal(buffer.Length);
Marshal.Copy(buffer, 0, hdrPtr, buffer.Length);
BaseStream.Seek(BaseExecutable.Header.new_offset, SeekOrigin.Begin);
byte[] buffer = new byte[Marshal.SizeOf(typeof(NEHeader))];
BaseStream.Read(buffer, 0, buffer.Length);
IntPtr hdrPtr = Marshal.AllocHGlobal(buffer.Length);
Marshal.Copy(buffer, 0, hdrPtr, buffer.Length);
Header = (NEHeader)Marshal.PtrToStructure(hdrPtr, typeof(NEHeader));
Marshal.FreeHGlobal(hdrPtr);
Marshal.FreeHGlobal(hdrPtr);
if(Header.signature == Signature)
{
IsNE = true;
if (Header.resource_entries > 0)
{
Resources = GetResources(BaseStream, BaseExecutable.Header.new_offset, Header.resource_table_offset);
Versions = GetVersions().ToArray();
}
}
}
}
}
/// <summary>
/// Initializes a new instance of the <see cref="T:libexeinfo.NE"/> class.
/// </summary>
/// <param name="stream">Stream containing the executable.</param>
public NE(FileStream stream)
{
IsNE = false;
BaseStream = stream;
BaseExecutable = new MZ(BaseStream);
if (BaseExecutable.IsMZ)
{
if (BaseExecutable.Header.new_offset < BaseStream.Length)
{
BaseStream.Seek(BaseExecutable.Header.new_offset, SeekOrigin.Begin);
byte[] buffer = new byte[Marshal.SizeOf(typeof(NEHeader))];
BaseStream.Read(buffer, 0, buffer.Length);
IntPtr hdrPtr = Marshal.AllocHGlobal(buffer.Length);
Marshal.Copy(buffer, 0, hdrPtr, buffer.Length);
Header = (NEHeader)Marshal.PtrToStructure(hdrPtr, typeof(NEHeader));
Marshal.FreeHGlobal(hdrPtr);
if (Header.signature == Signature)
{
IsNE = true;
if (Header.resource_entries > 0)
if(Header.resource_entries > 0)
{
Resources = GetResources(BaseStream, BaseExecutable.Header.new_offset, Header.resource_table_offset);
Resources = GetResources(BaseStream, BaseExecutable.Header.new_offset,
Header.resource_table_offset);
Versions = GetVersions().ToArray();
}
}
}
}
}
}
}
}
/// <summary>
/// Identifies if the specified executable is a Microsoft New Executable
/// </summary>
/// <returns><c>true</c> if the specified executable is a Microsoft New Executable, <c>false</c> otherwise.</returns>
/// <param name="path">Executable path.</param>
public static bool Identify(string path)
{
FileStream BaseStream = File.Open(path, FileMode.Open, FileAccess.Read);
MZ BaseExecutable = new MZ(BaseStream);
if (BaseExecutable.IsMZ)
{
if (BaseExecutable.Header.new_offset < BaseStream.Length)
{
BaseStream.Seek(BaseExecutable.Header.new_offset, SeekOrigin.Begin);
byte[] buffer = new byte[Marshal.SizeOf(typeof(NEHeader))];
BaseStream.Read(buffer, 0, buffer.Length);
IntPtr hdrPtr = Marshal.AllocHGlobal(buffer.Length);
Marshal.Copy(buffer, 0, hdrPtr, buffer.Length);
NEHeader Header = (NEHeader)Marshal.PtrToStructure(hdrPtr, typeof(NEHeader));
Marshal.FreeHGlobal(hdrPtr);
return Header.signature == Signature;
}
}
/// <summary>
/// Initializes a new instance of the <see cref="T:libexeinfo.NE" /> class.
/// </summary>
/// <param name="stream">Stream containing the executable.</param>
public NE(FileStream stream)
{
IsNE = false;
BaseStream = stream;
BaseExecutable = new MZ(BaseStream);
if(BaseExecutable.IsMZ)
if(BaseExecutable.Header.new_offset < BaseStream.Length)
{
BaseStream.Seek(BaseExecutable.Header.new_offset, SeekOrigin.Begin);
byte[] buffer = new byte[Marshal.SizeOf(typeof(NEHeader))];
BaseStream.Read(buffer, 0, buffer.Length);
IntPtr hdrPtr = Marshal.AllocHGlobal(buffer.Length);
Marshal.Copy(buffer, 0, hdrPtr, buffer.Length);
Header = (NEHeader)Marshal.PtrToStructure(hdrPtr, typeof(NEHeader));
Marshal.FreeHGlobal(hdrPtr);
if(Header.signature == Signature)
{
IsNE = true;
if(Header.resource_entries > 0)
{
Resources = GetResources(BaseStream, BaseExecutable.Header.new_offset,
Header.resource_table_offset);
Versions = GetVersions().ToArray();
}
}
}
}
/// <summary>
/// Identifies if the specified executable is a Microsoft New Executable
/// </summary>
/// <returns><c>true</c> if the specified executable is a Microsoft New Executable, <c>false</c> otherwise.</returns>
/// <param name="path">Executable path.</param>
public static bool Identify(string path)
{
FileStream BaseStream = File.Open(path, FileMode.Open, FileAccess.Read);
MZ BaseExecutable = new MZ(BaseStream);
if(BaseExecutable.IsMZ)
if(BaseExecutable.Header.new_offset < BaseStream.Length)
{
BaseStream.Seek(BaseExecutable.Header.new_offset, SeekOrigin.Begin);
byte[] buffer = new byte[Marshal.SizeOf(typeof(NEHeader))];
BaseStream.Read(buffer, 0, buffer.Length);
IntPtr hdrPtr = Marshal.AllocHGlobal(buffer.Length);
Marshal.Copy(buffer, 0, hdrPtr, buffer.Length);
NEHeader Header = (NEHeader)Marshal.PtrToStructure(hdrPtr, typeof(NEHeader));
Marshal.FreeHGlobal(hdrPtr);
return Header.signature == Signature;
}
return false;
}
}
/// <summary>
/// Identifies if the specified executable is a Microsoft New Executable
/// </summary>
/// <returns><c>true</c> if the specified executable is a Microsoft New Executable, <c>false</c> otherwise.</returns>
/// <param name="stream">Stream containing the executable.</param>
public static bool Identify(FileStream stream)
{
FileStream BaseStream = stream;
MZ BaseExecutable = new MZ(BaseStream);
if (BaseExecutable.IsMZ)
{
if (BaseExecutable.Header.new_offset < BaseStream.Length)
{
BaseStream.Seek(BaseExecutable.Header.new_offset, SeekOrigin.Begin);
byte[] buffer = new byte[Marshal.SizeOf(typeof(NEHeader))];
BaseStream.Read(buffer, 0, buffer.Length);
IntPtr hdrPtr = Marshal.AllocHGlobal(buffer.Length);
Marshal.Copy(buffer, 0, hdrPtr, buffer.Length);
NEHeader Header = (NEHeader)Marshal.PtrToStructure(hdrPtr, typeof(NEHeader));
Marshal.FreeHGlobal(hdrPtr);
return Header.signature == Signature;
}
}
/// <summary>
/// Identifies if the specified executable is a Microsoft New Executable
/// </summary>
/// <returns><c>true</c> if the specified executable is a Microsoft New Executable, <c>false</c> otherwise.</returns>
/// <param name="stream">Stream containing the executable.</param>
public static bool Identify(FileStream stream)
{
FileStream BaseStream = stream;
MZ BaseExecutable = new MZ(BaseStream);
if(BaseExecutable.IsMZ)
if(BaseExecutable.Header.new_offset < BaseStream.Length)
{
BaseStream.Seek(BaseExecutable.Header.new_offset, SeekOrigin.Begin);
byte[] buffer = new byte[Marshal.SizeOf(typeof(NEHeader))];
BaseStream.Read(buffer, 0, buffer.Length);
IntPtr hdrPtr = Marshal.AllocHGlobal(buffer.Length);
Marshal.Copy(buffer, 0, hdrPtr, buffer.Length);
NEHeader Header = (NEHeader)Marshal.PtrToStructure(hdrPtr, typeof(NEHeader));
Marshal.FreeHGlobal(hdrPtr);
return Header.signature == Signature;
}
return false;
}
}
return false;
}
}
}

View File

@@ -24,7 +24,6 @@
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
// THE SOFTWARE.
using System;
using System.Runtime.InteropServices;
namespace libexeinfo
@@ -32,62 +31,62 @@ namespace libexeinfo
public partial class NE
{
/// <summary>
/// Header for a Microsoft New Executable
/// Header for a Microsoft New Executable
/// </summary>
[StructLayout(LayoutKind.Sequential/*, Pack = 2*/)]
[StructLayout(LayoutKind.Sequential /*, Pack = 2*/)]
public struct NEHeader
{
public ushort signature;
public byte linker_major;
public byte linker_minor;
public ushort entry_table_offset;
public ushort entry_table_length;
public uint crc;
public ProgramFlags program_flags;
public ushort signature;
public byte linker_major;
public byte linker_minor;
public ushort entry_table_offset;
public ushort entry_table_length;
public uint crc;
public ProgramFlags program_flags;
public ApplicationFlags application_flags;
public byte auto_data_segment_index;
public ushort initial_heap;
public ushort initial_stack;
public uint entry_point;
public uint stack_pointer;
public ushort segment_count;
public ushort reference_count;
public ushort nonresident_table_size;
public ushort segment_table_offset;
public ushort resource_table_offset;
public ushort resident_names_offset;
public ushort module_reference_offset;
public ushort imported_names_offset;
public uint nonresident_names_offset;
public ushort movable_entries;
public ushort alignment_shift;
public ushort resource_entries;
public TargetOS target_os;
public OS2Flags os2_flags;
public ushort return_thunks_offset;
public ushort segment_reference_thunks;
public ushort minimum_swap_area;
public byte os_minor;
public byte os_major;
public byte auto_data_segment_index;
public ushort initial_heap;
public ushort initial_stack;
public uint entry_point;
public uint stack_pointer;
public ushort segment_count;
public ushort reference_count;
public ushort nonresident_table_size;
public ushort segment_table_offset;
public ushort resource_table_offset;
public ushort resident_names_offset;
public ushort module_reference_offset;
public ushort imported_names_offset;
public uint nonresident_names_offset;
public ushort movable_entries;
public ushort alignment_shift;
public ushort resource_entries;
public TargetOS target_os;
public OS2Flags os2_flags;
public ushort return_thunks_offset;
public ushort segment_reference_thunks;
public ushort minimum_swap_area;
public byte os_minor;
public byte os_major;
}
/// <summary>
/// Resource table
/// Resource table
/// </summary>
public struct ResourceTable
{
public ushort alignment_shift;
public ushort alignment_shift;
public ResourceType[] types;
}
/// <summary>
/// Resource type
/// Resource type
/// </summary>
public struct ResourceType
{
public ushort id;
public ushort count;
public uint reserved;
public ushort id;
public ushort count;
public uint reserved;
public Resource[] resources;
// Not sequentially stored
@@ -95,15 +94,15 @@ namespace libexeinfo
}
/// <summary>
/// Resource
/// Resource
/// </summary>
public struct Resource
{
public ushort dataOffset;
public ushort length;
public ushort dataOffset;
public ushort length;
public ResourceFlags flags;
public ushort id;
public uint reserved;
public ushort id;
public uint reserved;
// Not sequentially stored
public string name;
@@ -111,36 +110,36 @@ namespace libexeinfo
}
/// <summary>
/// Node in a version resource
/// Node in a version resource
/// </summary>
class VersionNode
{
public ushort cbNode;
public ushort cbData;
public string szName;
public byte[] rgbData;
public ushort cbData;
public ushort cbNode;
public VersionNode[] children;
public byte[] rgbData;
public string szName;
}
/// <summary>
/// Fixed file version info
/// Fixed file version info
/// </summary>
[StructLayout(LayoutKind.Sequential)]
public class FixedFileInfo
{
public uint dwFileDateLS;
public uint dwFileDateMS;
public uint dwFileFlags;
public uint dwFileFlagsMask;
public uint dwFileOS;
public uint dwFileSubtype;
public uint dwFileType;
public uint dwFileVersionLS;
public uint dwFileVersionMS;
public uint dwProductVersionLS;
public uint dwProductVersionMS;
public uint dwSignature;
public uint dwStrucVersion;
public uint dwFileVersionMS;
public uint dwFileVersionLS;
public uint dwProductVersionMS;
public uint dwProductVersionLS;
public uint dwFileFlagsMask;
public uint dwFileFlags;
public uint dwFileOS;
public uint dwFileType;
public uint dwFileSubtype;
public uint dwFileDateMS;
public uint dwFileDateLS;
}
}
}

View File

@@ -35,152 +35,39 @@ namespace libexeinfo
public partial class NE
{
/// <summary>
/// Gets all the version resources from this instance
/// Gets all the version resources from this instance
/// </summary>
/// <returns>The decoded version resources.</returns>
public List<Version> GetVersions()
{
List<Version> versions = new List<Version>();
foreach (ResourceType type in Resources.types)
{
if ((type.id & 0x7FFF) == (int)ResourceTypes.RT_VERSION)
{
foreach (Resource resource in type.resources)
foreach(ResourceType type in Resources.types)
if((type.id & 0x7FFF) == (int)ResourceTypes.RT_VERSION)
foreach(Resource resource in type.resources)
{
Version vers = new Version(resource.data, resource.name);
versions.Add(vers);
}
}
}
return versions;
}
/// <summary>
/// Represents a version ("RT_VERSION") resource
/// Represents a version ("RT_VERSION") resource
/// </summary>
public class Version
{
/// <summary>
/// This contains a list of all name=value strings pairs sorted by language
/// </summary>
/// <value>List of all name=value strings pairs sorted by language.</value>
public Dictionary<string, Dictionary<string, string>> StringsByLanguage { get; }
string fileVersion;
string productVersion;
VersionFileFlags fileFlags;
VersionFileOS fileOS;
VersionFileType fileType;
VersionFileSubtype fileSubtype;
DateTime fileDate;
string name;
/// <summary>
/// File version.
/// </summary>
/// <value>The file version.</value>
public string FileVersion
{
get
{
return fileVersion;
}
}
/// <summary>
/// Product version.
/// </summary>
/// <value>The product version.</value>
public string ProductVersion
{
get
{
return productVersion;
}
}
/// <summary>
/// File flags.
/// </summary>
/// <value>The file flags.</value>
public VersionFileFlags FileFlags
{
get
{
return fileFlags;
}
}
/// <summary>
/// File operating system.
/// </summary>
/// <value>The file operating system.</value>
public VersionFileOS FileOS
{
get
{
return fileOS;
}
}
/// <summary>
/// File type.
/// </summary>
/// <value>The type of the file.</value>
public VersionFileType FileType
{
get
{
return fileType;
}
}
/// <summary>
/// File subtype.
/// </summary>
/// <value>The file subtype.</value>
public VersionFileSubtype FileSubtype
{
get
{
return fileSubtype;
}
}
/// <summary>
/// File date.
/// </summary>
/// <value>The file date.</value>
public DateTime FileDate
{
get
{
return fileDate;
}
}
/// <summary>
/// Resource name
/// </summary>
/// <value>The resource name.</value>
public string Name
{
get { return name; }
}
/// <summary>
/// Initializes a new instance of the <see cref="T:libexeinfo.NE.Version"/> class.
/// Initializes a new instance of the <see cref="T:libexeinfo.NE.Version" /> class.
/// </summary>
/// <param name="data">Resource data.</param>
/// <param name="resourceName">Resource name.</param>
public Version(byte[] data, string resourceName = null)
{
if (data == null || data.Length < 5)
return;
if(data == null || data.Length < 5) return;
name = resourceName;
Name = resourceName;
StringsByLanguage = new Dictionary<string, Dictionary<string, string>>();
@@ -188,6 +75,60 @@ namespace libexeinfo
DecodeNode(root, null, null);
}
/// <summary>
/// This contains a list of all name=value strings pairs sorted by language
/// </summary>
/// <value>List of all name=value strings pairs sorted by language.</value>
public Dictionary<string, Dictionary<string, string>> StringsByLanguage { get; }
/// <summary>
/// File version.
/// </summary>
/// <value>The file version.</value>
public string FileVersion { get; set; }
/// <summary>
/// Product version.
/// </summary>
/// <value>The product version.</value>
public string ProductVersion { get; set; }
/// <summary>
/// File flags.
/// </summary>
/// <value>The file flags.</value>
public VersionFileFlags FileFlags { get; set; }
/// <summary>
/// File operating system.
/// </summary>
/// <value>The file operating system.</value>
public VersionFileOS FileOS { get; set; }
/// <summary>
/// File type.
/// </summary>
/// <value>The type of the file.</value>
public VersionFileType FileType { get; set; }
/// <summary>
/// File subtype.
/// </summary>
/// <value>The file subtype.</value>
public VersionFileSubtype FileSubtype { get; set; }
/// <summary>
/// File date.
/// </summary>
/// <value>The file date.</value>
public DateTime FileDate { get; set; }
/// <summary>
/// Resource name
/// </summary>
/// <value>The resource name.</value>
public string Name { get; }
VersionNode GetNode(byte[] data, int startPosition, out int nodeLength)
{
nodeLength = 0;
@@ -200,240 +141,196 @@ namespace libexeinfo
nodeLength += 4;
MemoryStream nameMs = new MemoryStream();
while (data[startPosition + nodeLength] > 0)
while(data[startPosition + nodeLength] > 0)
{
nameMs.WriteByte(data[startPosition + nodeLength]);
nodeLength++;
}
node.szName = Encoding.ASCII.GetString(nameMs.ToArray());
nodeLength++;
if (nodeLength % 4 > 0)
nodeLength += 4 - (nodeLength % 4);
if(nodeLength % 4 > 0) nodeLength += 4 - nodeLength % 4;
node.rgbData = new byte[node.cbData];
Array.Copy(data, startPosition + nodeLength, node.rgbData, 0, node.cbData);
nodeLength += node.cbData;
if (nodeLength % 4 > 0)
nodeLength += 4 - (nodeLength % 4);
nodeLength += node.cbData;
if(nodeLength % 4 > 0) nodeLength += 4 - nodeLength % 4;
List<VersionNode> children = new List<VersionNode>();
while (nodeLength < node.cbNode)
while(nodeLength < node.cbNode)
{
children.Add(GetNode(data, startPosition + nodeLength, out int childLength));
nodeLength += childLength;
}
if (children.Count > 0)
node.children = children.ToArray();
if(children.Count > 0) node.children = children.ToArray();
return node;
}
void DecodeNode(VersionNode node, string parent, string grandparent)
{
if (node.szName == FixedFileInfoSig)
if(node.szName == FixedFileInfoSig)
{
IntPtr infoPtr = Marshal.AllocHGlobal(node.cbData);
Marshal.Copy(node.rgbData, 0, infoPtr, node.cbData);
FixedFileInfo info = (FixedFileInfo)Marshal.PtrToStructure(infoPtr, typeof(FixedFileInfo));
Marshal.FreeHGlobal(infoPtr);
fileVersion = string.Format("{0}.{1:D2}.{2}.{3}", (info.dwFileVersionMS & 0xFFFF0000) >> 16, info.dwFileVersionMS & 0xFFFF,
(info.dwFileVersionLS & 0xFFFF0000) >> 16, info.dwFileVersionLS & 0xFFFF);
productVersion = string.Format("{0}.{1:D2}.{2}.{3}", (info.dwProductVersionMS & 0xFFFF0000) >> 16, info.dwProductVersionMS & 0xFFFF,
(info.dwProductVersionLS & 0xFFFF0000) >> 16, info.dwProductVersionLS & 0xFFFF);
fileFlags = (VersionFileFlags)(info.dwFileFlags & info.dwFileFlagsMask);
fileOS = (VersionFileOS)info.dwFileOS;
fileType = (VersionFileType)info.dwFileType;
fileSubtype = (VersionFileSubtype)info.dwFileSubtype;
fileDate = DateTime.FromFileTime(info.dwFileDateMS * 0x100000000 + info.dwFileDateLS);
FileVersion = string.Format("{0}.{1:D2}.{2}.{3}", (info.dwFileVersionMS & 0xFFFF0000) >> 16,
info.dwFileVersionMS & 0xFFFF,
(info.dwFileVersionLS & 0xFFFF0000) >> 16,
info.dwFileVersionLS & 0xFFFF);
ProductVersion = string.Format("{0}.{1:D2}.{2}.{3}", (info.dwProductVersionMS & 0xFFFF0000) >> 16,
info.dwProductVersionMS & 0xFFFF,
(info.dwProductVersionLS & 0xFFFF0000) >> 16,
info.dwProductVersionLS & 0xFFFF);
FileFlags =
(VersionFileFlags)(info.dwFileFlags & info.dwFileFlagsMask);
FileOS = (VersionFileOS)info.dwFileOS;
FileType = (VersionFileType)info.dwFileType;
FileSubtype = (VersionFileSubtype)info.dwFileSubtype;
FileDate = DateTime.FromFileTime(info.dwFileDateMS * 0x100000000 + info.dwFileDateLS);
}
if (parent == StringFileInfo)
if(parent == StringFileInfo)
{
Dictionary<string, string> strings = new Dictionary<string, string>();
StringsByLanguage.Add(node.szName, strings);
}
if (grandparent == StringFileInfo)
{
if (StringsByLanguage.TryGetValue(parent, out Dictionary<string, string> strings))
if(grandparent == StringFileInfo)
if(StringsByLanguage.TryGetValue(parent, out Dictionary<string, string> strings))
{
Encoding encoding;
try
{
encoding = Encoding.GetEncoding(Convert.ToInt32(parent.Substring(4), 16));
}
catch
{
encoding = Encoding.ASCII;
}
try { encoding = Encoding.GetEncoding(Convert.ToInt32(parent.Substring(4), 16)); }
catch { encoding = Encoding.ASCII; }
strings.Add(node.szName, encoding.GetString(node.rgbData));
}
}
if (node.children != null)
{
for (int i = 0; i < node.children.Length; i++)
if(node.children != null)
for(int i = 0; i < node.children.Length; i++)
DecodeNode(node.children[i], node.szName, parent);
}
/// <summary>
/// Converts a <see cref="VersionFileType" /> to string
/// </summary>
/// <returns>The string.</returns>
/// <param name="type">
/// <see cref="VersionFileType" />
/// </param>
public static string TypeToString(VersionFileType type)
{
switch(type)
{
case VersionFileType.VFT_APP: return "Application";
case VersionFileType.VFT_DLL: return "Dynamic-link library";
case VersionFileType.VFT_DRV: return "Device driver";
case VersionFileType.VFT_FONT: return "Font";
case VersionFileType.VFT_STATIC_LIB: return "Static-link library";
case VersionFileType.VFT_UNKNOWN: return "Unknown";
case VersionFileType.VFT_VXD: return "Virtual device";
default: return string.Format("Unknown type code {0}", (uint)type);
}
}
/// <summary>
/// Converts a <see cref="VersionFileType"/> to string
/// </summary>
/// <returns>The string.</returns>
/// <param name="type"><see cref="VersionFileType"/></param>
public static string TypeToString(VersionFileType type)
/// <summary>
/// Converts a <see cref="VersionFileSubtype" /> to string, considering file type to be a driver
/// </summary>
/// <returns>The string.</returns>
/// <param name="subtype">
/// <see cref="VersionFileSubtype" />
/// </param>
public static string DriverToString(VersionFileSubtype subtype)
{
switch (type)
switch(subtype)
{
case VersionFileType.VFT_APP:
return "Application";
case VersionFileType.VFT_DLL:
return "Dynamic-link library";
case VersionFileType.VFT_DRV:
return "Device driver";
case VersionFileType.VFT_FONT:
return "Font";
case VersionFileType.VFT_STATIC_LIB:
return "Static-link library";
case VersionFileType.VFT_UNKNOWN:
return "Unknown";
case VersionFileType.VFT_VXD:
return "Virtual device";
default:
return string.Format("Unknown type code {0}", (uint)type);
}
}
/// <summary>
/// Converts a <see cref="VersionFileSubtype"/> to string, considering file type to be a driver
/// </summary>
/// <returns>The string.</returns>
/// <param name="subtype"><see cref="VersionFileSubtype"/></param>
public static string DriverToString(VersionFileSubtype subtype)
{
switch (subtype)
{
case VersionFileSubtype.VFT2_DRV_COMM:
return "Communications";
case VersionFileSubtype.VFT2_DRV_DISPLAY:
return "Display";
case VersionFileSubtype.VFT2_DRV_INSTALLABLE:
return "Installable";
case VersionFileSubtype.VFT2_DRV_KEYBOARD:
return "Keyboard";
case VersionFileSubtype.VFT2_DRV_LANGUAGE:
return "Language";
case VersionFileSubtype.VFT2_DRV_MOUSE:
return "Mouse";
case VersionFileSubtype.VFT2_DRV_NETWORK:
return "Network";
case VersionFileSubtype.VFT2_DRV_PRINTER:
return "Printer";
case VersionFileSubtype.VFT2_DRV_SOUND:
return "Sound";
case VersionFileSubtype.VFT2_DRV_SYSTEM:
return "System";
case VersionFileSubtype.VFT2_DRV_VERSIONED_PRINTER:
return "Versioned";
case VersionFileSubtype.VFT2_UNKNOWN:
return "Unknown";
case VersionFileSubtype.VFT2_DRV_COMM: return "Communications";
case VersionFileSubtype.VFT2_DRV_DISPLAY: return "Display";
case VersionFileSubtype.VFT2_DRV_INSTALLABLE: return "Installable";
case VersionFileSubtype.VFT2_DRV_KEYBOARD: return "Keyboard";
case VersionFileSubtype.VFT2_DRV_LANGUAGE: return "Language";
case VersionFileSubtype.VFT2_DRV_MOUSE: return "Mouse";
case VersionFileSubtype.VFT2_DRV_NETWORK: return "Network";
case VersionFileSubtype.VFT2_DRV_PRINTER: return "Printer";
case VersionFileSubtype.VFT2_DRV_SOUND: return "Sound";
case VersionFileSubtype.VFT2_DRV_SYSTEM: return "System";
case VersionFileSubtype.VFT2_DRV_VERSIONED_PRINTER: return "Versioned";
case VersionFileSubtype.VFT2_UNKNOWN: return "Unknown";
default:
return string.Format("Unknown type code {0}", (uint)subtype);
}
}
/// <summary>
/// Converts a <see cref="VersionFileSubtype"/> to string, considering file type to be a font
/// </summary>
/// <returns>The string.</returns>
/// <param name="subtype"><see cref="VersionFileSubtype"/></param>
public static string FontToString(VersionFileSubtype subtype)
/// <summary>
/// Converts a <see cref="VersionFileSubtype" /> to string, considering file type to be a font
/// </summary>
/// <returns>The string.</returns>
/// <param name="subtype">
/// <see cref="VersionFileSubtype" />
/// </param>
public static string FontToString(VersionFileSubtype subtype)
{
switch (subtype)
switch(subtype)
{
case VersionFileSubtype.VFT2_FONT_RASTER:
return "Raster";
case VersionFileSubtype.VFT2_FONT_TRUETYPE:
return "TrueType";
case VersionFileSubtype.VFT2_FONT_VECTOR:
return "Vector";
case VersionFileSubtype.VFT2_UNKNOWN:
return "Unknown";
case VersionFileSubtype.VFT2_FONT_RASTER: return "Raster";
case VersionFileSubtype.VFT2_FONT_TRUETYPE: return "TrueType";
case VersionFileSubtype.VFT2_FONT_VECTOR: return "Vector";
case VersionFileSubtype.VFT2_UNKNOWN: return "Unknown";
default:
return string.Format("Unknown type code {0}", (uint)subtype);
}
}
/// <summary>
/// Converts a <see cref="VersionFileOS"/> to string
/// </summary>
/// <returns>The string.</returns>
/// <param name="os"><see cref="VersionFileOS"/></param>
public static string OsToString(VersionFileOS os)
/// <summary>
/// Converts a <see cref="VersionFileOS" /> to string
/// </summary>
/// <returns>The string.</returns>
/// <param name="os">
/// <see cref="VersionFileOS" />
/// </param>
public static string OsToString(VersionFileOS os)
{
switch (os)
switch(os)
{
case VersionFileOS.VOS_DOS:
return "DOS";
case VersionFileOS.VOS_NT:
return "Windows NT";
case VersionFileOS.VOS_WINDOWS16:
return "16-bit Windows";
case VersionFileOS.VOS_WINDOWS32:
return "32-bit Windows";
case VersionFileOS.VOS_OS216:
return "16-bit OS/2";
case VersionFileOS.VOS_OS232:
return "32-bit OS/2";
case VersionFileOS.VOS_PM16:
return "16-bit Presentation Manager";
case VersionFileOS.VOS_PM32:
return "32-bit Presentation Manager";
case VersionFileOS.VOS_UNKNOWN:
return "Unknown";
case VersionFileOS.VOS_DOS_NT:
return "DOS running under Windows NT";
case VersionFileOS.VOS_DOS_WINDOWS16:
return "16-bit Windows running under DOS";
case VersionFileOS.VOS_DOS_WINDOWS32:
return "32-bit Windows running under DOS";
case VersionFileOS.VOS_DOS_PM16:
return "16-bit Presentation Manager running under DOS";
case VersionFileOS.VOS_DOS_PM32:
return "32-bit Presentation Manager running under DOS";
case VersionFileOS.VOS_NT_WINDOWS16:
return "16-bit Windows running under Windows NT";
case VersionFileOS.VOS_NT_WINDOWS32:
return "32-bit Windows running under Windows NT";
case VersionFileOS.VOS_DOS: return "DOS";
case VersionFileOS.VOS_NT: return "Windows NT";
case VersionFileOS.VOS_WINDOWS16: return "16-bit Windows";
case VersionFileOS.VOS_WINDOWS32: return "32-bit Windows";
case VersionFileOS.VOS_OS216: return "16-bit OS/2";
case VersionFileOS.VOS_OS232: return "32-bit OS/2";
case VersionFileOS.VOS_PM16: return "16-bit Presentation Manager";
case VersionFileOS.VOS_PM32: return "32-bit Presentation Manager";
case VersionFileOS.VOS_UNKNOWN: return "Unknown";
case VersionFileOS.VOS_DOS_NT: return "DOS running under Windows NT";
case VersionFileOS.VOS_DOS_WINDOWS16: return "16-bit Windows running under DOS";
case VersionFileOS.VOS_DOS_WINDOWS32: return "32-bit Windows running under DOS";
case VersionFileOS.VOS_DOS_PM16: return "16-bit Presentation Manager running under DOS";
case VersionFileOS.VOS_DOS_PM32: return "32-bit Presentation Manager running under DOS";
case VersionFileOS.VOS_NT_WINDOWS16: return "16-bit Windows running under Windows NT";
case VersionFileOS.VOS_NT_WINDOWS32: return "32-bit Windows running under Windows NT";
case VersionFileOS.VOS_NT_PM16:
return "16-bit Presentation Manager running under Windows NT";
case VersionFileOS.VOS_NT_PM32:
return "32-bit Presentation Manager running under Windows NT";
case VersionFileOS.VOS_OS216_WINDOWS16:
return "16-bit Windows running under 16-bit OS/2";
case VersionFileOS.VOS_OS216_WINDOWS32:
return "32-bit Windows running under 16-bit OS/2";
case VersionFileOS.VOS_OS216_WINDOWS16: return "16-bit Windows running under 16-bit OS/2";
case VersionFileOS.VOS_OS216_WINDOWS32: return "32-bit Windows running under 16-bit OS/2";
case VersionFileOS.VOS_OS216_PM16:
return "16-bit Presentation Manager running under 16-bit OS/2";
case VersionFileOS.VOS_OS216_PM32:
return "32-bit Presentation Manager running under 16-bit OS/2";
case VersionFileOS.VOS_OS232_WINDOWS16:
return "16-bit Windows running under 32-bit OS/2";
case VersionFileOS.VOS_OS232_WINDOWS32:
return "32-bit Windows running under 32-bit OS/2";
case VersionFileOS.VOS_OS232_WINDOWS16: return "16-bit Windows running under 32-bit OS/2";
case VersionFileOS.VOS_OS232_WINDOWS32: return "32-bit Windows running under 32-bit OS/2";
case VersionFileOS.VOS_OS232_PM16:
return "16-bit Presentation Manager running under 32-bit OS/2";
case VersionFileOS.VOS_OS232_PM32:
return "32-bit Presentation Manager running under 32-bit OS/2";
default:
return string.Format("Unknown OS code {0}", (uint)os);
default: return string.Format("Unknown OS code {0}", (uint)os);
}
}
}

View File

@@ -28,11 +28,11 @@ namespace libexeinfo
{
public partial class PE
{
/// <summary>
/// Portable Executable signature, "PE\0\0"
/// </summary>
public const ushort Signature = 0x00004550;
public const ushort PE32 = COFF.ZMAGIC;
public const ushort PE32Plus = 0x20b;
}
/// <summary>
/// Portable Executable signature, "PE\0\0"
/// </summary>
public const ushort Signature = 0x00004550;
public const ushort PE32 = COFF.ZMAGIC;
public const ushort PE32Plus = 0x20b;
}
}

View File

@@ -25,153 +25,155 @@
// THE SOFTWARE.
using System;
namespace libexeinfo
{
public partial class PE
{
/// <summary>
/// The following values defined for the Subsystem field of the optional header determine which Windows subsystem (if any) is required to run the image.
/// </summary>
public enum Subsystems : ushort
{
/// <summary>
/// An unknown subsystem
/// </summary>
IMAGE_SUBSYSTEM_UNKNOWN = 0,
/// <summary>
/// Device drivers and native Windows processes
/// </summary>
IMAGE_SUBSYSTEM_NATIVE = 1,
/// <summary>
/// The Windows graphical user interface (GUI) subsystem
/// </summary>
IMAGE_SUBSYSTEM_WINDOWS_GUI = 2,
/// <summary>
/// The Windows character subsystem
/// </summary>
IMAGE_SUBSYSTEM_WINDOWS_CUI = 3,
/// <summary>
/// The OS/2 character subsystem
/// </summary>
IMAGE_SUBSYSTEM_OS2_CUI = 5,
/// <summary>
/// The Posix character subsystem
/// </summary>
IMAGE_SUBSYSTEM_POSIX_CUI = 7,
/// <summary>
/// Native Win9x driver
/// </summary>
IMAGE_SUBSYSTEM_NATIVE_WINDOWS = 8,
/// <summary>
/// Windows CE
/// </summary>
IMAGE_SUBSYSTEM_WINDOWS_CE_GUI = 9,
/// <summary>
/// An Extensible Firmware Interface (EFI) application
/// </summary>
IMAGE_SUBSYSTEM_EFI_APPLICATION = 10,
/// <summary>
/// An EFI driver with boot services
/// </summary>
IMAGE_SUBSYSTEM_EFI_BOOT_SERVICE_DRIVER = 11,
/// <summary>
/// An EFI driver with run-time services
/// </summary>
IMAGE_SUBSYSTEM_EFI_RUNTIME_DRIVER = 12,
/// <summary>
/// An EFI ROM image
/// </summary>
IMAGE_SUBSYSTEM_EFI_ROM = 13,
/// <summary>
/// XBOX
/// </summary>
IMAGE_SUBSYSTEM_XBOX = 14,
/// <summary>
/// Windows boot application
/// </summary>
IMAGE_SUBSYSTEM_WINDOWS_BOOT_APPLICATION = 16
}
/// <summary>
/// The following values are defined for the DllCharacteristics field of the optional header.
/// </summary>
[Flags]
public enum DllCharacteristics : ushort
{
/// <summary>
/// Image can handle a high entropy 64-bit virtual address space.
/// </summary>
IMAGE_DLLCHARACTERISTICS_HIGH_ENTROPY_VA = 0x0020,
/// <summary>
/// DLL can be relocated at load time.
/// </summary>
IMAGE_DLLCHARACTERISTICS_DYNAMIC_BASE = 0x0040,
/// <summary>
/// Code Integrity checks are enforced.
/// </summary>
IMAGE_DLLCHARACTERISTICS_FORCE_INTEGRITY = 0x0080,
/// <summary>
/// Image is NX compatible.
/// </summary>
IMAGE_DLLCHARACTERISTICS_NX_COMPAT = 0x0100,
/// <summary>
/// Isolation aware, but do not isolate the image.
/// </summary>
IMAGE_DLLCHARACTERISTICS_NO_ISOLATION = 0x0200,
/// <summary>
/// Does not use structured exception (SE) handling. No SE handler may be called in this image.
/// </summary>
IMAGE_DLLCHARACTERISTICS_NO_SEH = 0x0400,
/// <summary>
/// Do not bind the image.
/// </summary>
IMAGE_DLLCHARACTERISTICS_NO_BIND = 0x0800,
/// <summary>
/// Image must execute in an AppContainer.
/// </summary>
IMAGE_DLLCHARACTERISTICS_APPCONTAINER = 0x1000,
/// <summary>
/// A WDM driver.
/// </summary>
IMAGE_DLLCHARACTERISTICS_WDM_DRIVER = 0x2000,
/// <summary>
/// Image supports Control Flow Guard.
/// </summary>
IMAGE_DLLCHARACTERISTICS_GUARD_CF = 0x4000,
/// <summary>
/// Terminal Server aware.
/// </summary>
IMAGE_DLLCHARACTERISTICS_TERMINAL_SERVER_AWARE = 0x8000
}
public enum DebugTypes : uint
{
/// <summary>
/// Unknown value, ignored by all tools.
/// </summary>
IMAGE_DEBUG_TYPE_UNKNOWN = 0,
/// <summary>
/// COFF debug information (line numbers, symbol table, and string table).
/// This type of debug information is also pointed to by fields in the file headers.
/// </summary>
IMAGE_DEBUG_TYPE_COFF = 1,
/// <summary>
/// CodeView debug information. The format of the data block is described
/// by the CV4 specification.
/// </summary>
IMAGE_DEBUG_TYPE_CODEVIEW = 2,
/// <summary>
/// Frame Pointer Omission (FPO) information. This information tells the
/// debugger how to interpret non-standard stack frames, which use the
/// EBP register for a purpose other than as a frame pointer.
/// </summary>
IMAGE_DEBUG_TYPE_FPO = 3,
IMAGE_DEBUG_TYPE_MISC = 4,
IMAGE_DEBUG_TYPE_EXCEPTION = 5,
IMAGE_DEBUG_TYPE_FIXUP = 6,
IMAGE_DEBUG_TYPE_OMAP_TO_SRC = 7,
/// <summary>
/// Unknown value, ignored by all tools.
/// </summary>
IMAGE_DEBUG_TYPE_UNKNOWN = 0,
/// <summary>
/// COFF debug information (line numbers, symbol table, and string table).
/// This type of debug information is also pointed to by fields in the file headers.
/// </summary>
IMAGE_DEBUG_TYPE_COFF = 1,
/// <summary>
/// CodeView debug information. The format of the data block is described
/// by the CV4 specification.
/// </summary>
IMAGE_DEBUG_TYPE_CODEVIEW = 2,
/// <summary>
/// Frame Pointer Omission (FPO) information. This information tells the
/// debugger how to interpret non-standard stack frames, which use the
/// EBP register for a purpose other than as a frame pointer.
/// </summary>
IMAGE_DEBUG_TYPE_FPO = 3,
IMAGE_DEBUG_TYPE_MISC = 4,
IMAGE_DEBUG_TYPE_EXCEPTION = 5,
IMAGE_DEBUG_TYPE_FIXUP = 6,
IMAGE_DEBUG_TYPE_OMAP_TO_SRC = 7,
IMAGE_DEBUG_TYPE_OMAP_FROM_SRC = 8,
IMAGE_DEBUG_TYPE_BORLAND = 9
IMAGE_DEBUG_TYPE_BORLAND = 9
}
/// <summary>
/// The following values are defined for the DllCharacteristics field of the optional header.
/// </summary>
[Flags]
public enum DllCharacteristics : ushort
{
/// <summary>
/// Image can handle a high entropy 64-bit virtual address space.
/// </summary>
IMAGE_DLLCHARACTERISTICS_HIGH_ENTROPY_VA = 0x0020,
/// <summary>
/// DLL can be relocated at load time.
/// </summary>
IMAGE_DLLCHARACTERISTICS_DYNAMIC_BASE = 0x0040,
/// <summary>
/// Code Integrity checks are enforced.
/// </summary>
IMAGE_DLLCHARACTERISTICS_FORCE_INTEGRITY = 0x0080,
/// <summary>
/// Image is NX compatible.
/// </summary>
IMAGE_DLLCHARACTERISTICS_NX_COMPAT = 0x0100,
/// <summary>
/// Isolation aware, but do not isolate the image.
/// </summary>
IMAGE_DLLCHARACTERISTICS_NO_ISOLATION = 0x0200,
/// <summary>
/// Does not use structured exception (SE) handling. No SE handler may be called in this image.
/// </summary>
IMAGE_DLLCHARACTERISTICS_NO_SEH = 0x0400,
/// <summary>
/// Do not bind the image.
/// </summary>
IMAGE_DLLCHARACTERISTICS_NO_BIND = 0x0800,
/// <summary>
/// Image must execute in an AppContainer.
/// </summary>
IMAGE_DLLCHARACTERISTICS_APPCONTAINER = 0x1000,
/// <summary>
/// A WDM driver.
/// </summary>
IMAGE_DLLCHARACTERISTICS_WDM_DRIVER = 0x2000,
/// <summary>
/// Image supports Control Flow Guard.
/// </summary>
IMAGE_DLLCHARACTERISTICS_GUARD_CF = 0x4000,
/// <summary>
/// Terminal Server aware.
/// </summary>
IMAGE_DLLCHARACTERISTICS_TERMINAL_SERVER_AWARE = 0x8000
}
/// <summary>
/// The following values defined for the Subsystem field of the optional header determine which Windows subsystem (if
/// any) is required to run the image.
/// </summary>
public enum Subsystems : ushort
{
/// <summary>
/// An unknown subsystem
/// </summary>
IMAGE_SUBSYSTEM_UNKNOWN = 0,
/// <summary>
/// Device drivers and native Windows processes
/// </summary>
IMAGE_SUBSYSTEM_NATIVE = 1,
/// <summary>
/// The Windows graphical user interface (GUI) subsystem
/// </summary>
IMAGE_SUBSYSTEM_WINDOWS_GUI = 2,
/// <summary>
/// The Windows character subsystem
/// </summary>
IMAGE_SUBSYSTEM_WINDOWS_CUI = 3,
/// <summary>
/// The OS/2 character subsystem
/// </summary>
IMAGE_SUBSYSTEM_OS2_CUI = 5,
/// <summary>
/// The Posix character subsystem
/// </summary>
IMAGE_SUBSYSTEM_POSIX_CUI = 7,
/// <summary>
/// Native Win9x driver
/// </summary>
IMAGE_SUBSYSTEM_NATIVE_WINDOWS = 8,
/// <summary>
/// Windows CE
/// </summary>
IMAGE_SUBSYSTEM_WINDOWS_CE_GUI = 9,
/// <summary>
/// An Extensible Firmware Interface (EFI) application
/// </summary>
IMAGE_SUBSYSTEM_EFI_APPLICATION = 10,
/// <summary>
/// An EFI driver with boot services
/// </summary>
IMAGE_SUBSYSTEM_EFI_BOOT_SERVICE_DRIVER = 11,
/// <summary>
/// An EFI driver with run-time services
/// </summary>
IMAGE_SUBSYSTEM_EFI_RUNTIME_DRIVER = 12,
/// <summary>
/// An EFI ROM image
/// </summary>
IMAGE_SUBSYSTEM_EFI_ROM = 13,
/// <summary>
/// XBOX
/// </summary>
IMAGE_SUBSYSTEM_XBOX = 14,
/// <summary>
/// Windows boot application
/// </summary>
IMAGE_SUBSYSTEM_WINDOWS_BOOT_APPLICATION = 16
}
}
}

View File

@@ -24,77 +24,83 @@
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
// THE SOFTWARE.
using System;
using System.Collections.Generic;
using System.IO;
using System.Text;
namespace libexeinfo
{
public partial class PE
{
public partial class PE
{
public static string GetInfo(PEHeader header, WindowsHeader64 winheader)
{
{
StringBuilder sb = new StringBuilder();
sb.Append(COFF.GetInfo(header.coff));
sb.AppendLine("Portable Executable (PE):");
sb.AppendLine("Portable Executable (PE):");
if(header.coff.optionalHeader.magic == PE32Plus)
sb.AppendFormat("\tExecutable base address: 0x{0:X16}", winheader.imageBase).AppendLine();
else
sb.AppendFormat("\tExecutable base address: 0x{0:X8}", winheader.imageBase).AppendLine();
sb.AppendFormat("\tSections are aligned to {0} bytes", winheader.sectionAlignment).AppendLine();
sb.AppendFormat("\tFile is aligned to {0} bytes", winheader.fileAlignment).AppendLine();
else sb.AppendFormat("\tExecutable base address: 0x{0:X8}", winheader.imageBase).AppendLine();
sb.AppendFormat("\tSections are aligned to {0} bytes", winheader.sectionAlignment).AppendLine();
sb.AppendFormat("\tFile is aligned to {0} bytes", winheader.fileAlignment).AppendLine();
if(winheader.majorOperatingSystemVersion > 0 || winheader.minorOperatingSystemVersion > 0)
sb.AppendFormat("\tExecutable requires at least operating system version {0}.{1} to run", winheader.majorOperatingSystemVersion, winheader.minorOperatingSystemVersion).AppendLine();
if (winheader.majorImageVersion > 0 || winheader.minorImageVersion > 0)
sb.AppendFormat("\tExecutable version: {0}.{1}", winheader.majorImageVersion, winheader.minorImageVersion).AppendLine();
sb.AppendFormat("\tAccording to subsystem, executable is {0}", SubsystemToString(winheader.subsystem)).AppendLine();
if (winheader.majorSubsystemVersion > 0 || winheader.minorSubsystemVersion > 0)
sb.AppendFormat("\tExecutable requires at least subsystem version {0}.{1} to run", winheader.majorSubsystemVersion, winheader.minorSubsystemVersion).AppendLine();
sb.AppendFormat("\tExecutable requires at least operating system version {0}.{1} to run",
winheader.majorOperatingSystemVersion, winheader.minorOperatingSystemVersion)
.AppendLine();
if(winheader.majorImageVersion > 0 || winheader.minorImageVersion > 0)
sb.AppendFormat("\tExecutable version: {0}.{1}", winheader.majorImageVersion,
winheader.minorImageVersion).AppendLine();
sb.AppendFormat("\tAccording to subsystem, executable is {0}", SubsystemToString(winheader.subsystem))
.AppendLine();
if(winheader.majorSubsystemVersion > 0 || winheader.minorSubsystemVersion > 0)
sb.AppendFormat("\tExecutable requires at least subsystem version {0}.{1} to run",
winheader.majorSubsystemVersion, winheader.minorSubsystemVersion).AppendLine();
if (winheader.dllCharacteristics.HasFlag(DllCharacteristics.IMAGE_DLLCHARACTERISTICS_HIGH_ENTROPY_VA))
if(winheader.dllCharacteristics.HasFlag(DllCharacteristics.IMAGE_DLLCHARACTERISTICS_HIGH_ENTROPY_VA))
sb.AppendLine("\tExecutable can handle a high entropy 64-bit virtual address space");
if (winheader.dllCharacteristics.HasFlag(DllCharacteristics.IMAGE_DLLCHARACTERISTICS_DYNAMIC_BASE))
sb.AppendLine("\tExecutable can be relocated at load time");
if (winheader.dllCharacteristics.HasFlag(DllCharacteristics.IMAGE_DLLCHARACTERISTICS_FORCE_INTEGRITY))
sb.AppendLine("\tCode Integrity checks are enforced");
if (winheader.dllCharacteristics.HasFlag(DllCharacteristics.IMAGE_DLLCHARACTERISTICS_NX_COMPAT))
sb.AppendLine("\tExecutable is NX compatible");
if (winheader.dllCharacteristics.HasFlag(DllCharacteristics.IMAGE_DLLCHARACTERISTICS_NO_ISOLATION))
sb.AppendLine("\tExecutable is isolation aware, but should not be isolated");
if (winheader.dllCharacteristics.HasFlag(DllCharacteristics.IMAGE_DLLCHARACTERISTICS_NO_SEH))
sb.AppendLine("\tExecutable does not use structured exception handling");
if (winheader.dllCharacteristics.HasFlag(DllCharacteristics.IMAGE_DLLCHARACTERISTICS_NO_BIND))
sb.AppendLine("\tExecutable should not be binded");
if (winheader.dllCharacteristics.HasFlag(DllCharacteristics.IMAGE_DLLCHARACTERISTICS_APPCONTAINER))
sb.AppendLine("\tExecutable must be run inside an AppContainer");
if (winheader.dllCharacteristics.HasFlag(DllCharacteristics.IMAGE_DLLCHARACTERISTICS_WDM_DRIVER))
sb.AppendLine("\tExecutable contains a WDM driver");
if (winheader.dllCharacteristics.HasFlag(DllCharacteristics.IMAGE_DLLCHARACTERISTICS_GUARD_CF))
sb.AppendLine("\tExecutable supports Control Flow Guard");
if (winheader.dllCharacteristics.HasFlag(DllCharacteristics.IMAGE_DLLCHARACTERISTICS_TERMINAL_SERVER_AWARE))
sb.AppendLine("\tExecutable is Terminal Server aware");
if(winheader.dllCharacteristics.HasFlag(DllCharacteristics.IMAGE_DLLCHARACTERISTICS_DYNAMIC_BASE))
sb.AppendLine("\tExecutable can be relocated at load time");
if(winheader.dllCharacteristics.HasFlag(DllCharacteristics.IMAGE_DLLCHARACTERISTICS_FORCE_INTEGRITY))
sb.AppendLine("\tCode Integrity checks are enforced");
if(winheader.dllCharacteristics.HasFlag(DllCharacteristics.IMAGE_DLLCHARACTERISTICS_NX_COMPAT))
sb.AppendLine("\tExecutable is NX compatible");
if(winheader.dllCharacteristics.HasFlag(DllCharacteristics.IMAGE_DLLCHARACTERISTICS_NO_ISOLATION))
sb.AppendLine("\tExecutable is isolation aware, but should not be isolated");
if(winheader.dllCharacteristics.HasFlag(DllCharacteristics.IMAGE_DLLCHARACTERISTICS_NO_SEH))
sb.AppendLine("\tExecutable does not use structured exception handling");
if(winheader.dllCharacteristics.HasFlag(DllCharacteristics.IMAGE_DLLCHARACTERISTICS_NO_BIND))
sb.AppendLine("\tExecutable should not be binded");
if(winheader.dllCharacteristics.HasFlag(DllCharacteristics.IMAGE_DLLCHARACTERISTICS_APPCONTAINER))
sb.AppendLine("\tExecutable must be run inside an AppContainer");
if(winheader.dllCharacteristics.HasFlag(DllCharacteristics.IMAGE_DLLCHARACTERISTICS_WDM_DRIVER))
sb.AppendLine("\tExecutable contains a WDM driver");
if(winheader.dllCharacteristics.HasFlag(DllCharacteristics.IMAGE_DLLCHARACTERISTICS_GUARD_CF))
sb.AppendLine("\tExecutable supports Control Flow Guard");
if(winheader.dllCharacteristics.HasFlag(DllCharacteristics.IMAGE_DLLCHARACTERISTICS_TERMINAL_SERVER_AWARE))
sb.AppendLine("\tExecutable is Terminal Server aware");
if (winheader.win32VersionValue > 0)
sb.AppendFormat("\tWin32 version value: {0}", winheader.win32VersionValue).AppendLine();
sb.AppendFormat("\tExecutable is {0} bytes", winheader.sizeOfImage).AppendLine();
sb.AppendFormat("\tHeaders are {0} bytes", winheader.sizeOfHeaders).AppendLine();
sb.AppendFormat("\tChecksum: 0x{0:X8}", winheader.checksum).AppendLine();
sb.AppendFormat("\t{0} bytes of stack should be reserved", winheader.sizeOfStackReserve).AppendLine();
sb.AppendFormat("\t{0} bytes of stack should be committed", winheader.sizeOfStackCommit).AppendLine();
sb.AppendFormat("\t{0} bytes of heap should be reserved", winheader.sizeOfHeapReserve).AppendLine();
sb.AppendFormat("\t{0} bytes of heap should be committed", winheader.sizeOfHeapCommit).AppendLine();
if (winheader.loaderFlags > 0)
sb.AppendFormat("\tLoader flags: {0}", winheader.loaderFlags).AppendLine();
sb.AppendFormat("\t{0} RVA entries follow the header", winheader.numberOfRvaAndSizes).AppendLine();
if(winheader.win32VersionValue > 0)
sb.AppendFormat("\tWin32 version value: {0}", winheader.win32VersionValue)
.AppendLine();
sb.AppendFormat("\tExecutable is {0} bytes", winheader.sizeOfImage).AppendLine();
sb.AppendFormat("\tHeaders are {0} bytes", winheader.sizeOfHeaders).AppendLine();
sb.AppendFormat("\tChecksum: 0x{0:X8}", winheader.checksum).AppendLine();
sb.AppendFormat("\t{0} bytes of stack should be reserved", winheader.sizeOfStackReserve)
.AppendLine();
sb.AppendFormat("\t{0} bytes of stack should be committed", winheader.sizeOfStackCommit)
.AppendLine();
sb.AppendFormat("\t{0} bytes of heap should be reserved", winheader.sizeOfHeapReserve)
.AppendLine();
sb.AppendFormat("\t{0} bytes of heap should be committed", winheader.sizeOfHeapCommit)
.AppendLine();
if(winheader.loaderFlags > 0) sb.AppendFormat("\tLoader flags: {0}", winheader.loaderFlags).AppendLine();
sb.AppendFormat("\t{0} RVA entries follow the header", winheader.numberOfRvaAndSizes)
.AppendLine();
return sb.ToString();
}
return sb.ToString();
}
public string GetInfo()
{
return GetInfo(Header, WinHeader);
}
}
}
}

View File

@@ -23,6 +23,7 @@
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONPECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
// THE SOFTWARE.
using System;
using System.IO;
using System.Runtime.InteropServices;
@@ -30,203 +31,191 @@ using System.Runtime.InteropServices;
namespace libexeinfo
{
/// <summary>
/// Represents a Microsoft Portable Executable
/// Represents a Microsoft Portable Executable
/// </summary>
public partial class PE
{
/// <summary>
/// The <see cref="FileStream"/> that contains the executable represented by this instance
/// </summary>
public readonly FileStream BaseStream;
/// <summary>
/// Header for this executable
/// </summary>
public readonly PEHeader Header;
/// <summary>
/// If true this instance correctly represents a Microsoft Portable Executable
/// </summary>
public readonly bool IsPE;
public readonly MZ BaseExecutable;
public readonly MZ BaseExecutable;
/// <summary>
/// The <see cref="FileStream" /> that contains the executable represented by this instance
/// </summary>
public readonly FileStream BaseStream;
/// <summary>
/// Header for this executable
/// </summary>
public readonly PEHeader Header;
/// <summary>
/// If true this instance correctly represents a Microsoft Portable Executable
/// </summary>
public readonly bool IsPE;
public readonly WindowsHeader64 WinHeader;
/// <summary>
/// Initializes a new instance of the <see cref="T:libexeinfo.PE"/> class.
/// </summary>
/// <param name="path">Executable path.</param>
public PE(string path)
{
IsPE = false;
BaseStream = File.Open(path, FileMode.Open, FileAccess.Read);
/// <summary>
/// Initializes a new instance of the <see cref="T:libexeinfo.PE" /> class.
/// </summary>
/// <param name="path">Executable path.</param>
public PE(string path)
{
IsPE = false;
BaseStream = File.Open(path, FileMode.Open, FileAccess.Read);
BaseExecutable = new MZ(BaseStream);
if(BaseExecutable.IsMZ)
{
if(BaseExecutable.Header.new_offset < BaseStream.Length)
if(BaseExecutable.Header.new_offset < BaseStream.Length)
{
BaseStream.Seek(BaseExecutable.Header.new_offset, SeekOrigin.Begin);
byte[] buffer = new byte[Marshal.SizeOf(typeof(PEHeader))];
BaseStream.Read(buffer, 0, buffer.Length);
IntPtr hdrPtr = Marshal.AllocHGlobal(buffer.Length);
Marshal.Copy(buffer, 0, hdrPtr, buffer.Length);
BaseStream.Seek(BaseExecutable.Header.new_offset, SeekOrigin.Begin);
byte[] buffer = new byte[Marshal.SizeOf(typeof(PEHeader))];
BaseStream.Read(buffer, 0, buffer.Length);
IntPtr hdrPtr = Marshal.AllocHGlobal(buffer.Length);
Marshal.Copy(buffer, 0, hdrPtr, buffer.Length);
Header = (PEHeader)Marshal.PtrToStructure(hdrPtr, typeof(PEHeader));
Marshal.FreeHGlobal(hdrPtr);
Marshal.FreeHGlobal(hdrPtr);
IsPE = Header.signature == Signature;
if(IsPE)
{
if(Header.coff.optionalHeader.magic == PE32Plus)
{
BaseStream.Position -= 4;
buffer = new byte[Marshal.SizeOf(typeof(WindowsHeader64))];
BaseStream.Read(buffer, 0, buffer.Length);
hdrPtr = Marshal.AllocHGlobal(buffer.Length);
Marshal.Copy(buffer, 0, hdrPtr, buffer.Length);
WinHeader = (WindowsHeader64)Marshal.PtrToStructure(hdrPtr, typeof(WindowsHeader64));
Marshal.FreeHGlobal(hdrPtr);
}
buffer = new byte[Marshal.SizeOf(typeof(WindowsHeader64))];
BaseStream.Read(buffer, 0, buffer.Length);
hdrPtr = Marshal.AllocHGlobal(buffer.Length);
Marshal.Copy(buffer, 0, hdrPtr, buffer.Length);
WinHeader = (WindowsHeader64)Marshal.PtrToStructure(hdrPtr, typeof(WindowsHeader64));
Marshal.FreeHGlobal(hdrPtr);
}
else
{
buffer = new byte[Marshal.SizeOf(typeof(WindowsHeader))];
BaseStream.Read(buffer, 0, buffer.Length);
hdrPtr = Marshal.AllocHGlobal(buffer.Length);
Marshal.Copy(buffer, 0, hdrPtr, buffer.Length);
buffer = new byte[Marshal.SizeOf(typeof(WindowsHeader))];
BaseStream.Read(buffer, 0, buffer.Length);
hdrPtr = Marshal.AllocHGlobal(buffer.Length);
Marshal.Copy(buffer, 0, hdrPtr, buffer.Length);
WindowsHeader hdr32 = (WindowsHeader)Marshal.PtrToStructure(hdrPtr, typeof(WindowsHeader));
Marshal.FreeHGlobal(hdrPtr);
Marshal.FreeHGlobal(hdrPtr);
WinHeader = ToPlus(hdr32);
}
}
}
}
}
}
}
}
/// <summary>
/// Initializes a new instance of the <see cref="T:libexeinfo.PE"/> class.
/// </summary>
/// <param name="stream">Stream containing the executable.</param>
public PE(FileStream stream)
{
IsPE = false;
BaseStream = stream;
BaseExecutable = new MZ(BaseStream);
if (BaseExecutable.IsMZ)
{
if (BaseExecutable.Header.new_offset < BaseStream.Length)
{
BaseStream.Seek(BaseExecutable.Header.new_offset, SeekOrigin.Begin);
byte[] buffer = new byte[Marshal.SizeOf(typeof(PEHeader))];
BaseStream.Read(buffer, 0, buffer.Length);
IntPtr hdrPtr = Marshal.AllocHGlobal(buffer.Length);
Marshal.Copy(buffer, 0, hdrPtr, buffer.Length);
Header = (PEHeader)Marshal.PtrToStructure(hdrPtr, typeof(PEHeader));
Marshal.FreeHGlobal(hdrPtr);
IsPE = Header.signature == Signature;
/// <summary>
/// Initializes a new instance of the <see cref="T:libexeinfo.PE" /> class.
/// </summary>
/// <param name="stream">Stream containing the executable.</param>
public PE(FileStream stream)
{
IsPE = false;
BaseStream = stream;
BaseExecutable = new MZ(BaseStream);
if(BaseExecutable.IsMZ)
if(BaseExecutable.Header.new_offset < BaseStream.Length)
{
BaseStream.Seek(BaseExecutable.Header.new_offset, SeekOrigin.Begin);
byte[] buffer = new byte[Marshal.SizeOf(typeof(PEHeader))];
BaseStream.Read(buffer, 0, buffer.Length);
IntPtr hdrPtr = Marshal.AllocHGlobal(buffer.Length);
Marshal.Copy(buffer, 0, hdrPtr, buffer.Length);
Header = (PEHeader)Marshal.PtrToStructure(hdrPtr, typeof(PEHeader));
Marshal.FreeHGlobal(hdrPtr);
IsPE = Header.signature == Signature;
if (IsPE)
{
if (Header.coff.optionalHeader.magic == PE32Plus)
{
BaseStream.Position -= 4;
buffer = new byte[Marshal.SizeOf(typeof(WindowsHeader64))];
BaseStream.Read(buffer, 0, buffer.Length);
hdrPtr = Marshal.AllocHGlobal(buffer.Length);
Marshal.Copy(buffer, 0, hdrPtr, buffer.Length);
WinHeader = (WindowsHeader64)Marshal.PtrToStructure(hdrPtr, typeof(WindowsHeader64));
Marshal.FreeHGlobal(hdrPtr);
}
else
{
buffer = new byte[Marshal.SizeOf(typeof(WindowsHeader))];
BaseStream.Read(buffer, 0, buffer.Length);
hdrPtr = Marshal.AllocHGlobal(buffer.Length);
Marshal.Copy(buffer, 0, hdrPtr, buffer.Length);
WindowsHeader hdr32 = (WindowsHeader)Marshal.PtrToStructure(hdrPtr, typeof(WindowsHeader));
Marshal.FreeHGlobal(hdrPtr);
WinHeader = ToPlus(hdr32);
}
}
}
}
}
if(IsPE)
if(Header.coff.optionalHeader.magic == PE32Plus)
{
BaseStream.Position -= 4;
buffer = new byte[Marshal.SizeOf(typeof(WindowsHeader64))];
BaseStream.Read(buffer, 0, buffer.Length);
hdrPtr = Marshal.AllocHGlobal(buffer.Length);
Marshal.Copy(buffer, 0, hdrPtr, buffer.Length);
WinHeader = (WindowsHeader64)Marshal.PtrToStructure(hdrPtr, typeof(WindowsHeader64));
Marshal.FreeHGlobal(hdrPtr);
}
else
{
buffer = new byte[Marshal.SizeOf(typeof(WindowsHeader))];
BaseStream.Read(buffer, 0, buffer.Length);
hdrPtr = Marshal.AllocHGlobal(buffer.Length);
Marshal.Copy(buffer, 0, hdrPtr, buffer.Length);
WindowsHeader hdr32 = (WindowsHeader)Marshal.PtrToStructure(hdrPtr, typeof(WindowsHeader));
Marshal.FreeHGlobal(hdrPtr);
WinHeader = ToPlus(hdr32);
}
}
}
/// <summary>
/// Identifies if the specified executable is a Microsoft Portable Executable
/// </summary>
/// <returns><c>true</c> if the specified executable is a Microsoft Portable Executable, <c>false</c> otherwise.</returns>
/// <param name="path">Executable path.</param>
public static bool Identify(string path)
{
FileStream BaseStream = File.Open(path, FileMode.Open, FileAccess.Read);
MZ BaseExecutable = new MZ(BaseStream);
if (BaseExecutable.IsMZ)
{
if (BaseExecutable.Header.new_offset < BaseStream.Length)
{
BaseStream.Seek(BaseExecutable.Header.new_offset, SeekOrigin.Begin);
byte[] buffer = new byte[Marshal.SizeOf(typeof(PEHeader))];
BaseStream.Read(buffer, 0, buffer.Length);
IntPtr hdrPtr = Marshal.AllocHGlobal(buffer.Length);
Marshal.Copy(buffer, 0, hdrPtr, buffer.Length);
PEHeader Header = (PEHeader)Marshal.PtrToStructure(hdrPtr, typeof(PEHeader));
Marshal.FreeHGlobal(hdrPtr);
return Header.signature == Signature;
}
}
/// <summary>
/// Identifies if the specified executable is a Microsoft Portable Executable
/// </summary>
/// <returns><c>true</c> if the specified executable is a Microsoft Portable Executable, <c>false</c> otherwise.</returns>
/// <param name="path">Executable path.</param>
public static bool Identify(string path)
{
FileStream BaseStream = File.Open(path, FileMode.Open, FileAccess.Read);
MZ BaseExecutable = new MZ(BaseStream);
if(BaseExecutable.IsMZ)
if(BaseExecutable.Header.new_offset < BaseStream.Length)
{
BaseStream.Seek(BaseExecutable.Header.new_offset, SeekOrigin.Begin);
byte[] buffer = new byte[Marshal.SizeOf(typeof(PEHeader))];
BaseStream.Read(buffer, 0, buffer.Length);
IntPtr hdrPtr = Marshal.AllocHGlobal(buffer.Length);
Marshal.Copy(buffer, 0, hdrPtr, buffer.Length);
PEHeader Header = (PEHeader)Marshal.PtrToStructure(hdrPtr, typeof(PEHeader));
Marshal.FreeHGlobal(hdrPtr);
return Header.signature == Signature;
}
return false;
}
}
/// <summary>
/// Identifies if the specified executable is a Microsoft Portable Executable
/// </summary>
/// <returns><c>true</c> if the specified executable is a Microsoft Portable Executable, <c>false</c> otherwise.</returns>
/// <param name="stream">Stream containing the executable.</param>
public static bool Identify(FileStream stream)
{
FileStream BaseStream = stream;
MZ BaseExecutable = new MZ(BaseStream);
if (BaseExecutable.IsMZ)
{
if (BaseExecutable.Header.new_offset < BaseStream.Length)
{
BaseStream.Seek(BaseExecutable.Header.new_offset, SeekOrigin.Begin);
byte[] buffer = new byte[Marshal.SizeOf(typeof(PEHeader))];
BaseStream.Read(buffer, 0, buffer.Length);
IntPtr hdrPtr = Marshal.AllocHGlobal(buffer.Length);
Marshal.Copy(buffer, 0, hdrPtr, buffer.Length);
PEHeader Header = (PEHeader)Marshal.PtrToStructure(hdrPtr, typeof(PEHeader));
Marshal.FreeHGlobal(hdrPtr);
return Header.signature == Signature;
}
}
/// <summary>
/// Identifies if the specified executable is a Microsoft Portable Executable
/// </summary>
/// <returns><c>true</c> if the specified executable is a Microsoft Portable Executable, <c>false</c> otherwise.</returns>
/// <param name="stream">Stream containing the executable.</param>
public static bool Identify(FileStream stream)
{
FileStream BaseStream = stream;
MZ BaseExecutable = new MZ(BaseStream);
if(BaseExecutable.IsMZ)
if(BaseExecutable.Header.new_offset < BaseStream.Length)
{
BaseStream.Seek(BaseExecutable.Header.new_offset, SeekOrigin.Begin);
byte[] buffer = new byte[Marshal.SizeOf(typeof(PEHeader))];
BaseStream.Read(buffer, 0, buffer.Length);
IntPtr hdrPtr = Marshal.AllocHGlobal(buffer.Length);
Marshal.Copy(buffer, 0, hdrPtr, buffer.Length);
PEHeader Header = (PEHeader)Marshal.PtrToStructure(hdrPtr, typeof(PEHeader));
Marshal.FreeHGlobal(hdrPtr);
return Header.signature == Signature;
}
return false;
}
return false;
}
static WindowsHeader64 ToPlus(WindowsHeader header)
{
return new WindowsHeader64
{
imageBase = header.imageBase,
sectionAlignment = header.sectionAlignment,
fileAlignment = header.fileAlignment,
imageBase = header.imageBase,
sectionAlignment = header.sectionAlignment,
fileAlignment = header.fileAlignment,
majorOperatingSystemVersion = header.majorOperatingSystemVersion,
minorOperatingSystemVersion = header.minorOperatingSystemVersion,
majorImageVersion = header.majorImageVersion,
minorImageVersion = header.minorImageVersion,
majorSubsystemVersion = header.majorSubsystemVersion,
minorSubsystemVersion = header.minorSubsystemVersion,
win32VersionValue = header.win32VersionValue,
sizeOfImage = header.sizeOfImage,
sizeOfHeaders = header.sizeOfHeaders,
checksum = header.checksum,
subsystem = header.subsystem,
dllCharacteristics = header.dllCharacteristics,
sizeOfStackReserve = header.sizeOfStackReserve,
sizeOfStackCommit = header.sizeOfStackCommit,
sizeOfHeapReserve = header.sizeOfHeapReserve,
sizeOfHeapCommit = header.sizeOfHeapCommit,
loaderFlags = header.loaderFlags,
numberOfRvaAndSizes = header.numberOfRvaAndSizes
majorImageVersion = header.majorImageVersion,
minorImageVersion = header.minorImageVersion,
majorSubsystemVersion = header.majorSubsystemVersion,
minorSubsystemVersion = header.minorSubsystemVersion,
win32VersionValue = header.win32VersionValue,
sizeOfImage = header.sizeOfImage,
sizeOfHeaders = header.sizeOfHeaders,
checksum = header.checksum,
subsystem = header.subsystem,
dllCharacteristics = header.dllCharacteristics,
sizeOfStackReserve = header.sizeOfStackReserve,
sizeOfStackCommit = header.sizeOfStackCommit,
sizeOfHeapReserve = header.sizeOfHeapReserve,
sizeOfHeapCommit = header.sizeOfHeapCommit,
loaderFlags = header.loaderFlags,
numberOfRvaAndSizes = header.numberOfRvaAndSizes
};
}
}
}
}

View File

@@ -24,7 +24,6 @@
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
// THE SOFTWARE.
using System;
using System.Runtime.InteropServices;
using static libexeinfo.COFF;
@@ -32,320 +31,345 @@ namespace libexeinfo
{
public partial class PE
{
/// <summary>
/// Header for a Microsoft New Executable
/// </summary>
[StructLayout(LayoutKind.Sequential/*, Pack = 2*/)]
/// <summary>
/// Header for a Microsoft New Executable
/// </summary>
[StructLayout(LayoutKind.Sequential /*, Pack = 2*/)]
public struct PEHeader
{
/// <summary>
/// After the MS-DOS stub, at the file offset specified at offset 0x3c, is a 4-byte signature that identifies the file as a PE format image file. This signature is "PE\0\0" (the letters "P" and "E" followed by two null bytes).
/// </summary>
public uint signature;
/// <summary>
/// After the MS-DOS stub, at the file offset specified at offset 0x3c, is a 4-byte signature that identifies the file
/// as a PE format image file. This signature is "PE\0\0" (the letters "P" and "E" followed by two null bytes).
/// </summary>
public uint signature;
public COFFHeader coff;
}
/// <summary>
/// The next 21 fields are an extension to the COFF optional header format. They contain additional information that is required by the linker and loader in Windows.
/// </summary>
[StructLayout(LayoutKind.Sequential/*, Pack = 2*/)]
/// <summary>
/// The next 21 fields are an extension to the COFF optional header format. They contain additional information that is
/// required by the linker and loader in Windows.
/// </summary>
[StructLayout(LayoutKind.Sequential /*, Pack = 2*/)]
public struct WindowsHeader
{
/// <summary>
/// The preferred address of the first byte of image when loaded into memory; must be a multiple of 64 K. The default for DLLs is 0x10000000. The default for Windows CE EXEs is 0x00010000. The default for Windows NT, Windows 2000, Windows XP, Windows 95, Windows 98, and Windows Me is 0x00400000.
/// </summary>
public uint imageBase;
/// <summary>
/// The alignment (in bytes) of sections when they are loaded into memory. It must be greater than or equal to FileAlignment. The default is the page size for the architecture.
/// </summary>
public uint sectionAlignment;
/// <summary>
/// The alignment factor (in bytes) that is used to align the raw data of sections in the image file. The value should be a power of 2 between 512 and 64 K, inclusive. The default is 512. If the SectionAlignment is less than the architecture's page size, then FileAlignment must match SectionAlignment.
/// </summary>
public uint fileAlignment;
/// <summary>
/// The major version number of the required operating system.
/// </summary>
public ushort majorOperatingSystemVersion;
/// <summary>
/// The minor version number of the required operating system.
/// </summary>
public ushort minorOperatingSystemVersion;
/// <summary>
/// The major version number of the image.
/// </summary>
public ushort majorImageVersion;
/// <summary>
/// The minor version number of the image.
/// </summary>
public ushort minorImageVersion;
/// <summary>
/// The major version number of the subsystem.
/// </summary>
public ushort majorSubsystemVersion;
/// <summary>
/// The minor version number of the subsystem.
/// </summary>
public ushort minorSubsystemVersion;
/// <summary>
/// Reserved, must be zero.
/// </summary>
public uint win32VersionValue;
/// <summary>
/// The size (in bytes) of the image, including all headers, as the image is loaded in memory. It must be a multiple of SectionAlignment.
/// </summary>
public uint sizeOfImage;
/// <summary>
/// The combined size of an MS-DOS stub, PE header, and section headers rounded up to a multiple of FileAlignment.
/// </summary>
public uint sizeOfHeaders;
/// <summary>
/// The image file checksum. The algorithm for computing the checksum is incorporated into IMAGHELP.DLL. The following are checked for validation at load time: all drivers, any DLL loaded at boot time, and any DLL that is loaded into a critical Windows process.
/// </summary>
public uint checksum;
/// <summary>
/// The subsystem that is required to run this image. For more information, <see cref="Subsystems"/>.
/// </summary>
public Subsystems subsystem;
/// <summary>
/// For more information, <see cref="DllCharacteristics"/>.
/// </summary>
public DllCharacteristics dllCharacteristics;
/// <summary>
/// The size of the stack to reserve. Only SizeOfStackCommit is committed; the rest is made available one page at a time until the reserve size is reached.
/// </summary>
public uint sizeOfStackReserve;
/// <summary>
/// The size of the stack to commit.
/// </summary>
public uint sizeOfStackCommit;
/// <summary>
/// The size of the local heap space to reserve. Only SizeOfHeapCommit is committed; the rest is made available one page at a time until the reserve size is reached.
/// </summary>
public uint sizeOfHeapReserve;
/// <summary>
/// The size of the local heap space to commit.
/// </summary>
public uint sizeOfHeapCommit;
/// <summary>
/// Reserved, must be zero.
/// </summary>
public uint loaderFlags;
/// <summary>
/// The number of data-directory entries in the remainder of the optional header. Each describes a location and size.
/// </summary>
public uint numberOfRvaAndSizes;
/// <summary>
/// The preferred address of the first byte of image when loaded into memory; must be a multiple of 64 K. The default
/// for DLLs is 0x10000000. The default for Windows CE EXEs is 0x00010000. The default for Windows NT, Windows 2000,
/// Windows XP, Windows 95, Windows 98, and Windows Me is 0x00400000.
/// </summary>
public uint imageBase;
/// <summary>
/// The alignment (in bytes) of sections when they are loaded into memory. It must be greater than or equal to
/// FileAlignment. The default is the page size for the architecture.
/// </summary>
public uint sectionAlignment;
/// <summary>
/// The alignment factor (in bytes) that is used to align the raw data of sections in the image file. The value should
/// be a power of 2 between 512 and 64 K, inclusive. The default is 512. If the SectionAlignment is less than the
/// architecture's page size, then FileAlignment must match SectionAlignment.
/// </summary>
public uint fileAlignment;
/// <summary>
/// The major version number of the required operating system.
/// </summary>
public ushort majorOperatingSystemVersion;
/// <summary>
/// The minor version number of the required operating system.
/// </summary>
public ushort minorOperatingSystemVersion;
/// <summary>
/// The major version number of the image.
/// </summary>
public ushort majorImageVersion;
/// <summary>
/// The minor version number of the image.
/// </summary>
public ushort minorImageVersion;
/// <summary>
/// The major version number of the subsystem.
/// </summary>
public ushort majorSubsystemVersion;
/// <summary>
/// The minor version number of the subsystem.
/// </summary>
public ushort minorSubsystemVersion;
/// <summary>
/// Reserved, must be zero.
/// </summary>
public uint win32VersionValue;
/// <summary>
/// The size (in bytes) of the image, including all headers, as the image is loaded in memory. It must be a multiple of
/// SectionAlignment.
/// </summary>
public uint sizeOfImage;
/// <summary>
/// The combined size of an MS-DOS stub, PE header, and section headers rounded up to a multiple of FileAlignment.
/// </summary>
public uint sizeOfHeaders;
/// <summary>
/// The image file checksum. The algorithm for computing the checksum is incorporated into IMAGHELP.DLL. The following
/// are checked for validation at load time: all drivers, any DLL loaded at boot time, and any DLL that is loaded into
/// a critical Windows process.
/// </summary>
public uint checksum;
/// <summary>
/// The subsystem that is required to run this image. For more information, <see cref="Subsystems" />.
/// </summary>
public Subsystems subsystem;
/// <summary>
/// For more information, <see cref="DllCharacteristics" />.
/// </summary>
public DllCharacteristics dllCharacteristics;
/// <summary>
/// The size of the stack to reserve. Only SizeOfStackCommit is committed; the rest is made available one page at a
/// time until the reserve size is reached.
/// </summary>
public uint sizeOfStackReserve;
/// <summary>
/// The size of the stack to commit.
/// </summary>
public uint sizeOfStackCommit;
/// <summary>
/// The size of the local heap space to reserve. Only SizeOfHeapCommit is committed; the rest is made available one
/// page at a time until the reserve size is reached.
/// </summary>
public uint sizeOfHeapReserve;
/// <summary>
/// The size of the local heap space to commit.
/// </summary>
public uint sizeOfHeapCommit;
/// <summary>
/// Reserved, must be zero.
/// </summary>
public uint loaderFlags;
/// <summary>
/// The number of data-directory entries in the remainder of the optional header. Each describes a location and size.
/// </summary>
public uint numberOfRvaAndSizes;
}
/// <summary>
/// The next 21 fields are an extension to the COFF optional header format. They contain additional information that is required by the linker and loader in Windows.
/// </summary>
[StructLayout(LayoutKind.Sequential/*, Pack = 2*/)]
/// <summary>
/// The next 21 fields are an extension to the COFF optional header format. They contain additional information that is
/// required by the linker and loader in Windows.
/// </summary>
[StructLayout(LayoutKind.Sequential /*, Pack = 2*/)]
public struct WindowsHeader64
{
/// <summary>
/// The preferred address of the first byte of image when loaded into memory; must be a multiple of 64 K. The default for DLLs is 0x10000000. The default for Windows CE EXEs is 0x00010000. The default for Windows NT, Windows 2000, Windows XP, Windows 95, Windows 98, and Windows Me is 0x00400000.
/// </summary>
public ulong imageBase;
/// <summary>
/// The alignment (in bytes) of sections when they are loaded into memory. It must be greater than or equal to FileAlignment. The default is the page size for the architecture.
/// </summary>
public uint sectionAlignment;
/// <summary>
/// The alignment factor (in bytes) that is used to align the raw data of sections in the image file. The value should be a power of 2 between 512 and 64 K, inclusive. The default is 512. If the SectionAlignment is less than the architecture's page size, then FileAlignment must match SectionAlignment.
/// </summary>
public uint fileAlignment;
/// <summary>
/// The major version number of the required operating system.
/// </summary>
public ushort majorOperatingSystemVersion;
/// <summary>
/// The minor version number of the required operating system.
/// </summary>
public ushort minorOperatingSystemVersion;
/// <summary>
/// The major version number of the image.
/// </summary>
public ushort majorImageVersion;
/// <summary>
/// The minor version number of the image.
/// </summary>
public ushort minorImageVersion;
/// <summary>
/// The major version number of the subsystem.
/// </summary>
public ushort majorSubsystemVersion;
/// <summary>
/// The minor version number of the subsystem.
/// </summary>
public ushort minorSubsystemVersion;
/// <summary>
/// Reserved, must be zero.
/// </summary>
public uint win32VersionValue;
/// <summary>
/// The size (in bytes) of the image, including all headers, as the image is loaded in memory. It must be a multiple of SectionAlignment.
/// </summary>
public uint sizeOfImage;
/// <summary>
/// The combined size of an MS-DOS stub, PE header, and section headers rounded up to a multiple of FileAlignment.
/// </summary>
public uint sizeOfHeaders;
/// <summary>
/// The image file checksum. The algorithm for computing the checksum is incorporated into IMAGHELP.DLL. The following are checked for validation at load time: all drivers, any DLL loaded at boot time, and any DLL that is loaded into a critical Windows process.
/// </summary>
public uint checksum;
/// <summary>
/// The subsystem that is required to run this image. For more information, <see cref="Subsystems"/>.
/// </summary>
public Subsystems subsystem;
/// <summary>
/// For more information, <see cref="DllCharacteristics"/>.
/// </summary>
public DllCharacteristics dllCharacteristics;
/// <summary>
/// The size of the stack to reserve. Only SizeOfStackCommit is committed; the rest is made available one page at a time until the reserve size is reached.
/// </summary>
public ulong sizeOfStackReserve;
/// <summary>
/// The size of the stack to commit.
/// </summary>
public ulong sizeOfStackCommit;
/// <summary>
/// The size of the local heap space to reserve. Only SizeOfHeapCommit is committed; the rest is made available one page at a time until the reserve size is reached.
/// </summary>
public ulong sizeOfHeapReserve;
/// <summary>
/// The size of the local heap space to commit.
/// </summary>
public ulong sizeOfHeapCommit;
/// <summary>
/// Reserved, must be zero.
/// </summary>
public uint loaderFlags;
/// <summary>
/// The number of data-directory entries in the remainder of the optional header. Each describes a location and size.
/// </summary>
public uint numberOfRvaAndSizes;
/// <summary>
/// The preferred address of the first byte of image when loaded into memory; must be a multiple of 64 K. The default
/// for DLLs is 0x10000000. The default for Windows CE EXEs is 0x00010000. The default for Windows NT, Windows 2000,
/// Windows XP, Windows 95, Windows 98, and Windows Me is 0x00400000.
/// </summary>
public ulong imageBase;
/// <summary>
/// The alignment (in bytes) of sections when they are loaded into memory. It must be greater than or equal to
/// FileAlignment. The default is the page size for the architecture.
/// </summary>
public uint sectionAlignment;
/// <summary>
/// The alignment factor (in bytes) that is used to align the raw data of sections in the image file. The value should
/// be a power of 2 between 512 and 64 K, inclusive. The default is 512. If the SectionAlignment is less than the
/// architecture's page size, then FileAlignment must match SectionAlignment.
/// </summary>
public uint fileAlignment;
/// <summary>
/// The major version number of the required operating system.
/// </summary>
public ushort majorOperatingSystemVersion;
/// <summary>
/// The minor version number of the required operating system.
/// </summary>
public ushort minorOperatingSystemVersion;
/// <summary>
/// The major version number of the image.
/// </summary>
public ushort majorImageVersion;
/// <summary>
/// The minor version number of the image.
/// </summary>
public ushort minorImageVersion;
/// <summary>
/// The major version number of the subsystem.
/// </summary>
public ushort majorSubsystemVersion;
/// <summary>
/// The minor version number of the subsystem.
/// </summary>
public ushort minorSubsystemVersion;
/// <summary>
/// Reserved, must be zero.
/// </summary>
public uint win32VersionValue;
/// <summary>
/// The size (in bytes) of the image, including all headers, as the image is loaded in memory. It must be a multiple of
/// SectionAlignment.
/// </summary>
public uint sizeOfImage;
/// <summary>
/// The combined size of an MS-DOS stub, PE header, and section headers rounded up to a multiple of FileAlignment.
/// </summary>
public uint sizeOfHeaders;
/// <summary>
/// The image file checksum. The algorithm for computing the checksum is incorporated into IMAGHELP.DLL. The following
/// are checked for validation at load time: all drivers, any DLL loaded at boot time, and any DLL that is loaded into
/// a critical Windows process.
/// </summary>
public uint checksum;
/// <summary>
/// The subsystem that is required to run this image. For more information, <see cref="Subsystems" />.
/// </summary>
public Subsystems subsystem;
/// <summary>
/// For more information, <see cref="DllCharacteristics" />.
/// </summary>
public DllCharacteristics dllCharacteristics;
/// <summary>
/// The size of the stack to reserve. Only SizeOfStackCommit is committed; the rest is made available one page at a
/// time until the reserve size is reached.
/// </summary>
public ulong sizeOfStackReserve;
/// <summary>
/// The size of the stack to commit.
/// </summary>
public ulong sizeOfStackCommit;
/// <summary>
/// The size of the local heap space to reserve. Only SizeOfHeapCommit is committed; the rest is made available one
/// page at a time until the reserve size is reached.
/// </summary>
public ulong sizeOfHeapReserve;
/// <summary>
/// The size of the local heap space to commit.
/// </summary>
public ulong sizeOfHeapCommit;
/// <summary>
/// Reserved, must be zero.
/// </summary>
public uint loaderFlags;
/// <summary>
/// The number of data-directory entries in the remainder of the optional header. Each describes a location and size.
/// </summary>
public uint numberOfRvaAndSizes;
}
[StructLayout(LayoutKind.Sequential/*, Pack = 2*/)]
[StructLayout(LayoutKind.Sequential /*, Pack = 2*/)]
public struct ImageDataDirectory
{
/// <summary>
/// Relative virtual address of the table
/// </summary>
uint rva;
/// <summary>
/// The size in bytes
/// </summary>
uint size;
/// <summary>
/// Relative virtual address of the table
/// </summary>
uint rva;
/// <summary>
/// The size in bytes
/// </summary>
uint size;
}
[StructLayout(LayoutKind.Sequential/*, Pack = 2*/)]
[StructLayout(LayoutKind.Sequential /*, Pack = 2*/)]
public struct DebugDirectory
{
/// <summary>
/// A reserved field intended to be used for flags, set to zero for now.
/// </summary>
public uint characteristics;
/// <summary>
/// Time and date the debug data was created.
/// </summary>
public uint timeDateStamp;
/// <summary>
/// Major version number of the debug data format.
/// </summary>
public ushort majorVersion;
/// <summary>
/// Minor version number of the debug data format.
/// </summary>
public ushort minorVersion;
/// <summary>
/// Format of debugging information: this field enables support of multiple debuggers. <see cref="DebugTypes"/> for more information.
/// </summary>
public DebugTypes type;
/// <summary>
/// Size of the debug data (not including the debug directory itself).
/// </summary>
public uint sizeOfData;
/// <summary>
/// Address of the debug data when loaded, relative to the image base.
/// </summary>
public uint addressOfRawData;
/// <summary>
/// File pointer to the debug data.
/// </summary>
public uint pointerToRawData;
/// <summary>
/// A reserved field intended to be used for flags, set to zero for now.
/// </summary>
public uint characteristics;
/// <summary>
/// Time and date the debug data was created.
/// </summary>
public uint timeDateStamp;
/// <summary>
/// Major version number of the debug data format.
/// </summary>
public ushort majorVersion;
/// <summary>
/// Minor version number of the debug data format.
/// </summary>
public ushort minorVersion;
/// <summary>
/// Format of debugging information: this field enables support of multiple debuggers. <see cref="DebugTypes" /> for
/// more information.
/// </summary>
public DebugTypes type;
/// <summary>
/// Size of the debug data (not including the debug directory itself).
/// </summary>
public uint sizeOfData;
/// <summary>
/// Address of the debug data when loaded, relative to the image base.
/// </summary>
public uint addressOfRawData;
/// <summary>
/// File pointer to the debug data.
/// </summary>
public uint pointerToRawData;
}
[StructLayout(LayoutKind.Sequential/*, Pack = 2*/)]
[StructLayout(LayoutKind.Sequential /*, Pack = 2*/)]
public struct ResourceDirectoryTable
{
/// <summary>
/// Resource flags, reserved for future use; currently set to zero.
/// </summary>
public uint characteristics;
/// <summary>
/// Time the resource data was created by the resource compiler.
/// </summary>
public uint timeDateStamp;
/// <summary>
/// Major version number, set by the user.
/// </summary>
public ushort majorVersion;
/// <summary>
/// Minor version number.
/// </summary>
public ushort minorVersion;
/// <summary>
/// Number of directory entries, immediately following the table,
/// that use strings to identify Type, Name, or Language
/// (depending on the level of the table).
/// </summary>
public ushort nameEntries;
/// <summary>
/// Number of directory entries, immediately following the Name
/// entries, that use numeric identifiers for Type, Name, or Language.
/// </summary>
public ushort idEntries;
/// <summary>
/// Resource flags, reserved for future use; currently set to zero.
/// </summary>
public uint characteristics;
/// <summary>
/// Time the resource data was created by the resource compiler.
/// </summary>
public uint timeDateStamp;
/// <summary>
/// Major version number, set by the user.
/// </summary>
public ushort majorVersion;
/// <summary>
/// Minor version number.
/// </summary>
public ushort minorVersion;
/// <summary>
/// Number of directory entries, immediately following the table,
/// that use strings to identify Type, Name, or Language
/// (depending on the level of the table).
/// </summary>
public ushort nameEntries;
/// <summary>
/// Number of directory entries, immediately following the Name
/// entries, that use numeric identifiers for Type, Name, or Language.
/// </summary>
public ushort idEntries;
}
[StructLayout(LayoutKind.Sequential/*, Pack = 2*/)]
[StructLayout(LayoutKind.Sequential /*, Pack = 2*/)]
public struct ResourceDirectoryEntries
{
/// <summary>
/// Address of string that gives the Type, Name, or Language identifier, depending on level of table.
/// or
/// 32-bit integer that identifies Type, Name, or Language.
/// </summary>
public uint nameOrID;
/// <summary>
/// High bit 0: Address of a Resource Data Entry(a leaf).
/// High bit 1: Lower 31 bits are the address of another Resource Directory Table(the next level down).
/// </summary>
public uint rva;
/// <summary>
/// Address of string that gives the Type, Name, or Language identifier, depending on level of table.
/// or
/// 32-bit integer that identifies Type, Name, or Language.
/// </summary>
public uint nameOrID;
/// <summary>
/// High bit 0: Address of a Resource Data Entry(a leaf).
/// High bit 1: Lower 31 bits are the address of another Resource Directory Table(the next level down).
/// </summary>
public uint rva;
}
[StructLayout(LayoutKind.Sequential/*, Pack = 2*/)]
[StructLayout(LayoutKind.Sequential /*, Pack = 2*/)]
public struct ResourceDataEntry
{
/// <summary>
/// Address of a unit of resource data in the Resource Data area.
/// </summary>
public uint rva;
/// <summary>
/// Size, in bytes, of the resource data pointed to by the Data RVA field.
/// </summary>
public uint size;
/// <summary>
/// Code page used to decode code point values within the resource data.Typically, the code page would be the Unicode code page.
/// </summary>
public uint codepage;
/// <summary>
/// Reserved (must be set to 0)
/// </summary>
public uint reserved;
/// <summary>
/// Address of a unit of resource data in the Resource Data area.
/// </summary>
public uint rva;
/// <summary>
/// Size, in bytes, of the resource data pointed to by the Data RVA field.
/// </summary>
public uint size;
/// <summary>
/// Code page used to decode code point values within the resource data.Typically, the code page would be the Unicode
/// code page.
/// </summary>
public uint codepage;
/// <summary>
/// Reserved (must be set to 0)
/// </summary>
public uint reserved;
}
}
}

View File

@@ -23,7 +23,7 @@
// 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;
namespace libexeinfo
{
public partial class PE
@@ -32,39 +32,23 @@ namespace libexeinfo
{
switch(subsystem)
{
/// <summary>
/// An unknown subsystem
/// </summary>
case Subsystems.IMAGE_SUBSYSTEM_UNKNOWN:
return "for an unknown subsystem with no code";
case Subsystems.IMAGE_SUBSYSTEM_NATIVE:
return "an application for native Windows API";
case Subsystems.IMAGE_SUBSYSTEM_WINDOWS_GUI:
return "an application for Windows GUI";
case Subsystems.IMAGE_SUBSYSTEM_UNKNOWN: return "for an unknown subsystem with no code";
case Subsystems.IMAGE_SUBSYSTEM_NATIVE: return "an application for native Windows API";
case Subsystems.IMAGE_SUBSYSTEM_WINDOWS_GUI: return "an application for Windows GUI";
case Subsystems.IMAGE_SUBSYSTEM_WINDOWS_CUI:
return "an application for Windows command-line";
case Subsystems.IMAGE_SUBSYSTEM_OS2_CUI:
return "an application for OS/2 command-line";
case Subsystems.IMAGE_SUBSYSTEM_POSIX_CUI:
return "an application for POSIX command-line";
case Subsystems.IMAGE_SUBSYSTEM_NATIVE_WINDOWS:
return "a driver for Windows 9x";
case Subsystems.IMAGE_SUBSYSTEM_WINDOWS_CE_GUI:
return "an application for Windows CE";
case Subsystems.IMAGE_SUBSYSTEM_EFI_APPLICATION:
return "an EFI application";
case Subsystems.IMAGE_SUBSYSTEM_EFI_BOOT_SERVICE_DRIVER:
return "an EFI boot services driver";
case Subsystems.IMAGE_SUBSYSTEM_EFI_RUNTIME_DRIVER:
return "an EFI run-time services driver";
case Subsystems.IMAGE_SUBSYSTEM_EFI_ROM:
return "an EFI ROM image";
case Subsystems.IMAGE_SUBSYSTEM_XBOX:
return "a Xbox executable";
case Subsystems.IMAGE_SUBSYSTEM_OS2_CUI: return "an application for OS/2 command-line";
case Subsystems.IMAGE_SUBSYSTEM_POSIX_CUI: return "an application for POSIX command-line";
case Subsystems.IMAGE_SUBSYSTEM_NATIVE_WINDOWS: return "a driver for Windows 9x";
case Subsystems.IMAGE_SUBSYSTEM_WINDOWS_CE_GUI: return "an application for Windows CE";
case Subsystems.IMAGE_SUBSYSTEM_EFI_APPLICATION: return "an EFI application";
case Subsystems.IMAGE_SUBSYSTEM_EFI_BOOT_SERVICE_DRIVER: return "an EFI boot services driver";
case Subsystems.IMAGE_SUBSYSTEM_EFI_RUNTIME_DRIVER: return "an EFI run-time services driver";
case Subsystems.IMAGE_SUBSYSTEM_EFI_ROM: return "an EFI ROM image";
case Subsystems.IMAGE_SUBSYSTEM_XBOX: return "a Xbox executable";
case Subsystems.IMAGE_SUBSYSTEM_WINDOWS_BOOT_APPLICATION:
return "an application for Windows Boot environment";
default:
return string.Format("for an unknown subsystem with code {0}", (ushort)subsystem);
default: return $"for an unknown subsystem with code {(ushort)subsystem}";
}
}
}

View File

@@ -1,5 +1,4 @@
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.

View File

@@ -23,112 +23,113 @@
// 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.Linq;
namespace libexeinfo
{
public static class Swapping
{
public static byte[] SwapTenBytes(byte[] source)
{
byte[] destination = new byte[8];
public static class Swapping
{
public static byte[] SwapTenBytes(byte[] source)
{
byte[] destination = new byte[8];
destination[0] = source[9];
destination[1] = source[8];
destination[2] = source[7];
destination[3] = source[6];
destination[4] = source[5];
destination[5] = source[4];
destination[6] = source[3];
destination[7] = source[2];
destination[8] = source[1];
destination[9] = source[0];
destination[0] = source[9];
destination[1] = source[8];
destination[2] = source[7];
destination[3] = source[6];
destination[4] = source[5];
destination[5] = source[4];
destination[6] = source[3];
destination[7] = source[2];
destination[8] = source[1];
destination[9] = source[0];
return destination;
}
return destination;
}
public static byte[] SwapEightBytes(byte[] source)
{
byte[] destination = new byte[8];
public static byte[] SwapEightBytes(byte[] source)
{
byte[] destination = new byte[8];
destination[0] = source[7];
destination[1] = source[6];
destination[2] = source[5];
destination[3] = source[4];
destination[4] = source[3];
destination[5] = source[2];
destination[6] = source[1];
destination[7] = source[0];
destination[0] = source[7];
destination[1] = source[6];
destination[2] = source[5];
destination[3] = source[4];
destination[4] = source[3];
destination[5] = source[2];
destination[6] = source[1];
destination[7] = source[0];
return destination;
}
return destination;
}
public static byte[] SwapFourBytes(byte[] source)
{
byte[] destination = new byte[4];
public static byte[] SwapFourBytes(byte[] source)
{
byte[] destination = new byte[4];
destination[0] = source[3];
destination[1] = source[2];
destination[2] = source[1];
destination[3] = source[0];
destination[0] = source[3];
destination[1] = source[2];
destination[2] = source[1];
destination[3] = source[0];
return destination;
}
return destination;
}
public static byte[] SwapTwoBytes(byte[] source)
{
byte[] destination = new byte[2];
public static byte[] SwapTwoBytes(byte[] source)
{
byte[] destination = new byte[2];
destination[0] = source[1];
destination[1] = source[0];
destination[0] = source[1];
destination[1] = source[0];
return destination;
}
return destination;
}
public static uint PDPFromLittleEndian(uint x)
{
return ((x & 0xffff) << 16) | ((x & 0xffff0000) >> 16);
}
public static uint PDPFromLittleEndian(uint x)
{
return ((x & 0xffff) << 16) | ((x & 0xffff0000) >> 16);
}
public static uint PDPFromBigEndian(uint x)
{
return ((x & 0xff00ff) << 8) | ((x & 0xff00ff00) >> 8);
}
public static uint PDPFromBigEndian(uint x)
{
return ((x & 0xff00ff) << 8) | ((x & 0xff00ff00) >> 8);
}
public static ulong Swap(ulong x)
{
x = (x & 0x00000000FFFFFFFF) << 32 | (x & 0xFFFFFFFF00000000) >> 32;
x = (x & 0x0000FFFF0000FFFF) << 16 | (x & 0xFFFF0000FFFF0000) >> 16;
x = (x & 0x00FF00FF00FF00FF) << 8 | (x & 0xFF00FF00FF00FF00) >> 8;
return x;
}
public static ulong Swap(ulong x)
{
x = ((x & 0x00000000FFFFFFFF) << 32) | ((x & 0xFFFFFFFF00000000) >> 32);
x = ((x & 0x0000FFFF0000FFFF) << 16) | ((x & 0xFFFF0000FFFF0000) >> 16);
x = ((x & 0x00FF00FF00FF00FF) << 8) | ((x & 0xFF00FF00FF00FF00) >> 8);
return x;
}
public static long Swap(long x)
{
return BitConverter.ToInt64(BitConverter.GetBytes(x).Reverse().ToArray(), 0);
}
public static long Swap(long x)
{
return BitConverter.ToInt64(BitConverter.GetBytes(x).Reverse().ToArray(), 0);
}
public static ushort Swap(ushort x)
{
return (ushort)((x << 8) | (x >> 8));
}
public static ushort Swap(ushort x)
{
return (ushort)((x << 8) | (x >> 8));
}
public static short Swap(short x)
{
return (short)((x << 8) | ((x >> 8) & 0xFF));
}
public static short Swap(short x)
{
return (short)((x << 8) | ((x >> 8) & 0xFF));
}
public static uint Swap(uint x)
{
x = ((x << 8) & 0xFF00FF00) | ((x >> 8) & 0xFF00FF);
return (x << 16) | (x >> 16);
}
public static uint Swap(uint x)
{
x = ((x << 8) & 0xFF00FF00) | ((x >> 8) & 0xFF00FF);
return (x << 16) | (x >> 16);
}
public static int Swap(int x)
{
x = (int)(((x << 8) & 0xFF00FF00) | ((x >> 8) & 0xFF00FF));
return (x << 16) | ((x >> 16) & 0xFFFF);
}
}
public static int Swap(int x)
{
x = (int)(((x << 8) & 0xFF00FF00) | ((x >> 8) & 0xFF00FF));
return (x << 16) | ((x >> 16) & 0xFFFF);
}
}
}