Skip to content
Browse files

Move dead code

svn path=/old-code/; revision=133369
  • Loading branch information...
1 parent f79abb9 commit ee070414c08221450d9f902b8e191a761d1a0905 @migueldeicaza migueldeicaza committed May 2, 2009
Showing with 17,069 additions and 0 deletions.
  1. +41 −0 Mono.PEToolkit/BadImageException.cs
  2. +127 −0 Mono.PEToolkit/COFFHeader.cs
  3. +49 −0 Mono.PEToolkit/ChangeLog
  4. +186 −0 Mono.PEToolkit/Characteristics.cs
  5. +117 −0 Mono.PEToolkit/CheckSum.cs
  6. +409 −0 Mono.PEToolkit/CorHeader.cs
  7. +1 −0 Mono.PEToolkit/DEPRECATED
  8. +117 −0 Mono.PEToolkit/DOSHeader.cs
  9. +132 −0 Mono.PEToolkit/DataDir.cs
  10. +79 −0 Mono.PEToolkit/ExeSignature.cs
  11. +297 −0 Mono.PEToolkit/Image.cs
  12. +272 −0 Mono.PEToolkit/LEBitConverter.cs
  13. +278 −0 Mono.PEToolkit/MachineId.cs
  14. +15 −0 Mono.PEToolkit/Makefile
  15. +41 −0 Mono.PEToolkit/Mono.PEToolkit.dll.sources
  16. +698 −0 Mono.PEToolkit/PEHeader.cs
  17. +192 −0 Mono.PEToolkit/PEUtils.cs
  18. +1 −0 Mono.PEToolkit/README
  19. +143 −0 Mono.PEToolkit/RVA.cs
  20. +234 −0 Mono.PEToolkit/Section.cs
  21. +213 −0 Mono.PEToolkit/SectionCharacteristics.cs
  22. +132 −0 Mono.PEToolkit/Subsystem.cs
  23. +3 −0 Mono.PEToolkit/build/ChangeLog
  24. +23 −0 Mono.PEToolkit/build/Makefile
  25. +64 −0 Mono.PEToolkit/build/mddump.cs
  26. +42 −0 Mono.PEToolkit/build/mddump.exe.sources
  27. +14 −0 Mono.PEToolkit/build/srcgen/a.bat
  28. +10 −0 Mono.PEToolkit/build/srcgen/bitconv-types.xml
  29. +149 −0 Mono.PEToolkit/build/srcgen/bitconverter.xsl
  30. +34 −0 Mono.PEToolkit/build/srcgen/coded-id.xsl
  31. +549 −0 Mono.PEToolkit/build/srcgen/data/ilasm.xml
  32. +589 −0 Mono.PEToolkit/build/srcgen/data/md-schema.xml
  33. +36 −0 Mono.PEToolkit/build/srcgen/elem-type.xsl
  34. +72 −0 Mono.PEToolkit/build/srcgen/gen-utils.xsl
  35. +225 −0 Mono.PEToolkit/build/srcgen/rows.xsl
  36. +41 −0 Mono.PEToolkit/build/srcgen/table-id.xsl
  37. +69 −0 Mono.PEToolkit/build/srcgen/tabs-base.xsl
  38. +255 −0 Mono.PEToolkit/build/srcgen/tabs-decoder.xsl
  39. +53 −0 Mono.PEToolkit/build/srcgen/tabs.xsl
  40. +231 −0 Mono.PEToolkit/build/srcgen/xslt.wsf
  41. +56 −0 Mono.PEToolkit/metadata/AssemblyFlags.cs
  42. +20 −0 Mono.PEToolkit/metadata/BadMetaDataException.cs
  43. +20 −0 Mono.PEToolkit/metadata/ChangeLog
  44. +31 −0 Mono.PEToolkit/metadata/CodedTokenId.cs
  45. +53 −0 Mono.PEToolkit/metadata/ElementType.cs
  46. +40 −0 Mono.PEToolkit/metadata/GUIDHeap.cs
  47. +60 −0 Mono.PEToolkit/metadata/MDHeap.cs
  48. +219 −0 Mono.PEToolkit/metadata/MDStream.cs
  49. +105 −0 Mono.PEToolkit/metadata/MDTable.cs
  50. +158 −0 Mono.PEToolkit/metadata/MDToken.cs
  51. +89 −0 Mono.PEToolkit/metadata/MDUtils.cs
  52. +29 −0 Mono.PEToolkit/metadata/ManifestResourceAttributes.cs
  53. +237 −0 Mono.PEToolkit/metadata/MetaDataRoot.cs
  54. +149 −0 Mono.PEToolkit/metadata/MethodIL.cs
  55. +26 −0 Mono.PEToolkit/metadata/MethodSemanticsAttributes.cs
  56. +54 −0 Mono.PEToolkit/metadata/PInvokeAttributes.cs
  57. +74 −0 Mono.PEToolkit/metadata/Row.cs
  58. +4,887 −0 Mono.PEToolkit/metadata/Rows.cs
  59. +105 −0 Mono.PEToolkit/metadata/StringsHeap.cs
  60. +66 −0 Mono.PEToolkit/metadata/TableId.cs
  61. +1,375 −0 Mono.PEToolkit/metadata/Tables.cs
  62. +217 −0 Mono.PEToolkit/metadata/TablesHeap.cs
  63. +787 −0 Mono.PEToolkit/metadata/TablesHeapBase.cs
  64. +1,933 −0 Mono.PEToolkit/metadata/TabsDecoder.cs
  65. +46 −0 Mono.PEToolkit/metadata/TokenType.cs
