From c3cd0b5831ce1f1fbd77821c7369af50dd82814a Mon Sep 17 00:00:00 2001 From: Natalia Portillo Date: Sun, 25 Feb 2018 22:44:26 +0000 Subject: [PATCH] Refactor. --- libexeinfo/COFF/COFF.cs | 16 +-- libexeinfo/LX/Consts.cs | 4 +- libexeinfo/LX/Info.cs | 102 ++++++++++-------- libexeinfo/LX/LX.cs | 209 ++++++++++++++++++------------------ libexeinfo/MZ/Consts.cs | 2 +- libexeinfo/MZ/Info.cs | 2 +- libexeinfo/MZ/MZ.cs | 4 +- libexeinfo/NE/Consts.cs | 8 +- libexeinfo/NE/NE.cs | 17 ++- libexeinfo/NE/Version.cs | 50 ++++----- libexeinfo/PE/Consts.cs | 6 +- libexeinfo/PE/Enums.cs | 1 + libexeinfo/PE/Info.cs | 2 +- libexeinfo/PE/PE.cs | 44 ++++---- libexeinfo/PE/Subsystems.cs | 2 +- 15 files changed, 224 insertions(+), 245 deletions(-) diff --git a/libexeinfo/COFF/COFF.cs b/libexeinfo/COFF/COFF.cs index c7855a7..1379bba 100644 --- a/libexeinfo/COFF/COFF.cs +++ b/libexeinfo/COFF/COFF.cs @@ -68,17 +68,11 @@ namespace libexeinfo /// /// Header for this executable /// - public COFFHeader Header { get; private set; } - /// - /// The that contains the executable represented by this instance - /// - public Stream BaseStream { get; } - public bool IsBigEndian { get; private set; } - /// - /// If true this instance correctly represents a Common Object File Format - /// - public bool Recognized { get; private set; } - public string Type { get; private set; } + public COFFHeader Header { get; private set; } + public Stream BaseStream { get; } + public bool IsBigEndian { get; private set; } + public bool Recognized { get; private set; } + public string Type { get; private set; } void Initialize() { diff --git a/libexeinfo/LX/Consts.cs b/libexeinfo/LX/Consts.cs index 6b9b288..ba09734 100644 --- a/libexeinfo/LX/Consts.cs +++ b/libexeinfo/LX/Consts.cs @@ -31,10 +31,10 @@ namespace libexeinfo /// /// Linear Executable signature, "LE" /// - public const ushort Signature16 = 0x454C; + const ushort SIGNATURE16 = 0x454C; /// /// Linear eXecutable signature, "LX" /// - public const ushort Signature = 0x584C; + const ushort SIGNATURE = 0x584C; } } \ No newline at end of file diff --git a/libexeinfo/LX/Info.cs b/libexeinfo/LX/Info.cs index cc416e3..3d5d7f4 100644 --- a/libexeinfo/LX/Info.cs +++ b/libexeinfo/LX/Info.cs @@ -30,59 +30,67 @@ namespace libexeinfo { public partial class LX { - public string Information => GetInfo(Header); + public string Information => GetInfo(header); - public static string GetInfo(LXHeader header) + static string GetInfo(LXHeader header) { StringBuilder sb = new StringBuilder(); - if(header.signature == Signature16) sb.AppendLine("Linear Executable (LE):"); - else sb.AppendLine("Linear eXecutable (LX):"); + sb.AppendLine(header.signature == SIGNATURE16 ? "Linear Executable (LE):" : "Linear eXecutable (LX):"); - if(header.os_type == TargetOS.OS2) + switch(header.os_type) { - 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"); + case 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"); + break; + case TargetOS.Windows: + case TargetOS.Win32: + case TargetOS.Unknown: + switch(header.os_type) + { + case TargetOS.Windows: + case TargetOS.Unknown: + sb.AppendLine("\t16-bit Windows application"); + break; + case TargetOS.Win32: + sb.AppendLine("\t32-bit Windows application"); + break; + } + + 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"); + break; + case 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"); + break; + default: + sb.AppendFormat("\tApplication for unknown OS {0}", (ushort)header.os_type).AppendLine(); + break; } - 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(); diff --git a/libexeinfo/LX/LX.cs b/libexeinfo/LX/LX.cs index 3df895d..3cb4d41 100644 --- a/libexeinfo/LX/LX.cs +++ b/libexeinfo/LX/LX.cs @@ -30,127 +30,124 @@ using System.Runtime.InteropServices; namespace libexeinfo { - /// - /// Represents a Microsoft/IBM Linear EXecutable - /// - // TODO: Big-endian (really needed?) - public partial class LX : IExecutable + /// + /// Represents a Microsoft/IBM Linear EXecutable + /// + // TODO: Big-endian (really needed?) + public partial class LX : IExecutable { - MZ BaseExecutable; - /// - /// The that contains the executable represented by this instance - /// - public Stream BaseStream { get; } - public bool IsBigEndian => false; - /// - /// Header for this executable - /// - LXHeader Header; - /// - /// If true this instance correctly represents a Microsoft/IBM Linear EXecutable - /// - public bool Recognized { get; private set;} - public string Type { get; private set;} + MZ baseExecutable; + /// + /// Header for this executable + /// + LXHeader header; - /// - /// Initializes a new instance of the class. - /// - /// Executable path. - public LX(string path) + /// + /// Initializes a new instance of the class. + /// + /// Executable path. + public LX(string path) { - BaseStream = File.Open(path, FileMode.Open, FileAccess.Read); - Initialize(); + BaseStream = File.Open(path, FileMode.Open, FileAccess.Read); + Initialize(); } - /// - /// Initializes a new instance of the class. - /// - /// Stream containing the executable. - public LX(Stream stream) + /// + /// Initializes a new instance of the class. + /// + /// Stream containing the executable. + public LX(Stream stream) { - BaseStream = stream; - Initialize(); + BaseStream = stream; + Initialize(); } - /// - /// Initializes a new instance of the class. - /// - /// Byte array containing the executable. - public LX(byte[] data) - { - BaseStream = new MemoryStream(data); - Initialize(); - } - - void Initialize() - { - Recognized = false; - if(BaseStream == null) return; - BaseExecutable = new MZ(BaseStream); - if(!BaseExecutable.Recognized) return; - - if(BaseExecutable.Header.new_offset >= BaseStream.Length) return; - - 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); - Recognized = Header.signature == Signature || Header.signature == Signature16; - - if(!Recognized) return; - - Type = Header.signature == Signature16 ? "Linear Executable (LE)" : "Linear eXecutable (LX)"; - } - - /// - /// Identifies if the specified executable is a Microsoft/IBM Linear EXecutable - /// - /// true if the specified executable is a Microsoft/IBM Linear EXecutable, false otherwise. - /// Executable path. - public static bool Identify(string path) + /// + /// Initializes a new instance of the class. + /// + /// Byte array containing the executable. + public LX(byte[] data) { - FileStream BaseStream = File.Open(path, FileMode.Open, FileAccess.Read); - MZ BaseExecutable = new MZ(BaseStream); - if(!BaseExecutable.Recognized) return false; - - if(BaseExecutable.Header.new_offset >= BaseStream.Length) return false; - - 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; - + BaseStream = new MemoryStream(data); + Initialize(); } - /// - /// Identifies if the specified executable is a Microsoft/IBM Linear EXecutable - /// - /// true if the specified executable is a Microsoft/IBM Linear EXecutable, false otherwise. - /// Stream containing the executable. - public static bool Identify(FileStream stream) + /// + /// The that contains the executable represented by this instance + /// + public Stream BaseStream { get; } + public bool IsBigEndian => false; + public bool Recognized { get; private set; } + public string Type { get; private set; } + + void Initialize() { - FileStream BaseStream = stream; - MZ BaseExecutable = new MZ(BaseStream); - if(!BaseExecutable.Recognized) return false; + Recognized = false; + if(BaseStream == null) return; - if(BaseExecutable.Header.new_offset >= BaseStream.Length) return false; + baseExecutable = new MZ(BaseStream); + if(!baseExecutable.Recognized) return; - 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; + if(baseExecutable.Header.new_offset >= BaseStream.Length) return; + 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); + Recognized = header.signature == SIGNATURE || header.signature == SIGNATURE16; + + if(!Recognized) return; + + Type = header.signature == SIGNATURE16 ? "Linear Executable (LE)" : "Linear eXecutable (LX)"; + } + + /// + /// Identifies if the specified executable is a Microsoft/IBM Linear EXecutable + /// + /// true if the specified executable is a Microsoft/IBM Linear EXecutable, false otherwise. + /// Executable path. + public static bool Identify(string path) + { + FileStream baseStream = File.Open(path, FileMode.Open, FileAccess.Read); + MZ baseExecutable = new MZ(baseStream); + if(!baseExecutable.Recognized) return false; + + if(baseExecutable.Header.new_offset >= baseStream.Length) return false; + + 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; + } + + /// + /// Identifies if the specified executable is a Microsoft/IBM Linear EXecutable + /// + /// true if the specified executable is a Microsoft/IBM Linear EXecutable, false otherwise. + /// Stream containing the executable. + public static bool Identify(FileStream stream) + { + FileStream baseStream = stream; + MZ baseExecutable = new MZ(baseStream); + if(!baseExecutable.Recognized) return false; + + if(baseExecutable.Header.new_offset >= baseStream.Length) return false; + + 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; } } } \ No newline at end of file diff --git a/libexeinfo/MZ/Consts.cs b/libexeinfo/MZ/Consts.cs index 257956f..375fa51 100644 --- a/libexeinfo/MZ/Consts.cs +++ b/libexeinfo/MZ/Consts.cs @@ -31,6 +31,6 @@ namespace libexeinfo /// /// MZ executable signature, "MZ" /// - public const ushort Signature = 0x5A4D; + const ushort SIGNATURE = 0x5A4D; } } \ No newline at end of file diff --git a/libexeinfo/MZ/Info.cs b/libexeinfo/MZ/Info.cs index 15baeed..a565c6f 100644 --- a/libexeinfo/MZ/Info.cs +++ b/libexeinfo/MZ/Info.cs @@ -41,7 +41,7 @@ namespace libexeinfo /// /// Human readable information for given MZ header. /// MZ executable header. - public static string GetInfo(MZHeader header) + static string GetInfo(MZHeader header) { StringBuilder sb = new StringBuilder(); sb.AppendLine("DOS MZ executable:"); diff --git a/libexeinfo/MZ/MZ.cs b/libexeinfo/MZ/MZ.cs index 1a576f8..1eebdca 100644 --- a/libexeinfo/MZ/MZ.cs +++ b/libexeinfo/MZ/MZ.cs @@ -94,7 +94,7 @@ namespace libexeinfo Marshal.Copy(buffer, 0, hdrPtr, buffer.Length); Header = (MZHeader)Marshal.PtrToStructure(hdrPtr, typeof(MZHeader)); Marshal.FreeHGlobal(hdrPtr); - Recognized = Header.signature == Signature; + Recognized = Header.signature == SIGNATURE; if(!Recognized) return; @@ -128,7 +128,7 @@ namespace libexeinfo MZHeader mzHdr = (MZHeader)Marshal.PtrToStructure(hdrPtr, typeof(MZHeader)); Marshal.FreeHGlobal(hdrPtr); - return mzHdr.signature == Signature; + return mzHdr.signature == SIGNATURE; } } } \ No newline at end of file diff --git a/libexeinfo/NE/Consts.cs b/libexeinfo/NE/Consts.cs index cf8a0bb..0b5eed3 100644 --- a/libexeinfo/NE/Consts.cs +++ b/libexeinfo/NE/Consts.cs @@ -31,15 +31,15 @@ namespace libexeinfo /// /// New Executable signature, "NE" /// - public const ushort Signature = 0x454E; + const ushort SIGNATURE = 0x454E; /// /// Signature for a /// - public static readonly string FixedFileInfoSig = "VS_VERSION_INFO"; + const string FIXED_FILE_INFO_SIG = "VS_VERSION_INFO"; /// /// Signature for list of name=value strings inside a version resource /// - public static readonly string StringFileInfo = "StringFileInfo"; + const string STRING_FILE_INFO = "StringFileInfo"; /// /// Gets the name of a resource type according to its identifier @@ -76,7 +76,7 @@ namespace libexeinfo 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); + default: return $"{id & 0x7FFF}"; } } } diff --git a/libexeinfo/NE/NE.cs b/libexeinfo/NE/NE.cs index 0eb9c4c..e90450b 100644 --- a/libexeinfo/NE/NE.cs +++ b/libexeinfo/NE/NE.cs @@ -73,16 +73,10 @@ namespace libexeinfo Initialize(); } - /// - /// The that contains the executable represented by this instance - /// public Stream BaseStream { get; } public bool IsBigEndian => false; - /// - /// If true this instance correctly represents a Microsoft New Executable - /// - public bool Recognized { get; private set; } - public string Type { get; } + public bool Recognized { get; private set; } + public string Type { get; private set; } void Initialize() { @@ -102,9 +96,10 @@ namespace libexeinfo Marshal.Copy(buffer, 0, hdrPtr, buffer.Length); Header = (NEHeader)Marshal.PtrToStructure(hdrPtr, typeof(NEHeader)); Marshal.FreeHGlobal(hdrPtr); - if(Header.signature != Signature) return; + if(Header.signature != SIGNATURE) return; Recognized = true; + Type = "New Executable (NE)"; if(Header.resource_entries <= 0) return; Resources = GetResources(BaseStream, BaseExecutable.Header.new_offset, Header.resource_table_offset); @@ -131,7 +126,7 @@ namespace libexeinfo Marshal.Copy(buffer, 0, hdrPtr, buffer.Length); NEHeader Header = (NEHeader)Marshal.PtrToStructure(hdrPtr, typeof(NEHeader)); Marshal.FreeHGlobal(hdrPtr); - return Header.signature == Signature; + return Header.signature == SIGNATURE; } /// @@ -154,7 +149,7 @@ namespace libexeinfo Marshal.Copy(buffer, 0, hdrPtr, buffer.Length); NEHeader Header = (NEHeader)Marshal.PtrToStructure(hdrPtr, typeof(NEHeader)); Marshal.FreeHGlobal(hdrPtr); - return Header.signature == Signature; + return Header.signature == SIGNATURE; } } } \ No newline at end of file diff --git a/libexeinfo/NE/Version.cs b/libexeinfo/NE/Version.cs index 345c08c..e3224f8 100644 --- a/libexeinfo/NE/Version.cs +++ b/libexeinfo/NE/Version.cs @@ -27,6 +27,7 @@ using System; using System.Collections.Generic; using System.IO; +using System.Linq; using System.Runtime.InteropServices; using System.Text; @@ -40,17 +41,10 @@ namespace libexeinfo /// The decoded version resources. public List GetVersions() { - List versions = new List(); - - 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; + return (from type in Resources.types + where (type.id & 0x7FFF) == (int)ResourceTypes.RT_VERSION + from resource in type.resources + select new Version(resource.data, resource.name)).ToList(); } /// @@ -129,7 +123,7 @@ namespace libexeinfo /// The resource name. public string Name { get; } - VersionNode GetNode(byte[] data, int startPosition, out int nodeLength) + static VersionNode GetNode(byte[] data, int startPosition, out int nodeLength) { nodeLength = 0; @@ -172,21 +166,17 @@ namespace libexeinfo void DecodeNode(VersionNode node, string parent, string grandparent) { - if(node.szName == FixedFileInfoSig) + if(node.szName == FIXED_FILE_INFO_SIG) { 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); + FileVersion = + $"{(info.dwFileVersionMS & 0xFFFF0000) >> 16}.{info.dwFileVersionMS & 0xFFFF:D2}.{(info.dwFileVersionLS & 0xFFFF0000) >> 16}.{info.dwFileVersionLS & 0xFFFF}"; + ProductVersion = + $"{(info.dwProductVersionMS & 0xFFFF0000) >> 16}.{info.dwProductVersionMS & 0xFFFF:D2}.{(info.dwProductVersionLS & 0xFFFF0000) >> 16}.{info.dwProductVersionLS & 0xFFFF}"; FileFlags = (VersionFileFlags)(info.dwFileFlags & info.dwFileFlagsMask); FileOS = (VersionFileOS)info.dwFileOS; @@ -195,13 +185,13 @@ namespace libexeinfo FileDate = DateTime.FromFileTime(info.dwFileDateMS * 0x100000000 + info.dwFileDateLS); } - if(parent == StringFileInfo) + if(parent == STRING_FILE_INFO) { Dictionary strings = new Dictionary(); StringsByLanguage.Add(node.szName, strings); } - if(grandparent == StringFileInfo) + if(grandparent == STRING_FILE_INFO) if(StringsByLanguage.TryGetValue(parent, out Dictionary strings)) { Encoding encoding; @@ -212,9 +202,9 @@ namespace libexeinfo strings.Add(node.szName, encoding.GetString(node.rgbData)); } - if(node.children != null) - for(int i = 0; i < node.children.Length; i++) - DecodeNode(node.children[i], node.szName, parent); + if(node.children == null) return; + + foreach(VersionNode n in node.children) DecodeNode(n, node.szName, parent); } /// @@ -235,7 +225,7 @@ namespace libexeinfo 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); + default: return $"Unknown type code {(uint)type}"; } } @@ -263,7 +253,7 @@ namespace libexeinfo case VersionFileSubtype.VFT2_DRV_VERSIONED_PRINTER: return "Versioned"; case VersionFileSubtype.VFT2_UNKNOWN: return "Unknown"; default: - return string.Format("Unknown type code {0}", (uint)subtype); + return $"Unknown type code {(uint)subtype}"; } } @@ -283,7 +273,7 @@ namespace libexeinfo case VersionFileSubtype.VFT2_FONT_VECTOR: return "Vector"; case VersionFileSubtype.VFT2_UNKNOWN: return "Unknown"; default: - return string.Format("Unknown type code {0}", (uint)subtype); + return $"Unknown type code {(uint)subtype}"; } } @@ -330,7 +320,7 @@ namespace libexeinfo 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 $"Unknown OS code {(uint)os}"; } } } diff --git a/libexeinfo/PE/Consts.cs b/libexeinfo/PE/Consts.cs index 2709591..2f23b38 100644 --- a/libexeinfo/PE/Consts.cs +++ b/libexeinfo/PE/Consts.cs @@ -31,8 +31,8 @@ namespace libexeinfo /// /// Portable Executable signature, "PE\0\0" /// - public const ushort Signature = 0x00004550; - public const ushort PE32 = COFF.ZMAGIC; - public const ushort PE32Plus = 0x20b; + const ushort SIGNATURE = 0x00004550; + const ushort PE32 = COFF.ZMAGIC; + internal const ushort PE32Plus = 0x20b; } } \ No newline at end of file diff --git a/libexeinfo/PE/Enums.cs b/libexeinfo/PE/Enums.cs index 06caf02..00ee97d 100644 --- a/libexeinfo/PE/Enums.cs +++ b/libexeinfo/PE/Enums.cs @@ -25,6 +25,7 @@ // THE SOFTWARE. using System; +// ReSharper disable InconsistentNaming namespace libexeinfo { diff --git a/libexeinfo/PE/Info.cs b/libexeinfo/PE/Info.cs index 5010826..b72e6df 100644 --- a/libexeinfo/PE/Info.cs +++ b/libexeinfo/PE/Info.cs @@ -32,7 +32,7 @@ namespace libexeinfo { public string Information => GetInfo(Header, WinHeader); - public static string GetInfo(PEHeader header, WindowsHeader64 winheader) + static string GetInfo(PEHeader header, WindowsHeader64 winheader) { StringBuilder sb = new StringBuilder(); sb.Append(COFF.GetInfo(header.coff)); diff --git a/libexeinfo/PE/PE.cs b/libexeinfo/PE/PE.cs index 5842efa..6482aa4 100644 --- a/libexeinfo/PE/PE.cs +++ b/libexeinfo/PE/PE.cs @@ -72,16 +72,10 @@ namespace libexeinfo Initialize(); } - /// - /// The that contains the executable represented by this instance - /// public Stream BaseStream { get; } public bool IsBigEndian => false; - /// - /// If true this instance correctly represents a Microsoft Portable Executable - /// - public bool Recognized { get; private set; } - public string Type { get; private set; } + public bool Recognized { get; private set; } + public string Type { get; private set; } void Initialize() { @@ -100,7 +94,7 @@ namespace libexeinfo Marshal.Copy(buffer, 0, hdrPtr, buffer.Length); Header = (PEHeader)Marshal.PtrToStructure(hdrPtr, typeof(PEHeader)); Marshal.FreeHGlobal(hdrPtr); - Recognized = Header.signature == Signature; + Recognized = Header.signature == SIGNATURE; if(!Recognized) return; @@ -135,20 +129,20 @@ namespace libexeinfo /// Executable path. public static bool Identify(string path) { - FileStream BaseStream = File.Open(path, FileMode.Open, FileAccess.Read); - MZ BaseExecutable = new MZ(BaseStream); - if(!BaseExecutable.Recognized) return false; + FileStream baseStream = File.Open(path, FileMode.Open, FileAccess.Read); + MZ baseExecutable = new MZ(baseStream); + if(!baseExecutable.Recognized) return false; - if(BaseExecutable.Header.new_offset >= BaseStream.Length) return false; + if(baseExecutable.Header.new_offset >= baseStream.Length) return false; - BaseStream.Seek(BaseExecutable.Header.new_offset, SeekOrigin.Begin); + baseStream.Seek(baseExecutable.Header.new_offset, SeekOrigin.Begin); byte[] buffer = new byte[Marshal.SizeOf(typeof(PEHeader))]; - BaseStream.Read(buffer, 0, buffer.Length); + 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)); + PEHeader header = (PEHeader)Marshal.PtrToStructure(hdrPtr, typeof(PEHeader)); Marshal.FreeHGlobal(hdrPtr); - return Header.signature == Signature; + return header.signature == SIGNATURE; } /// @@ -158,20 +152,20 @@ namespace libexeinfo /// Stream containing the executable. public static bool Identify(FileStream stream) { - FileStream BaseStream = stream; - MZ BaseExecutable = new MZ(BaseStream); - if(!BaseExecutable.Recognized) return false; + FileStream baseStream = stream; + MZ baseExecutable = new MZ(baseStream); + if(!baseExecutable.Recognized) return false; - if(BaseExecutable.Header.new_offset >= BaseStream.Length) return false; + if(baseExecutable.Header.new_offset >= baseStream.Length) return false; - BaseStream.Seek(BaseExecutable.Header.new_offset, SeekOrigin.Begin); + baseStream.Seek(baseExecutable.Header.new_offset, SeekOrigin.Begin); byte[] buffer = new byte[Marshal.SizeOf(typeof(PEHeader))]; - BaseStream.Read(buffer, 0, buffer.Length); + 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)); + PEHeader header = (PEHeader)Marshal.PtrToStructure(hdrPtr, typeof(PEHeader)); Marshal.FreeHGlobal(hdrPtr); - return Header.signature == Signature; + return header.signature == SIGNATURE; } static WindowsHeader64 ToPlus(WindowsHeader header) diff --git a/libexeinfo/PE/Subsystems.cs b/libexeinfo/PE/Subsystems.cs index 7a23a1d..3a5a234 100644 --- a/libexeinfo/PE/Subsystems.cs +++ b/libexeinfo/PE/Subsystems.cs @@ -28,7 +28,7 @@ namespace libexeinfo { public partial class PE { - public static string SubsystemToString(Subsystems subsystem) + static string SubsystemToString(Subsystems subsystem) { switch(subsystem) {