View
41 Mono.PEToolkit/BadImageException.cs
@@ -0,0 +1,41 @@
+
+//
+// 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.
+//
+/*
+ * Copyright (c) 2002 Sergey Chaban <serge@wildwestsoftware.com>
+ */
+
+using System;
+
+namespace Mono.PEToolkit {
+
+ public class BadImageException : Exception {
+
+ public BadImageException() : base()
+ {
+ }
+
+ public BadImageException(string msg) : base(msg)
+ {
+ }
+ }
+
+}
View
127 Mono.PEToolkit/COFFHeader.cs
@@ -0,0 +1,127 @@
+
+//
+// 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.
+//
+/*
+ * Copyright (c) 2002 Sergey Chaban <serge@wildwestsoftware.com>
+ */
+
+using System;
+using System.IO;
+using System.Runtime.InteropServices;
+
+namespace Mono.PEToolkit {
+
+ public class COFFHeader {
+
+ private MachineId machine;
+ private short sections;
+ private uint tdStampRaw;
+ private uint symTabPtr;
+ private uint numSymbols;
+ private short optHeaderSize;
+ private Characteristics characteristics;
+
+ public MachineId Machine {
+ get { return machine; }
+ set { machine = value; }
+ }
+
+ public short NumberOfSections {
+ get { return sections; }
+ set { sections = value; }
+ }
+
+ public uint TimeDateStamp {
+ get { return tdStampRaw; }
+ set { tdStampRaw = value; }
+ }
+
+ public DateTime TimeStamp {
+ get {
+ return (new DateTime(1970, 1, 1) +
+ TimeSpan.FromSeconds(tdStampRaw)).ToLocalTime();
+ }
+ }
+
+ public uint PointerToSymbolTable {
+ get { return symTabPtr; }
+ set { symTabPtr = value; }
+ }
+
+ public uint NumberOfSymbols {
+ get { return numSymbols; }
+ set { numSymbols = value; }
+ }
+
+ public short SizeOfOptionalHeader {
+ get { return optHeaderSize; }
+ set { optHeaderSize = value; }
+ }
+
+ public Characteristics Characteristics {
+ get { return characteristics; }
+ set { characteristics = value; }
+ }
+
+ public void Read (BinaryReader reader)
+ {
+ machine = (MachineId) reader.ReadUInt16 ();
+ sections = reader.ReadInt16 ();
+ tdStampRaw = reader.ReadUInt32 ();
+ symTabPtr = reader.ReadUInt32 ();
+ numSymbols = reader.ReadUInt32 ();
+ optHeaderSize = reader.ReadInt16 ();
+ characteristics = (Characteristics) reader.ReadUInt16 ();
+ }
+
+ public void Write (BinaryWriter writer)
+ {
+ writer.Write ((ushort)machine);
+ writer.Write (sections);
+ writer.Write (tdStampRaw);
+ writer.Write (symTabPtr);
+ writer.Write (numSymbols);
+ writer.Write (optHeaderSize);
+ writer.Write ((ushort)characteristics);
+ }
+
+ public void Dump(TextWriter writer)
+ {
+ writer.WriteLine(
+ "Machine ID : {0}" + Environment.NewLine +
+ "Sections : {1}" + Environment.NewLine +
+ "Characteristics : {2}" + Environment.NewLine +
+ "timestamp : {3} ({4})" + Environment.NewLine
+ ,machine, sections, (ushort)characteristics,
+ TimeStamp, tdStampRaw.ToString("X")
+ );
+ }
+
+ public override string ToString()
+ {
+ StringWriter sw = new StringWriter();
+ Dump(sw);
+ return sw.ToString();
+ }
+
+ }
+}
+
View
49 Mono.PEToolkit/ChangeLog
@@ -0,0 +1,49 @@
+2003-03-01 Jackson Harper <jackson@latitudegeo.com>
+
+ * Image.cs: Write Metadata root
+
+2003-02-23 Peter Williams <peterw@ximian.com>
+
+ * makefile.gnu (all): Give this dependencies
+ so that we don't rebuild every time.
+ (all): Take two.
+
+2003-02-22 Jackson Harper <jackson@latitudegeo.com>
+
+ * Image.cs: Write CorHeader
+
+2003-02-22 Jackson Harper <jackson@latitudegeo.com>
+
+ * Image.cs: Write Sections
+
+2003-02-22 Jackson Harper <jackson@latitudegeo.com>
+
+ * Section.cs: Add method for writing Sections
+
+2003-02-21 Jackson Harper <jackson@latitudegeo.com>
+
+ * Section.cs: Add Method to write Headers
+
+2003-02-21 Jackson Harper <jackson@latitudegeo.com>
+
+ * Section.cs: Make Read method safe
+
+2003-02-21 Jackson Harper <jackson@latitudegeo.com>
+
+ * Section.cs: Convert Header to a class and lowers it accesability.
+
+2003-02-17 Jackson Harper <jackson@latitudegeo.com>
+
+ * COFFHeader.cs: Convert to class, use Read and Write methods. Compress some code
+ * CheckSum.cs: Access Lfanew through property not field.
+ * CorHeader.cs: Convert to class, use Read and Write methods.
+ * DOSHeader.cs: Convert to class, use Read and Write methods, and Init method for creating
+ new DOSHeaders
+ * DataDir.cs: Convert to class, use Read and Write methods.
+ * Image.cs: Access lfanew through property, add Write method.
+ * PEHeader.cs: Convert to class, use Read and Write methods.
+ * PEUtils.cs: Add GetString method for converting byte pointers to strings.
+ * RVA.cs: Add Write method
+ * Section.cs: Use PEUtils.GetString method instead of new string (byte*)
+ * makefuile.gnu: Compile all the files I want compiled ;-)
+
View
186 Mono.PEToolkit/Characteristics.cs
@@ -0,0 +1,186 @@
+
+//
+// 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.
+//
+/*
+ * Copyright (c) 2002 Sergey Chaban <serge@wildwestsoftware.com>
+ */
+
+using System;
+
+namespace Mono.PEToolkit {
+
+ [Flags]
+ public enum Characteristics : ushort {
+
+
+ /// <summary>
+ /// Relocation info stripped from file.
+ /// </summary>
+ /// <remarks>
+ /// IMAGE_FILE_RELOCS_STRIPPED
+ /// </remarks>
+ RELOCS_STRIPPED = 0x0001,
+
+
+
+ /// <summary>
+ /// File is executable
+ /// (i.e. file is neither object file nor library file,
+ /// so there are no unresolved externel references).
+ /// </summary>
+ /// <remarks>
+ /// IMAGE_FILE_EXECUTABLE_IMAGE
+ /// </remarks>
+ EXECUTABLE_IMAGE = 0x0002,
+
+
+ /// <summary>
+ /// Line nunbers stripped from file.
+ /// </summary>
+ /// <remarks>
+ /// IMAGE_FILE_LINE_NUMS_STRIPPED
+ /// </remarks>
+ LINE_NUMS_STRIPPED = 0x0004,
+
+
+ /// <summary>
+ /// Local symbols stripped from file.
+ /// </summary>
+ /// <remarks>
+ /// IMAGE_FILE_LOCAL_SYMS_STRIPPED
+ /// </remarks>
+ LOCAL_SYMS_STRIPPED = 0x0008,
+
+
+ /// <summary>
+ /// Agressively trim working set
+ /// </summary>
+ /// <remarks>
+ /// IMAGE_FILE_AGGRESIVE_WS_TRIM
+ /// </remarks>
+ AGGRESIVE_WS_TRIM = 0x0010,
+
+
+ /// <summary>
+ /// App can handle >2gb addresses
+ /// </summary>
+ /// <remarks>
+ /// IMAGE_FILE_LARGE_ADDRESS_AWARE
+ /// </remarks>
+ LARGE_ADDRESS_AWARE = 0x0020,
+
+
+ /// <summary>
+ /// Bytes of machine word are reversed.
+ /// </summary>
+ /// <remarks>
+ /// IMAGE_FILE_BYTES_REVERSED_LO
+ /// </remarks>
+ BYTES_REVERSED_LO = 0x0080,
+
+
+ /// <summary>
+ /// 32 bit word machine.
+ /// </summary>
+ /// <remarks>
+ /// IMAGE_FILE_32BIT_MACHINE
+ /// </remarks>
+ MACHINE_32BIT = 0x0100,
+
+
+ /// <summary>
+ /// Debugging info stripped from file in .DBG file
+ /// </summary>
+ /// <remarks>
+ /// IMAGE_FILE_DEBUG_STRIPPED
+ /// </remarks>
+ DEBUG_STRIPPED = 0x0200,
+
+
+ /// <summary>
+ /// If Image is on removable media, copy and run from the swap file.
+ /// </summary>
+ /// <remarks>
+ /// IMAGE_FILE_REMOVABLE_RUN_FROM_SWAP
+ /// </remarks>
+ REMOVABLE_RUN_FROM_SWAP = 0x0400,
+
+
+ /// <summary>
+ /// If Image is on Net, copy and run from the swap file.
+ /// </summary>
+ /// <remarks>
+ /// IMAGE_FILE_NET_RUN_FROM_SWAP
+ /// </remarks>
+ NET_RUN_FROM_SWAP = 0x0800,
+
+
+ /// <summary>
+ /// This flag is used to indicate that the file
+ /// is a system sile, such as device driver.
+ /// </summary>
+ /// <remarks>
+ /// IMAGE_FILE_SYSTEM
+ /// </remarks>
+ SYSTEM = 0x1000,
+
+
+ /// <summary>
+ /// This flag indicates that the file
+ /// is a dynamic library (DLL).
+ /// </summary>
+ /// <remarks>
+ /// IMAGE_FILE_DLL
+ /// </remarks>
+ DLL = 0x2000,
+
+
+ /// <summary>
+ /// File should only be run on a uni-processor (UP) machine.
+ /// </summary>
+ /// <remarks>
+ /// IMAGE_FILE_UP_SYSTEM_ONLY
+ /// </remarks>
+ UP_SYSTEM_ONLY = 0x4000,
+
+
+ /// <summary>
+ /// Bytes of machine word are reversed.
+ /// </summary>
+ /// <remarks>
+ /// IMAGE_FILE_BYTES_REVERSED_HI
+ /// </remarks>
+ BYTES_REVERSED_HI = 0x8000,
+
+
+
+ /// <summary>
+ /// Default flags that must be set in CIL-only image.
+ /// </summary>
+ /// <remarks>
+ /// See Partition II, 24.2.2.1
+ /// </remarks>
+ CIL_DEFAULT = LINE_NUMS_STRIPPED |
+ LOCAL_SYMS_STRIPPED |
+ DEBUG_STRIPPED
+ }
+
+}
View
117 Mono.PEToolkit/CheckSum.cs
@@ -0,0 +1,117 @@
+
+//
+// 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.
+//
+/*
+ * Copyright (c) 2002 Sergey Chaban <serge@wildwestsoftware.com>
+ */
+
+// see http://cvs.winehq.com/cvsweb/wine/dlls/imagehlp/modify.c
+// starting from Revision 1.8
+
+using System;
+using System.IO;
+
+namespace Mono.PEToolkit {
+
+ public sealed class CheckSum {
+
+ private CheckSum()
+ {
+ // Never instantiated.
+ }
+
+
+ public static uint Calc(string peFile)
+ {
+ uint res = 0;
+
+ FileInfo pe = new FileInfo(peFile);
+ if (!pe.Exists) {
+ throw new Exception("CheckSum : Invalid file path.");
+ }
+
+ using (BinaryReader reader = new BinaryReader(pe.OpenRead())) {
+ if (!reader.BaseStream.CanSeek) {
+ throw new Exception("Can't seek.");
+ }
+
+ DOSHeader dosHdr = new DOSHeader();
+ COFFHeader coffHdr = new COFFHeader();
+ PEHeader peHdr = new PEHeader();
+
+ dosHdr.Read (reader);
+ reader.BaseStream.Position = dosHdr.Lfanew;
+ ExeSignature peSig = (ExeSignature) reader.ReadUInt16();
+ if (peSig != ExeSignature.NT) {
+ throw new BadImageException("Checksum : Invalid image format, cannot find PE signature.");
+ }
+
+ peSig = (ExeSignature) reader.ReadUInt16();
+ if (peSig != ExeSignature.NT2) {
+ throw new BadImageException("Checksum : Invalid image format, cannot find PE signature.");
+ }
+
+ coffHdr.Read(reader);
+ peHdr.Read(reader);
+
+ uint oldSum = peHdr.CheckSum;
+ reader.BaseStream.Position = 0;
+ long len = pe.Length;
+ long whole = len >> 1;
+ uint sum = 0;
+ uint hi, lo;
+ for (long i = whole; --i >= 0;) {
+ sum += reader.ReadUInt16();
+ hi = sum >> 16;
+ if (hi != 0) {
+ sum = hi + (sum & 0xFFFF);
+ }
+ }
+ if ((len & 1L) != 0) {
+ sum += (uint) reader.ReadByte();
+ hi = sum >> 16;
+ if (hi != 0) {
+ sum = hi + (sum & 0xFFFF);
+ }
+ }
+
+ // fix low word of checksum
+ lo = oldSum & 0xFFFF;
+ if ((sum & 0xFFFF) >= lo) {
+ sum -= lo;
+ } else {
+ sum = (((sum & 0xFFFF) - lo) & 0xFFFF) - 1;
+ }
+
+ // fix high word of checksum
+ hi = oldSum >> 16;
+ if ((sum & 0xFFFF) >= hi) {
+ sum -= hi;
+ } else {
+ sum = (((sum & 0xFFFF) - hi) & 0xFFFF) - 1;
+ }
+ }
+
+ return res;
+ }
+ }
+
+}
View
409 Mono.PEToolkit/CorHeader.cs
@@ -0,0 +1,409 @@
+
+//
+// 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.
+//
+/*
+ * Copyright (c) 2002 Sergey Chaban <serge@wildwestsoftware.com>
+ */
+
+using System;
+using System.IO;
+using System.Runtime.InteropServices;
+
+using Mono.PEToolkit.Metadata;
+
+namespace Mono.PEToolkit {
+
+ [Flags]
+ public enum CorFlags : uint {
+ /// <summary>
+ /// COMIMAGE_FLAGS_ILONLY
+ /// </summary>
+ ILONLY = 0x00000001,
+
+ /// <summary>
+ /// COMIMAGE_FLAGS_32BITREQUIRED
+ /// </summary>
+ REQUIRED_32BIT = 0x00000002,
+
+ /// <summary>
+ /// COMIMAGE_FLAGS_IL_LIBRARY
+ /// </summary>
+ IL_LIBRARY = 0x00000004,
+
+ /// <summary>
+ /// COMIMAGE_FLAGS_TRACKDEBUGDATA
+ /// </summary>
+ TRACKDEBUGDATA = 0x00010000,
+ }
+
+
+ /// <summary>
+ /// CLR 2.0 header structure.
+ /// IMAGE_COR20_HEADER
+ /// </summary>
+ public class CorHeader {
+ // Header versioning
+ internal uint cb;
+ internal short runtimeMaj;
+ internal short runtimeMin;
+
+ // Symbol table and startup information.
+ internal DataDir meta;
+ internal CorFlags flags;
+ internal uint entryTok;
+
+ // Binding information.
+ internal DataDir rsrc;
+ internal DataDir strongSig;
+
+ // Regular fixup and binding information.
+ internal DataDir codeManTab;
+ internal DataDir vtab;
+ internal DataDir jumps;
+
+ // Managed Native Code.
+ internal DataDir eeInfo;
+ internal DataDir helper;
+ internal DataDir dynInfo;
+ internal DataDir delayInfo;
+ internal DataDir modImg;
+ internal DataDir extFixups;
+ internal DataDir ridMap;
+ internal DataDir dbgMap;
+
+ // obsolete?
+ internal DataDir ipMap;
+
+
+ //
+ // Accessors
+ //
+
+ public uint Size {
+ get {
+ return cb;
+ }
+ set {
+ cb = value;
+ }
+ }
+
+ public short MajorRuntimeVersion {
+ get {
+ return runtimeMaj;
+ }
+ set {
+ runtimeMaj = value;
+ }
+ }
+
+ public short MinorRuntimeVersion {
+ get {
+ return runtimeMin;
+ }
+ set {
+ runtimeMin = value;
+ }
+ }
+
+ public string RuntimeVersion {
+ get {
+ return String.Format("{0}.{1}", runtimeMaj, runtimeMin);
+ }
+ }
+
+ public DataDir MetaData {
+ get {
+ return meta;
+ }
+ set {
+ meta = value;
+ }
+ }
+
+ public CorFlags Flags {
+ get {
+ return flags;
+ }
+ set {
+ flags = value;
+ }
+ }
+
+ public MDToken EntryPointToken {
+ get {
+ return entryTok;
+ }
+ set {
+ entryTok = value;
+ }
+ }
+
+ public DataDir Resources {
+ get {
+ return rsrc;
+ }
+ set {
+ rsrc = value;
+ }
+ }
+
+ public DataDir StrongNameSignature {
+ get {
+ return strongSig;
+ }
+ set {
+ strongSig = value;
+ }
+ }
+
+ public DataDir CodeManagerTable {
+ get {
+ return codeManTab;
+ }
+ set {
+ codeManTab = value;
+ }
+ }
+
+ public DataDir VTableFixups {
+ get {
+ return vtab;
+ }
+ set {
+ vtab = value;
+ }
+ }
+
+ public DataDir ExportAddressTableJumps {
+ get {
+ return jumps;
+ }
+ set {
+ jumps = value;
+ }
+ }
+
+
+ public DataDir EEInfoTable {
+ get {
+ return eeInfo;
+ }
+ set {
+ eeInfo = value;
+ }
+ }
+
+ public DataDir HelperTable {
+ get {
+ return helper;
+ }
+ set {
+ helper = value;
+ }
+ }
+
+ public DataDir DynamicInfo {
+ get {
+ return dynInfo;
+ }
+ set {
+ dynInfo = value;
+ }
+ }
+
+ public DataDir DelayLoadInfo {
+ get {
+ return delayInfo;
+ }
+ set {
+ delayInfo = value;
+ }
+ }
+
+ public DataDir ModuleImage {
+ get {
+ return modImg;
+ }
+ set {
+ modImg = value;
+ }
+ }
+
+ public DataDir ExternalFixups {
+ get {
+ return extFixups;
+ }
+ set {
+ extFixups = value;
+ }
+ }
+
+ public DataDir RidMap {
+ get {
+ return ridMap;
+ }
+ set {
+ ridMap = value;
+ }
+ }
+
+ public DataDir DebugMap {
+ get {
+ return dbgMap;
+ }
+ set {
+ dbgMap = value;
+ }
+ }
+
+
+ public DataDir IPMap {
+ get {
+ return ipMap;
+ }
+ set {
+ ipMap = value;
+ }
+ }
+
+
+ public void Read(BinaryReader reader)
+ {
+ // Header versioning
+ cb = reader.ReadUInt32 ();
+
+ runtimeMaj = reader.ReadInt16 ();
+ runtimeMin = reader.ReadInt16 ();
+
+
+ // Symbol table and startup information.
+ meta = new DataDir (reader);
+ flags = (CorFlags) reader.ReadUInt32 ();
+ entryTok = reader.ReadUInt32 ();
+
+ // Binding information.
+ rsrc = new DataDir (reader);
+ strongSig = new DataDir (reader);
+
+ // Regular fixup and binding information.
+ codeManTab = new DataDir (reader);
+ vtab = new DataDir (reader);
+ jumps = new DataDir (reader);
+
+ // Managed Native Code.
+ eeInfo = new DataDir (reader);
+ helper = new DataDir (reader);
+ dynInfo = new DataDir (reader);
+ delayInfo = new DataDir (reader);
+ modImg = new DataDir (reader);
+ extFixups = new DataDir (reader);
+ ridMap = new DataDir (reader);
+ dbgMap = new DataDir (reader);
+
+ // obsolete?
+ ipMap = new DataDir (reader);
+ }
+
+ public void Write (BinaryWriter writer)
+ {
+ // Header versioning
+ writer.Write (cb);
+
+ writer.Write (runtimeMaj);
+ writer.Write (runtimeMin);
+
+ // Symbol table and startup information.
+ meta.Write (writer);
+ writer.Write ((uint)flags);
+ writer.Write (entryTok);
+
+ // Binding information.
+ rsrc.Write (writer);
+ strongSig.Write (writer);
+
+ // Regular fixup and binding information.
+ codeManTab.Write (writer);
+ vtab.Write (writer);
+ jumps.Write (writer);
+
+ // Managed Native Code.
+ eeInfo.Write (writer);
+ helper.Write (writer);
+ dynInfo.Write (writer);
+ delayInfo.Write (writer);
+ modImg.Write (writer);
+ extFixups.Write (writer);
+ ridMap.Write (writer);
+ dbgMap.Write (writer);
+
+ // obsolete?
+ ipMap.Write (writer);
+ }
+
+
+ public void Dump(TextWriter writer)
+ {
+ writer.WriteLine(String.Format (
+ "Header Size : {0}" + Environment.NewLine +
+ "Runtime Version : {1}" + Environment.NewLine +
+ "MetaData Root : {2}" + Environment.NewLine +
+ "Flags : {3}" + Environment.NewLine +
+ "Entry Point Token : {4}" + Environment.NewLine +
+ "Resources : {5}" + Environment.NewLine +
+ "Strong Name Signature : {6}" + Environment.NewLine +
+ "Code Manager Table : {7}" + Environment.NewLine +
+ "VTable Fixups : {8}" + Environment.NewLine +
+ "Export Address Table Jumps : {9}" + Environment.NewLine +
+ "EE Info Table : {10}" + Environment.NewLine +
+ "Helper Table : {11}" + Environment.NewLine +
+ "Dynamic Info : {12}" + Environment.NewLine +
+ "Delay Load Info : {13}" + Environment.NewLine +
+ "Module Image : {14}" + Environment.NewLine +
+ "External Fixups : {15}" + Environment.NewLine +
+ "Rid Map : {16}" + Environment.NewLine +
+ "Debug Map : {17}" + Environment.NewLine +
+ "IP Map : {18}" + Environment.NewLine +
+ "Runtime Major : {19}" + Environment.NewLine +
+ "Runtime Minor : {20}" + Environment.NewLine,
+ cb,
+ RuntimeVersion,
+ meta, null, EntryPointToken,
+ rsrc, strongSig,
+ codeManTab, vtab, jumps,
+ eeInfo, helper, dynInfo, delayInfo, modImg, extFixups,
+ ridMap, dbgMap, ipMap, MajorRuntimeVersion, MinorRuntimeVersion
+ ));
+
+ }
+
+ /// <summary>
+ /// </summary>
+ /// <returns></returns>
+ public override string ToString() {
+ StringWriter sw = new StringWriter();
+ Dump(sw);
+ return sw.ToString();
+ }
+
+
+ }
+}
View
1 Mono.PEToolkit/DEPRECATED
@@ -0,0 +1 @@
+This module has not been maintained
View
117 Mono.PEToolkit/DOSHeader.cs
@@ -0,0 +1,117 @@
+
+//
+// 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.
+//
+/*
+ * Copyright (c) 2002 Sergey Chaban <serge@wildwestsoftware.com>
+ */
+
+using System;
+using System.IO;
+using System.Runtime.InteropServices;
+
+namespace Mono.PEToolkit {
+
+ public class DOSHeader {
+
+ private readonly int OpenSize = 60;
+ private readonly int CloseSize = 64;
+
+ private byte[] open_data; // First 60 bytes of data
+ private byte[] close_data; // Last 64 bytes of data
+
+ // File address of new exe header.
+ private uint lfanew;
+
+ public DOSHeader ()
+ {
+ Init ();
+ }
+
+ public DOSHeader (BinaryReader reader)
+ {
+ Read (reader);
+ }
+
+ public uint Lfanew {
+ get { return lfanew; }
+ }
+
+ public void Read (BinaryReader reader)
+ {
+ open_data = reader.ReadBytes (OpenSize);
+ lfanew = reader.ReadUInt32 ();
+ close_data = reader.ReadBytes (CloseSize);
+ }
+
+ public void Write (BinaryWriter writer)
+ {
+ writer.Write (open_data);
+ writer.Write (lfanew);
+ writer.Write (close_data);
+ }
+
+ public void Init ()
+ {
+ open_data = new byte[] { 0x4D, 0x5A, 0x0, 0x0, 0xE7, 0x0, 0x0, 0x0,
+ 0x4, 0x0, 0x0, 0x0, 0xFF, 0xFF, 0x0, 0x0,
+ 0xB8, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0,
+ 0x40, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0,
+ 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0,
+ 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0,
+ 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0,
+ 0x0, 0x0, 0x0, 0x0 };
+
+ close_data = new byte[] { 0xE, 0x1F, 0xBA, 0xE, 0x0, 0xB4, 0x9, 0xCD,
+ 0x21, 0xB8, 0x1, 0x4C, 0xCD, 0x21,0x54, 0x68,
+ 0x69, 0x73, 0x20, 0x70, 0x72, 0x6F, 0x67, 0x72,
+ 0x61, 0x6D, 0x20, 0x63, 0x61, 0x6E, 0x6E, 0x6F,
+ 0x74, 0x20, 0x62, 0x65, 0x20, 0x72, 0x75, 0x6E,
+ 0x20, 0x69, 0x6E, 0x20, 0x44, 0x4F, 0x53, 0x20,
+ 0x6D, 0x6F, 0x64, 0x65, 0x2E, 0xD, 0xD, 0xA,
+ 0x24, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0 };
+
+ }
+
+ /// <summary>
+ /// </summary>
+ /// <param name="writer"></param>
+ public void Dump(TextWriter writer)
+ {
+ writer.WriteLine(
+ "New header offset : {0}",
+ lfanew + " (0x" + lfanew.ToString("X") + ")"
+ );
+ }
+
+ /// <summary>
+ /// </summary>
+ /// <returns></returns>
+ public override string ToString()
+ {
+ StringWriter sw = new StringWriter();
+ Dump(sw);
+ return sw.ToString();
+ }
+
+ }
+
+}
+
View
132 Mono.PEToolkit/DataDir.cs
@@ -0,0 +1,132 @@
+
+//
+// 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.
+//
+/*
+ * Copyright (c) 2002 Sergey Chaban <serge@wildwestsoftware.com>
+ */
+
+using System;
+using System.IO;
+using System.Runtime.InteropServices;
+
+namespace Mono.PEToolkit {
+
+ /// <summary>
+ /// IMAGE_DATA_DIRECTORY.
+ /// </summary>
+ public class DataDir {
+
+ public static readonly DataDir Null;
+
+ public RVA virtAddr;
+ public uint size;
+
+ static DataDir ()
+ {
+ Null = new DataDir ();
+ Null.virtAddr = 0;
+ Null.size = 0;
+ }
+
+ public DataDir () {
+
+ }
+
+ public DataDir (BinaryReader reader)
+ {
+ Read (reader);
+ }
+
+ public void Read (BinaryReader reader)
+ {
+ virtAddr = new RVA (reader.ReadUInt32 ());
+ size = reader.ReadUInt32 ();
+ }
+
+ public void Write (BinaryWriter writer)
+ {
+ virtAddr.Write (writer);
+ writer.Write (size);
+ }
+
+ public RVA VirtualAddress {
+ get {
+ return virtAddr;
+ }
+ set {
+ virtAddr = value;
+ }
+ }
+
+ public uint Size {
+ get {
+ return size;
+ }
+ set {
+ size = value;
+ }
+ }
+
+ public bool IsNull {
+ get {
+ return (this == Null);
+ }
+ }
+
+ public override int GetHashCode()
+ {
+ return (virtAddr.GetHashCode() ^ (int)(size << 1));
+ }
+
+ public override bool Equals(object obj)
+ {
+ bool res = (obj is DataDir);
+ if (res) {
+ DataDir that = (DataDir) obj;
+ res = (this.virtAddr == that.virtAddr) &&
+ (this.size == that.size);
+ }
+ return res;
+ }
+
+ public static bool operator == (DataDir d1, DataDir d2)
+ {
+ return d1.Equals(d2);
+ }
+
+ public static bool operator != (DataDir d1, DataDir d2)
+ {
+ return !d1.Equals(d2);
+ }
+
+ /// <summary>
+ /// </summary>
+ /// <returns></returns>
+ public override string ToString()
+ {
+ if (this.IsNull) return "NULL";
+ return String.Format("RVA = {0}, size = 0x{1}", virtAddr, size.ToString("X"));
+ }
+
+ }
+
+}
+
View
79 Mono.PEToolkit/ExeSignature.cs
@@ -0,0 +1,79 @@
+
+//
+// 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.
+//
+/*
+ * Copyright (c) 2002 Sergey Chaban <serge@wildwestsoftware.com>
+ */
+
+namespace Mono.PEToolkit {
+
+ public enum ExeSignature : ushort {
+
+ UNKNOWN = 0,
+
+ /// <summary>
+ /// "MZ"
+ /// </summary>
+ /// <remarks>
+ /// IMAGE_DOS_SIGNATURE
+ /// </remarks>
+ DOS = 0x5A4D,
+
+
+ /// <summary>
+ /// "NE"
+ /// </summary>
+ /// <remarks>
+ /// IMAGE_OS2_SIGNATURE
+ /// </remarks>
+ OS2 = 0x454E,
+
+
+ /// <summary>
+ /// "LE"
+ /// </summary>
+ /// <remarks>
+ /// IMAGE_OS2_SIGNATURE_LE
+ /// </remarks>
+ OS2_LE = 0x454C,
+
+
+ /// <summary>
+ /// "LE"
+ /// </summary>
+ /// <remarks>
+ /// IMAGE_VXD_SIGNATURE
+ /// </remarks>
+ VXD = OS2_LE,
+
+
+ /// <summary>
+ /// "PE", the complete signature is "PE\0\0"
+ /// (that is, NT followed by NT2).
+ /// </summary>
+ /// <remarks>
+ /// IMAGE_NT_SIGNATURE
+ /// </remarks>
+ NT = 0x4550,
+ NT2 = 0
+ }
+
+}
View
297 Mono.PEToolkit/Image.cs
@@ -0,0 +1,297 @@
+
+//
+// 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.
+//
+/*
+ * Copyright (c) 2002 Sergey Chaban <serge@wildwestsoftware.com>
+ */
+
+using System;
+using System.IO;
+using System.Collections;
+using System.Runtime.InteropServices;
+
+using Mono.PEToolkit.Metadata;
+
+namespace Mono.PEToolkit {
+
+ public class Image : IDisposable {
+
+ internal DOSHeader dosHdr;
+ internal COFFHeader coffHdr;
+ internal PEHeader peHdr;
+
+ internal CorHeader corHdr;
+
+ internal Hashtable sections;
+ // File position right after PEHeader (NT Optional Header).
+ protected long sectionsPos;
+
+ private MetaDataRoot mdRoot;
+
+ private string name;
+ private bool open;
+ internal BinaryReader reader;
+
+ public Image(string name)
+ {
+ this.name = name;
+ open = false;
+ reader = null;
+
+ mdRoot = null;
+
+ dosHdr = new DOSHeader();
+ coffHdr = new COFFHeader();
+ peHdr = new PEHeader();
+ corHdr = new CorHeader();
+
+ sections = new Hashtable();
+ sectionsPos = -1;
+ }
+
+ ~Image()
+ {
+ Close();
+ }
+
+
+ public Hashtable Sections {
+ get {
+ return sections;
+ }
+ }
+
+ public void Open()
+ {
+ lock (this) if (!open) {
+ FileInfo pe = new FileInfo(name);
+ if (!pe.Exists) {
+ throw new Exception("Invalid file path.");
+ }
+
+ reader = new BinaryReader(new BufferedStream(pe.OpenRead()));
+ if (!reader.BaseStream.CanSeek) {
+ throw new Exception("Can't seek.");
+ }
+
+ open = true;
+ }
+ }
+
+ public void Close()
+ {
+ lock (this) if (open) {
+ reader.Close();
+ open = false;
+ }
+ }
+
+ // IDisposable
+ public void Dispose()
+ {
+ Close();
+ }
+
+
+ public bool IsCLI {
+ get {
+ return peHdr.IsCLIImage;
+ }
+ }
+
+ public MetaDataRoot MetadataRoot {
+ get {
+ return mdRoot;
+ }
+ }
+
+ /// <summary>
+ /// </summary>
+ public void ReadHeaders()
+ {
+ if (!open) {
+ throw new Exception("You must open image before trying to read it.");
+ }
+
+ dosHdr.Read(reader);
+ reader.BaseStream.Position = dosHdr.Lfanew;
+ ExeSignature peSig = (ExeSignature) reader.ReadUInt16();
+ if (peSig != ExeSignature.NT) {
+ throw new Exception ("Invalid image format: cannot find PE signature.");
+ }
+ peSig = (ExeSignature) reader.ReadUInt16();
+ if (peSig != ExeSignature.NT2) {
+ throw new Exception ("Invalid image format: cannot find PE signature.");
+ }
+
+ coffHdr.Read(reader);
+ peHdr.Read(reader);
+
+ sectionsPos = reader.BaseStream.Position;
+ ReadSections();
+
+ if (this.IsCLI) {
+
+ reader.BaseStream.Position = RVAToVA(peHdr.CLIHdrDir.virtAddr);
+ corHdr.Read (reader);
+
+ mdRoot = new MetaDataRoot(this);
+ reader.BaseStream.Position = RVAToVA(corHdr.MetaData.virtAddr);
+ mdRoot.Read(reader);
+ }
+
+ }
+
+ public void WriteHeaders (BinaryWriter writer)
+ {
+ dosHdr.Write (writer);
+ writer.BaseStream.Position = dosHdr.Lfanew;
+ writer.Write ((ushort)ExeSignature.NT);
+ writer.Write ((ushort)ExeSignature.NT2);
+
+ coffHdr.Write (writer);
+ peHdr.Write (writer);
+
+ WriteSections (writer);
+
+ if (this.IsCLI) {
+
+ writer.BaseStream.Position = RVAToVA (peHdr.CLIHdrDir.virtAddr);
+ corHdr.Write (writer);
+
+ long pos = RVAToVA (corHdr.MetaData.virtAddr);
+ writer.BaseStream.Position = pos;
+ mdRoot.Write (writer);
+
+ }
+
+ }
+
+ /// <summary>
+ /// </summary>
+ protected void ReadSections()
+ {
+ if (sectionsPos < 0) {
+ throw new Exception("Read headers first.");
+ }
+ reader.BaseStream.Position = sectionsPos;
+
+ int n = coffHdr.NumberOfSections;
+ for (int i = n; --i >=0;) {
+ Section sect = new Section();
+ sect.Read(reader);
+ sections [sect.Name] = sect;
+ }
+ }
+
+ protected void WriteSections (BinaryWriter writer)
+ {
+ foreach (Section section in sections.Values) {
+ section.Write (writer);
+ }
+ }
+
+
+ /// <summary>
+ /// </summary>
+ /// <param name="writer"></param>
+ public void Dump(TextWriter writer)
+ {
+ writer.WriteLine (
+ "COFF Header:" + Environment.NewLine +
+ coffHdr.ToString() + Environment.NewLine +
+ "PE Header:" + Environment.NewLine +
+ peHdr.ToString() + Environment.NewLine +
+ "Core Header:" + Environment.NewLine +
+ corHdr.ToString()
+ );
+ }
+
+
+ /// <summary>
+ /// </summary>
+ /// <returns></returns>
+ public override string ToString()
+ {
+ StringWriter sw = new StringWriter();
+ Dump(sw);
+ return sw.ToString();
+ }
+
+
+ /// <summary>
+ /// Returns name of the section for the given RVA.
+ /// </summary>
+ /// <param name="rva"></param>
+ /// <returns></returns>
+ public string RVAToSectionName(RVA rva)
+ {
+ string res = null;
+ foreach (Section s in Sections.Values) {
+ RVA sva = s.VirtualAddress;
+ if (rva >= sva && rva < sva + s.SizeOfRawData) {
+ res = s.Name;
+ break;
+ }
+ }
+ return res;
+ }
+
+ public long RVAToVA(RVA rva)
+ {
+ string sectName = RVAToSectionName(rva);
+ long res = 0;
+ if (sectName != null) {
+ Section s = (Section) Sections [sectName];
+ res = rva + (s.PointerToRawData - s.VirtualAddress);
+ }
+ return res;
+ }
+
+ public MetaDataRoot MetaDataRoot {
+ get {
+ return mdRoot;
+ }
+ }
+
+ public void DumpStreamHeader(TextWriter writer, string name)
+ {
+ if (mdRoot == null || name == null || name == String.Empty || writer == null) return;
+ writer.Write(name + " header: ");
+ MDStream s = MetaDataRoot.Streams[name] as MDStream;
+ if (s != null) {
+ writer.WriteLine();
+ writer.WriteLine(s);
+ } else {
+ writer.WriteLine("not present.");
+ writer.WriteLine();
+ }
+ }
+
+ public void DumpStreamHeader(string name)
+ {
+ DumpStreamHeader(Console.Out, name);
+ }
+
+ }
+
+}
+
View
272 Mono.PEToolkit/LEBitConverter.cs
@@ -0,0 +1,272 @@
+
+//
+// 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.
+//
+
+// Auto-generated file - DO NOT EDIT!
+// Please edit bitconverter.xsl if you want to make changes.
+
+using System;
+
+namespace Mono.PEToolkit {
+
+ /// <summary>
+ /// Little-endian bit converter.
+ /// </summary>
+ public sealed class LEBitConverter {
+
+ internal interface IConverter {
+
+ short ToInt16(byte [] val, int idx);
+ ushort ToUInt16(byte [] val, int idx);
+ int ToInt32(byte [] val, int idx);
+ uint ToUInt32(byte [] val, int idx);
+ long ToInt64(byte [] val, int idx);
+ ulong ToUInt64(byte [] val, int idx);
+
+ }
+
+ public static readonly bool Native = System.BitConverter.IsLittleEndian;
+
+ private static readonly IConverter impl = System.BitConverter.IsLittleEndian
+ ? new LEConverter() as IConverter
+ : new BEConverter() as IConverter;
+
+
+
+
+ private LEBitConverter()
+ {
+ // Never instantiated.
+ }
+
+ ///<summary></summary>
+ unsafe public static short SwapInt16(short x)
+ {
+ short* p = stackalloc short [1];
+ *p = x;
+ byte* bp = (byte*) p;
+ byte b = bp [0];
+ bp [0] = bp [1];
+ bp [1] = b;
+ return *p;
+ }
+
+ ///<summary></summary>
+ unsafe public static ushort SwapUInt16(ushort x)
+ {
+ ushort* p = stackalloc ushort [1];
+ *p = x;
+ byte* bp = (byte*) p;
+ byte b = bp [0];
+ bp [0] = bp [1];
+ bp [1] = b;
+ return *p;
+ }
+
+ ///<summary></summary>
+ unsafe public static int SwapInt32(int x)
+ {
+ int* p = stackalloc int [1];
+ *p = x;
+ byte* bp = (byte*) p;
+ byte b = bp [0];
+ bp [0] = bp [3];
+ bp [3] = b;
+ b = bp [1];
+ bp [1] = bp [2];
+ bp [2] = b;
+ return *p;
+ }
+
+ ///<summary></summary>
+ unsafe public static uint SwapUInt32(uint x)
+ {
+ uint* p = stackalloc uint [1];
+ *p = x;
+ byte* bp = (byte*) p;
+ byte b = bp [0];
+ bp [0] = bp [3];
+ bp [3] = b;
+ b = bp [1];
+ bp [1] = bp [2];
+ bp [2] = b;
+ return *p;
+ }
+
+ ///<summary></summary>
+ unsafe public static long SwapInt64(long x)
+ {
+ long* p = stackalloc long [1];
+ *p = x;
+ byte* bp = (byte*) p;
+ byte b = bp [0];
+ bp [0] = bp [7];
+ bp [7] = b;
+ b = bp [1];
+ bp [1] = bp [6];
+ bp [6] = b;
+ b = bp [2];
+ bp [2] = bp [5];
+ bp [5] = b;
+ b = bp [3];
+ bp [3] = bp [4];
+ bp [4] = b;
+ return *p;
+ }
+
+ ///<summary></summary>
+ unsafe public static ulong SwapUInt64(ulong x)
+ {
+ ulong* p = stackalloc ulong [1];
+ *p = x;
+ byte* bp = (byte*) p;
+ byte b = bp [0];
+ bp [0] = bp [7];
+ bp [7] = b;
+ b = bp [1];
+ bp [1] = bp [6];
+ bp [6] = b;
+ b = bp [2];
+ bp [2] = bp [5];
+ bp [5] = b;
+ b = bp [3];
+ bp [3] = bp [4];
+ bp [4] = b;
+ return *p;
+ }
+
+
+
+
+
+ internal sealed class LEConverter : IConverter {
+ ///<summary></summary>
+ public short ToInt16(byte [] val, int idx)
+ {
+ return BitConverter.ToInt16(val, idx);
+ }
+ ///<summary></summary>
+ public ushort ToUInt16(byte [] val, int idx)
+ {
+ return BitConverter.ToUInt16(val, idx);
+ }
+ ///<summary></summary>
+ public int ToInt32(byte [] val, int idx)
+ {
+ return BitConverter.ToInt32(val, idx);
+ }
+ ///<summary></summary>
+ public uint ToUInt32(byte [] val, int idx)
+ {
+ return BitConverter.ToUInt32(val, idx);
+ }
+ ///<summary></summary>
+ public long ToInt64(byte [] val, int idx)
+ {
+ return BitConverter.ToInt64(val, idx);
+ }
+ ///<summary></summary>
+ public ulong ToUInt64(byte [] val, int idx)
+ {
+ return BitConverter.ToUInt64(val, idx);
+ }
+
+ }
+
+ internal sealed class BEConverter : IConverter {
+ ///<summary></summary>
+ public short ToInt16(byte [] val, int idx)
+ {
+ return SwapInt16(BitConverter.ToInt16(val, idx));
+ }
+ ///<summary></summary>
+ public ushort ToUInt16(byte [] val, int idx)
+ {
+ return SwapUInt16(BitConverter.ToUInt16(val, idx));
+ }
+ ///<summary></summary>
+ public int ToInt32(byte [] val, int idx)
+ {
+ return SwapInt32(BitConverter.ToInt32(val, idx));
+ }
+ ///<summary></summary>
+ public uint ToUInt32(byte [] val, int idx)
+ {
+ return SwapUInt32(BitConverter.ToUInt32(val, idx));
+ }
+ ///<summary></summary>
+ public long ToInt64(byte [] val, int idx)
+ {
+ return SwapInt64(BitConverter.ToInt64(val, idx));
+ }
+ ///<summary></summary>
+ public ulong ToUInt64(byte [] val, int idx)
+ {
+ return SwapUInt64(BitConverter.ToUInt64(val, idx));
+ }
+
+ }
+
+
+
+
+ ///<summary></summary>
+ public static short ToInt16(byte [] val, int idx)
+ {
+ return impl.ToInt16(val, idx);
+ }
+
+ ///<summary></summary>
+ public static ushort ToUInt16(byte [] val, int idx)
+ {
+ return impl.ToUInt16(val, idx);
+ }
+
+ ///<summary></summary>
+ public static int ToInt32(byte [] val, int idx)
+ {
+ return impl.ToInt32(val, idx);
+ }
+
+ ///<summary></summary>
+ public static uint ToUInt32(byte [] val, int idx)
+ {
+ return impl.ToUInt32(val, idx);
+ }
+
+ ///<summary></summary>
+ public static long ToInt64(byte [] val, int idx)
+ {
+ return impl.ToInt64(val, idx);
+ }
+
+ ///<summary></summary>
+ public static ulong ToUInt64(byte [] val, int idx)
+ {
+ return impl.ToUInt64(val, idx);
+ }
+
+
+
+ }
+
+}
+
View
278 Mono.PEToolkit/MachineId.cs
@@ -0,0 +1,278 @@
+
+//
+// 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.
+//
+/*
+ * Copyright (c) 2002 Sergey Chaban <serge@wildwestsoftware.com>
+ */
+
+namespace Mono.PEToolkit {
+
+ public enum MachineId : ushort {
+
+ /// <summary>
+ /// </summary>
+ /// <remarks>
+ /// IMAGE_FILE_MACHINE_UNKNOWN
+ /// </remarks>
+ UNKNOWN = 0,
+
+ /// <summary>
+ /// Intel 386.
+ /// </summary>
+ /// <remarks>
+ /// IMAGE_FILE_MACHINE_I386
+ /// </remarks>
+ I386 = 0x014c,
+
+ /// <summary>
+ /// Intel 486.
+ /// </summary>
+ /// <remarks>
+ /// </remarks>
+ I486 = 0x014d,
+
+ /// <summary>
+ /// Intel Pentium.
+ /// </summary>
+ /// <remarks>
+ /// </remarks>
+ PENTIUM = 0x014e,
+
+ /// <summary>
+ /// MIPS 3K big-endian
+ /// </summary>
+ /// <remarks>
+ /// IMAGE_FILE_MACHINE_R3000
+ /// </remarks>
+ R3000_BE = 0x0160,
+
+ /// <summary>
+ /// MIPS 3K little-endian, 0x160 big-endian
+ /// </summary>
+ /// <remarks>
+ /// IMAGE_FILE_MACHINE_R3000
+ /// </remarks>
+ R3000 = 0x0162,
+
+ /// <summary>
+ /// MIPS 4K little-endian
+ /// </summary>
+ /// <remarks>
+ /// IMAGE_FILE_MACHINE_R4000
+ /// </remarks>
+ R4000 = 0x0166,
+
+ /// <summary>
+ /// MIPS little-endian
+ /// </summary>
+ /// <remarks>
+ /// IMAGE_FILE_MACHINE_R10000
+ /// </remarks>
+ R10000 = 0x0168,
+
+ /// <summary>
+ /// MIPS little-endian WCE v2
+ /// </summary>
+ /// <remarks>
+ /// IMAGE_FILE_MACHINE_WCEMIPSV2
+ /// </remarks>
+ WCEMIPSV2 = 0x0169,
+
+ /// <summary>
+ /// Alpha_AXP
+ /// </summary>
+ /// <remarks>
+ /// IMAGE_FILE_MACHINE_ALPHA
+ /// </remarks>
+ ALPHA = 0x0184,
+
+ /// <summary>
+ /// SH3 little-endian
+ /// </summary>
+ /// <remarks>
+ /// IMAGE_FILE_MACHINE_SH3
+ /// </remarks>
+ SH3 = 0x01a2,
+
+ /// <summary>
+ /// </summary>
+ /// <remarks>
+ /// IMAGE_FILE_MACHINE_SH3DSP
+ /// </remarks>
+ SH3DSP = 0x01a3,
+
+ /// <summary>
+ /// SH3E little-endian
+ /// </summary>
+ /// <remarks>
+ /// IMAGE_FILE_MACHINE_SH3E
+ /// </remarks>
+ SH3E = 0x01a4,
+
+ /// <summary>
+ /// SH4 little-endian
+ /// </summary>
+ /// <remarks>
+ /// IMAGE_FILE_MACHINE_SH4
+ /// </remarks>
+ SH4 = 0x01a6,
+
+ /// <summary>
+ /// SH5
+ /// </summary>
+ /// <remarks>
+ /// IMAGE_FILE_MACHINE_SH5
+ /// </remarks>
+ SH5 = 0x01a8,
+
+ /// <summary>
+ /// ARM Little-Endian
+ /// </summary>
+ /// <remarks>
+ /// IMAGE_FILE_MACHINE_ARM
+ /// </remarks>
+ ARM = 0x01c0,
+
+ /// <summary>
+ /// ARM 10 Thumb family CPU.
+ /// </summary>
+ /// <remarks>
+ /// IMAGE_FILE_MACHINE_THUMB
+ /// http://www.arm.com/armtech/ARM10_Thumb?OpenDocument&ExpandSection=2
+ /// </remarks>
+ THUMB = 0x01c2,
+
+ /// <summary>
+ /// </summary>
+ /// <remarks>
+ /// IMAGE_FILE_MACHINE_AM33
+ /// </remarks>
+ AM33 = 0x01d3,
+
+ /// <summary>
+ /// IBM PowerPC Little-Endian
+ /// </summary>
+ /// <remarks>
+ /// IMAGE_FILE_MACHINE_POWERPC
+ /// </remarks>
+ POWERPC = 0x01F0,
+
+ /// <summary>
+ /// </summary>
+ /// <remarks>
+ /// IMAGE_FILE_MACHINE_POWERPCFP
+ /// </remarks>
+ POWERPCFP = 0x01f1,
+
+ /// <summary>
+ /// Intel 64
+ /// </summary>
+ /// <remarks>
+ /// IMAGE_FILE_MACHINE_IA64
+ /// </remarks>
+ IA64 = 0x0200,
+
+ /// <summary>
+ /// MIPS
+ /// </summary>
+ /// <remarks>
+ /// IMAGE_FILE_MACHINE_MIPS16
+ /// </remarks>
+ MIPS16 = 0x0266,
+
+ /// <summary>
+ /// ALPHA64
+ /// </summary>
+ /// <remarks>
+ /// IMAGE_FILE_MACHINE_ALPHA64
+ /// </remarks>
+ ALPHA64 = 0x0284,
+
+ /// <summary>
+ /// MIPS
+ /// </summary>
+ /// <remarks>
+ /// IMAGE_FILE_MACHINE_MIPSFPU
+ /// </remarks>
+ MIPSFPU = 0x0366,
+
+ /// <summary>
+ /// MIPS
+ /// </summary>
+ /// <remarks>
+ /// IMAGE_FILE_MACHINE_MIPSFPU16
+ /// </remarks>
+ MIPSFPU16 = 0x0466,
+
+ /// <summary>
+ /// </summary>
+ /// <remarks>
+ /// IMAGE_FILE_MACHINE_AXP64
+ /// </remarks>
+ AXP64 = ALPHA64,
+
+ /// <summary>
+ /// Infineon
+ /// </summary>
+ /// <remarks>
+ /// IMAGE_FILE_MACHINE_TRICORE
+ /// http://www.infineon.com/tricore
+ /// </remarks>
+ TRICORE = 0x0520,
+
+ /// <summary>
+ /// Common Executable Format (Windows CE).
+ /// </summary>
+ /// <remarks>
+ /// IMAGE_FILE_MACHINE_CEF
+ /// </remarks>
+ CEF = 0x0CEF,
+
+ /// <summary>
+ /// EFI Byte Code
+ /// </summary>
+ EBC = 0x0EBC,
+
+ /// <summary>
+ /// AMD64 (K8)
+ /// </summary>
+ /// <remarks>
+ /// IMAGE_FILE_MACHINE_AMD64
+ /// </remarks>
+ AMD64 = 0x8664,
+
+ /// <summary>
+ /// M32R little-endian
+ /// </summary>
+ /// <remarks>
+ /// IMAGE_FILE_MACHINE_M32R
+ /// </remarks>
+ M32R = 0x9104,
+
+ /// <summary>
+ /// </summary>
+ /// <remarks>
+ /// IMAGE_FILE_MACHINE_CEE
+ /// </remarks>
+ CEE = 0xC0EE,
+ }
+
+}
View
15 Mono.PEToolkit/Makefile
@@ -0,0 +1,15 @@
+thisdir = class/Mono.PEToolkit
+SUBDIRS = build
+include ../../build/rules.make
+
+LIBRARY = Mono.PEToolkit.dll
+LIB_MCS_FLAGS = /unsafe /r:$(corlib) /r:System.Xml.dll
+NO_TEST = yes
+
+EXTRA_DISTFILES = README
+
+mddump.exe:
+ @echo $@ now lives in build/. Email 'peter@newton.cx'
+ @echo if this change is a problem.
+
+include ../../build/library.make
View
41 Mono.PEToolkit/Mono.PEToolkit.dll.sources
@@ -0,0 +1,41 @@
+BadImageException.cs
+COFFHeader.cs
+Characteristics.cs
+CheckSum.cs
+CorHeader.cs
+DOSHeader.cs
+DataDir.cs
+ExeSignature.cs
+Image.cs
+LEBitConverter.cs
+MachineId.cs
+PEHeader.cs
+PEUtils.cs
+RVA.cs
+Section.cs
+SectionCharacteristics.cs
+Subsystem.cs
+metadata/AssemblyFlags.cs
+metadata/BadMetaDataException.cs
+metadata/CodedTokenId.cs
+metadata/ElementType.cs
+metadata/GUIDHeap.cs
+metadata/MDHeap.cs
+metadata/MDStream.cs
+metadata/MDTable.cs
+metadata/MDToken.cs
+metadata/MDUtils.cs
+metadata/ManifestResourceAttributes.cs
+metadata/MetaDataRoot.cs
+metadata/MethodIL.cs
+metadata/MethodSemanticsAttributes.cs
+metadata/PInvokeAttributes.cs
+metadata/Row.cs
+metadata/Rows.cs
+metadata/StringsHeap.cs
+metadata/TableId.cs
+metadata/Tables.cs
+metadata/TablesHeap.cs
+metadata/TablesHeapBase.cs
+metadata/TabsDecoder.cs
+metadata/TokenType.cs
View
698 Mono.PEToolkit/PEHeader.cs
@@ -0,0 +1,698 @@
+
+//
+// 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.
+//
+/*
+ * Copyright (c) 2002 Sergey Chaban <serge@wildwestsoftware.com>
+ */
+
+using System;
+using System.IO;
+using System.Runtime.InteropServices;
+
+namespace Mono.PEToolkit {
+
+ // IMAGE_OPTIONAL_HEADER
+ public class PEHeader {
+
+ /// <summary>
+ /// Standard PE/COFF fields.
+ /// </summary>
+ public class StdFields {
+ internal short magic; // always 0x10B?
+ internal byte lMajor;
+ internal byte lMinor;
+ internal uint codeSize;
+ internal uint initDataSize;
+ internal uint uninitDataSize;
+ internal RVA entryRVA;
+ internal RVA codeBase;
+ internal RVA dataBase;
+
+ public StdFields ()
+ {
+
+ }
+
+ public StdFields (BinaryReader reader)
+ {
+ Read (reader);
+ }
+
+ public void Read (BinaryReader reader)
+ {
+ magic = reader.ReadInt16 ();
+ lMajor = reader.ReadByte ();
+ lMinor = reader.ReadByte ();
+ codeSize = reader.ReadUInt32 ();
+ initDataSize = reader.ReadUInt32 ();
+ uninitDataSize = reader.ReadUInt32 ();
+ entryRVA = new RVA (reader.ReadUInt32 ());
+ codeBase = new RVA (reader.ReadUInt32 ());
+ dataBase = new RVA (reader.ReadUInt32 ());
+ }
+
+ public void Write (BinaryWriter writer)
+ {
+ writer.Write (magic);
+ writer.Write (lMajor);
+ writer.Write (lMinor);
+ writer.Write (codeSize);
+ writer.Write (initDataSize);
+ writer.Write (uninitDataSize);
+ entryRVA.Write (writer);
+ codeBase.Write (writer);
+ dataBase.Write (writer);
+ }
+
+ public string LinkerVersion {
+ get {
+ return String.Format("{0}.{1}", lMajor, lMinor);
+ }
+ }
+
+ public override string ToString() {
+ return String.Format(
+ "Magic : 0x{0}" + Environment.NewLine +
+ "Linker ver. : {1}" + Environment.NewLine +
+ "Size of code : {2}" + Environment.NewLine +
+ "Size of initialized data : {3}" + Environment.NewLine +
+ "Size of uinitialized data (BSS) : {4}" + Environment.NewLine,
+ magic.ToString("X"), LinkerVersion,
+ codeSize, initDataSize, uninitDataSize
+ );
+ }
+ }
+
+
+ /// <summary>
+ /// Windows-specific fields.
+ /// </summary>
+ /// <remarks>
+ /// See Partition II, 24.2.3.2
+ /// </remarks>
+ public class NTFields {
+ internal uint imgBase;
+ internal uint sectAlign;
+ internal uint fileAlign;
+ internal short osMaj;
+ internal short osMin;
+ internal short imgMaj;
+ internal short imgMin;
+ internal short subSysMaj;
+ internal short subSysMin;
+ internal int reserved_win32ver;
+ internal uint imgSize;
+ internal uint hdrSize;
+ internal uint chksum;
+ internal Subsystem subSys;
+ internal short dllFlags;
+ internal uint stackRes;
+ internal uint stackCommit;
+ internal uint heapRes;
+ internal uint heapCommit;
+ internal uint ldrFlags;
+ internal uint numDirs;
+
+ public NTFields ()
+ {
+
+ }
+
+ public NTFields (BinaryReader reader)
+ {
+ Read (reader);
+ }
+
+ public void Read (BinaryReader reader)
+ {
+ imgBase = reader.ReadUInt32 ();
+ sectAlign = reader.ReadUInt32 ();
+ fileAlign = reader.ReadUInt32 ();
+ osMaj = reader.ReadInt16 ();
+ osMin = reader.ReadInt16 ();
+ imgMaj = reader.ReadInt16 ();
+ imgMin = reader.ReadInt16 ();
+ subSysMaj = reader.ReadInt16 ();
+ subSysMin = reader.ReadInt16 ();
+ reserved_win32ver = reader.ReadInt32 ();
+ imgSize = reader.ReadUInt32 ();
+ hdrSize = reader.ReadUInt32 ();
+ chksum = reader.ReadUInt32 ();
+ subSys = (Subsystem) reader.ReadInt16 ();
+ dllFlags = reader.ReadInt16 ();
+ stackRes = reader.ReadUInt32 ();
+ stackCommit = reader.ReadUInt32 ();
+ heapRes = reader.ReadUInt32 ();
+ heapCommit = reader.ReadUInt32 ();
+ ldrFlags = reader.ReadUInt32 ();
+ numDirs = reader.ReadUInt32 ();
+ }
+
+ public void Write (BinaryWriter writer)
+ {
+ writer.Write (imgBase);
+ writer.Write (sectAlign);
+ writer.Write (fileAlign);
+ writer.Write (osMaj);
+ writer.Write (osMin);
+ writer.Write (imgMaj);
+ writer.Write (imgMin);
+ writer.Write (subSysMaj);
+ writer.Write (subSysMin);
+ writer.Write (reserved_win32ver);
+ writer.Write (imgSize);
+ writer.Write (hdrSize);
+ writer.Write (chksum);
+ writer.Write ((short)subSys);
+ writer.Write (dllFlags);
+ writer.Write (stackRes);
+ writer.Write (stackCommit);
+ writer.Write (heapRes);
+ writer.Write (heapCommit);
+ writer.Write (ldrFlags);
+ writer.Write (numDirs);
+ }
+
+ public string OSVersion {
+ get {
+ return String.Format("{0}.{1}", osMaj, osMin);
+ }
+ }
+
+ public string ImageVersion {
+ get {
+ return String.Format("{0}.{1}", imgMaj, imgMin);
+ }
+ }
+
+ public string SubsysVersion {
+ get {
+ return String.Format("{0}.{1}", subSysMaj, subSysMin);
+ }
+ }
+
+
+ /// <summary>
+ /// </summary>
+ /// <returns></returns>
+ public override string ToString() {
+ return String.Format(
+ "Image Base : 0x{0}" + Environment.NewLine +
+ "Section Alignment : 0x{1}" + Environment.NewLine +
+ "File Alignment : 0x{2}" + Environment.NewLine +
+ "OS Version : {3}" + Environment.NewLine +
+ "Image Version : {4}" + Environment.NewLine +
+ "Subsystem Version : {5}" + Environment.NewLine +
+ "Reserved/Win32Ver : {6}" + Environment.NewLine +
+ "Image Size : {7}" + Environment.NewLine +
+ "Header Size : {8}" + Environment.NewLine +
+ "Checksum : 0x{9}" + Environment.NewLine +
+ "Subsystem : {10}" + Environment.NewLine +
+ "DLL Flags : {11}" + Environment.NewLine +
+ "Stack Reserve Size : 0x{12}" + Environment.NewLine +
+ "Stack Commit Size : 0x{13}" + Environment.NewLine +
+ "Heap Reserve Size : 0x{14}" + Environment.NewLine +
+ "Heap Commit Size : 0x{15}" + Environment.NewLine +
+ "Loader Flags : {16}" + Environment.NewLine +
+ "Number of Directories : {17}" + Environment.NewLine,
+ imgBase.ToString("X"), sectAlign.ToString("X"), fileAlign.ToString("X"),
+ OSVersion, ImageVersion, SubsysVersion,
+ reserved_win32ver,
+ imgSize, hdrSize, chksum.ToString("X"), subSys, dllFlags,
+ stackRes.ToString("X"), stackCommit.ToString("X"), heapRes.ToString("X"), heapCommit.ToString ("X"),
+ ldrFlags, numDirs
+ );
+ }
+ }
+
+
+ internal StdFields stdFlds;
+ internal NTFields ntFlds;
+
+ internal DataDir exportDir;
+ internal DataDir importDir;
+ internal DataDir resourceDir;
+ internal DataDir exceptionDir;
+ internal DataDir securityDir;
+ internal DataDir baseRelocDir;
+ internal DataDir debugDir;
+ internal DataDir copyrightDir;
+ internal DataDir GPDir;
+ internal DataDir TLSDir;
+ internal DataDir loadCfgDir;
+ internal DataDir boundImpDir;
+ internal DataDir IATDir;
+ internal DataDir delayImpDir;
+ internal DataDir CLIHdrDir;
+ internal DataDir reservedDir;
+
+
+ public bool IsCLIImage {
+ get {
+ return (CLIHdrDir.virtAddr.Value != 0);
+ }
+ }
+
+
+ //
+ // Accessors for standard COFF fields.
+ //
+
+ public short Magic {
+ get {
+ return stdFlds.magic;
+ }
+ set {
+ stdFlds.magic = value;
+ }
+ }
+
+ public byte MajorLinkerVersion {
+ get {
+ return stdFlds.lMajor;
+ }
+ set {
+ stdFlds.lMajor = value;
+ }
+ }
+
+ public byte MinorLinkerVersion {