Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

Move dead code

svn path=/old-code/; revision=133369
  • Loading branch information...
commit ee070414c08221450d9f902b8e191a761d1a0905 1 parent f79abb9
Miguel de Icaza migueldeicaza authored

Showing 65 changed files with 17,069 additions and 0 deletions. Show diff stats Hide diff stats

  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
41 Mono.PEToolkit/BadImageException.cs
... ... @@ -0,0 +1,41 @@
  1 +
  2 +//
  3 +// Permission is hereby granted, free of charge, to any person obtaining
  4 +// a copy of this software and associated documentation files (the
  5 +// "Software"), to deal in the Software without restriction, including
  6 +// without limitation the rights to use, copy, modify, merge, publish,
  7 +// distribute, sublicense, and/or sell copies of the Software, and to
  8 +// permit persons to whom the Software is furnished to do so, subject to
  9 +// the following conditions:
  10 +//
  11 +// The above copyright notice and this permission notice shall be
  12 +// included in all copies or substantial portions of the Software.
  13 +//
  14 +// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  15 +// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  16 +// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  17 +// NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
  18 +// LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
  19 +// OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
  20 +// WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  21 +//
  22 +/*
  23 + * Copyright (c) 2002 Sergey Chaban <serge@wildwestsoftware.com>
  24 + */
  25 +
  26 +using System;
  27 +
  28 +namespace Mono.PEToolkit {
  29 +
  30 + public class BadImageException : Exception {
  31 +
  32 + public BadImageException() : base()
  33 + {
  34 + }
  35 +
  36 + public BadImageException(string msg) : base(msg)
  37 + {
  38 + }
  39 + }
  40 +
  41 +}
127 Mono.PEToolkit/COFFHeader.cs
... ... @@ -0,0 +1,127 @@
  1 +
  2 +//
  3 +// Permission is hereby granted, free of charge, to any person obtaining
  4 +// a copy of this software and associated documentation files (the
  5 +// "Software"), to deal in the Software without restriction, including
  6 +// without limitation the rights to use, copy, modify, merge, publish,
  7 +// distribute, sublicense, and/or sell copies of the Software, and to
  8 +// permit persons to whom the Software is furnished to do so, subject to
  9 +// the following conditions:
  10 +//
  11 +// The above copyright notice and this permission notice shall be
  12 +// included in all copies or substantial portions of the Software.
  13 +//
  14 +// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  15 +// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  16 +// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  17 +// NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
  18 +// LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
  19 +// OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
  20 +// WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  21 +//
  22 +/*
  23 + * Copyright (c) 2002 Sergey Chaban <serge@wildwestsoftware.com>
  24 + */
  25 +
  26 +using System;
  27 +using System.IO;
  28 +using System.Runtime.InteropServices;
  29 +
  30 +namespace Mono.PEToolkit {
  31 +
  32 + public class COFFHeader {
  33 +
  34 + private MachineId machine;
  35 + private short sections;
  36 + private uint tdStampRaw;
  37 + private uint symTabPtr;
  38 + private uint numSymbols;
  39 + private short optHeaderSize;
  40 + private Characteristics characteristics;
  41 +
  42 + public MachineId Machine {
  43 + get { return machine; }
  44 + set { machine = value; }
  45 + }
  46 +
  47 + public short NumberOfSections {
  48 + get { return sections; }
  49 + set { sections = value; }
  50 + }
  51 +
  52 + public uint TimeDateStamp {
  53 + get { return tdStampRaw; }
  54 + set { tdStampRaw = value; }
  55 + }
  56 +
  57 + public DateTime TimeStamp {
  58 + get {
  59 + return (new DateTime(1970, 1, 1) +
  60 + TimeSpan.FromSeconds(tdStampRaw)).ToLocalTime();
  61 + }
  62 + }
  63 +
  64 + public uint PointerToSymbolTable {
  65 + get { return symTabPtr; }
  66 + set { symTabPtr = value; }
  67 + }
  68 +
  69 + public uint NumberOfSymbols {
  70 + get { return numSymbols; }
  71 + set { numSymbols = value; }
  72 + }
  73 +
  74 + public short SizeOfOptionalHeader {
  75 + get { return optHeaderSize; }
  76 + set { optHeaderSize = value; }
  77 + }
  78 +
  79 + public Characteristics Characteristics {
  80 + get { return characteristics; }
  81 + set { characteristics = value; }
  82 + }
  83 +
  84 + public void Read (BinaryReader reader)
  85 + {
  86 + machine = (MachineId) reader.ReadUInt16 ();
  87 + sections = reader.ReadInt16 ();
  88 + tdStampRaw = reader.ReadUInt32 ();
  89 + symTabPtr = reader.ReadUInt32 ();
  90 + numSymbols = reader.ReadUInt32 ();
  91 + optHeaderSize = reader.ReadInt16 ();
  92 + characteristics = (Characteristics) reader.ReadUInt16 ();
  93 + }
  94 +
  95 + public void Write (BinaryWriter writer)
  96 + {
  97 + writer.Write ((ushort)machine);
  98 + writer.Write (sections);
  99 + writer.Write (tdStampRaw);
  100 + writer.Write (symTabPtr);
  101 + writer.Write (numSymbols);
  102 + writer.Write (optHeaderSize);
  103 + writer.Write ((ushort)characteristics);
  104 + }
  105 +
  106 + public void Dump(TextWriter writer)
  107 + {
  108 + writer.WriteLine(
  109 + "Machine ID : {0}" + Environment.NewLine +
  110 + "Sections : {1}" + Environment.NewLine +
  111 + "Characteristics : {2}" + Environment.NewLine +
  112 + "timestamp : {3} ({4})" + Environment.NewLine
  113 + ,machine, sections, (ushort)characteristics,
  114 + TimeStamp, tdStampRaw.ToString("X")
  115 + );
  116 + }
  117 +
  118 + public override string ToString()
  119 + {
  120 + StringWriter sw = new StringWriter();
  121 + Dump(sw);
  122 + return sw.ToString();
  123 + }
  124 +
  125 + }
  126 +}
  127 +
49 Mono.PEToolkit/ChangeLog
... ... @@ -0,0 +1,49 @@
  1 +2003-03-01 Jackson Harper <jackson@latitudegeo.com>
  2 +
  3 + * Image.cs: Write Metadata root
  4 +
  5 +2003-02-23 Peter Williams <peterw@ximian.com>
  6 +
  7 + * makefile.gnu (all): Give this dependencies
  8 + so that we don't rebuild every time.
  9 + (all): Take two.
  10 +
  11 +2003-02-22 Jackson Harper <jackson@latitudegeo.com>
  12 +
  13 + * Image.cs: Write CorHeader
  14 +
  15 +2003-02-22 Jackson Harper <jackson@latitudegeo.com>
  16 +
  17 + * Image.cs: Write Sections
  18 +
  19 +2003-02-22 Jackson Harper <jackson@latitudegeo.com>
  20 +
  21 + * Section.cs: Add method for writing Sections
  22 +
  23 +2003-02-21 Jackson Harper <jackson@latitudegeo.com>
  24 +
  25 + * Section.cs: Add Method to write Headers
  26 +
  27 +2003-02-21 Jackson Harper <jackson@latitudegeo.com>
  28 +
  29 + * Section.cs: Make Read method safe
  30 +
  31 +2003-02-21 Jackson Harper <jackson@latitudegeo.com>
  32 +
  33 + * Section.cs: Convert Header to a class and lowers it accesability.
  34 +
  35 +2003-02-17 Jackson Harper <jackson@latitudegeo.com>
  36 +
  37 + * COFFHeader.cs: Convert to class, use Read and Write methods. Compress some code
  38 + * CheckSum.cs: Access Lfanew through property not field.
  39 + * CorHeader.cs: Convert to class, use Read and Write methods.
  40 + * DOSHeader.cs: Convert to class, use Read and Write methods, and Init method for creating
  41 + new DOSHeaders
  42 + * DataDir.cs: Convert to class, use Read and Write methods.
  43 + * Image.cs: Access lfanew through property, add Write method.
  44 + * PEHeader.cs: Convert to class, use Read and Write methods.
  45 + * PEUtils.cs: Add GetString method for converting byte pointers to strings.
  46 + * RVA.cs: Add Write method
  47 + * Section.cs: Use PEUtils.GetString method instead of new string (byte*)
  48 + * makefuile.gnu: Compile all the files I want compiled ;-)
  49 +
186 Mono.PEToolkit/Characteristics.cs
... ... @@ -0,0 +1,186 @@
  1 +
  2 +//
  3 +// Permission is hereby granted, free of charge, to any person obtaining
  4 +// a copy of this software and associated documentation files (the
  5 +// "Software"), to deal in the Software without restriction, including
  6 +// without limitation the rights to use, copy, modify, merge, publish,
  7 +// distribute, sublicense, and/or sell copies of the Software, and to
  8 +// permit persons to whom the Software is furnished to do so, subject to
  9 +// the following conditions:
  10 +//
  11 +// The above copyright notice and this permission notice shall be
  12 +// included in all copies or substantial portions of the Software.
  13 +//
  14 +// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  15 +// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  16 +// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  17 +// NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
  18 +// LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
  19 +// OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
  20 +// WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  21 +//
  22 +/*
  23 + * Copyright (c) 2002 Sergey Chaban <serge@wildwestsoftware.com>
  24 + */
  25 +
  26 +using System;
  27 +
  28 +namespace Mono.PEToolkit {
  29 +
  30 + [Flags]
  31 + public enum Characteristics : ushort {
  32 +
  33 +
  34 + /// <summary>
  35 + /// Relocation info stripped from file.
  36 + /// </summary>
  37 + /// <remarks>
  38 + /// IMAGE_FILE_RELOCS_STRIPPED
  39 + /// </remarks>
  40 + RELOCS_STRIPPED = 0x0001,
  41 +
  42 +
  43 +
  44 + /// <summary>
  45 + /// File is executable
  46 + /// (i.e. file is neither object file nor library file,
  47 + /// so there are no unresolved externel references).
  48 + /// </summary>
  49 + /// <remarks>
  50 + /// IMAGE_FILE_EXECUTABLE_IMAGE
  51 + /// </remarks>
  52 + EXECUTABLE_IMAGE = 0x0002,
  53 +
  54 +
  55 + /// <summary>
  56 + /// Line nunbers stripped from file.
  57 + /// </summary>
  58 + /// <remarks>
  59 + /// IMAGE_FILE_LINE_NUMS_STRIPPED
  60 + /// </remarks>
  61 + LINE_NUMS_STRIPPED = 0x0004,
  62 +
  63 +
  64 + /// <summary>
  65 + /// Local symbols stripped from file.
  66 + /// </summary>
  67 + /// <remarks>
  68 + /// IMAGE_FILE_LOCAL_SYMS_STRIPPED
  69 + /// </remarks>
  70 + LOCAL_SYMS_STRIPPED = 0x0008,
  71 +
  72 +
  73 + /// <summary>
  74 + /// Agressively trim working set
  75 + /// </summary>
  76 + /// <remarks>
  77 + /// IMAGE_FILE_AGGRESIVE_WS_TRIM
  78 + /// </remarks>
  79 + AGGRESIVE_WS_TRIM = 0x0010,
  80 +
  81 +
  82 + /// <summary>
  83 + /// App can handle >2gb addresses
  84 + /// </summary>
  85 + /// <remarks>
  86 + /// IMAGE_FILE_LARGE_ADDRESS_AWARE
  87 + /// </remarks>
  88 + LARGE_ADDRESS_AWARE = 0x0020,
  89 +
  90 +
  91 + /// <summary>
  92 + /// Bytes of machine word are reversed.
  93 + /// </summary>
  94 + /// <remarks>
  95 + /// IMAGE_FILE_BYTES_REVERSED_LO
  96 + /// </remarks>
  97 + BYTES_REVERSED_LO = 0x0080,
  98 +
  99 +
  100 + /// <summary>
  101 + /// 32 bit word machine.
  102 + /// </summary>
  103 + /// <remarks>
  104 + /// IMAGE_FILE_32BIT_MACHINE
  105 + /// </remarks>
  106 + MACHINE_32BIT = 0x0100,
  107 +
  108 +
  109 + /// <summary>
  110 + /// Debugging info stripped from file in .DBG file
  111 + /// </summary>
  112 + /// <remarks>
  113 + /// IMAGE_FILE_DEBUG_STRIPPED
  114 + /// </remarks>
  115 + DEBUG_STRIPPED = 0x0200,
  116 +
  117 +
  118 + /// <summary>
  119 + /// If Image is on removable media, copy and run from the swap file.
  120 + /// </summary>
  121 + /// <remarks>
  122 + /// IMAGE_FILE_REMOVABLE_RUN_FROM_SWAP
  123 + /// </remarks>
  124 + REMOVABLE_RUN_FROM_SWAP = 0x0400,
  125 +
  126 +
  127 + /// <summary>
  128 + /// If Image is on Net, copy and run from the swap file.
  129 + /// </summary>
  130 + /// <remarks>
  131 + /// IMAGE_FILE_NET_RUN_FROM_SWAP
  132 + /// </remarks>
  133 + NET_RUN_FROM_SWAP = 0x0800,
  134 +
  135 +
  136 + /// <summary>
  137 + /// This flag is used to indicate that the file
  138 + /// is a system sile, such as device driver.
  139 + /// </summary>
  140 + /// <remarks>
  141 + /// IMAGE_FILE_SYSTEM
  142 + /// </remarks>
  143 + SYSTEM = 0x1000,
  144 +
  145 +
  146 + /// <summary>
  147 + /// This flag indicates that the file
  148 + /// is a dynamic library (DLL).
  149 + /// </summary>
  150 + /// <remarks>
  151 + /// IMAGE_FILE_DLL
  152 + /// </remarks>
  153 + DLL = 0x2000,
  154 +
  155 +
  156 + /// <summary>
  157 + /// File should only be run on a uni-processor (UP) machine.
  158 + /// </summary>
  159 + /// <remarks>
  160 + /// IMAGE_FILE_UP_SYSTEM_ONLY
  161 + /// </remarks>
  162 + UP_SYSTEM_ONLY = 0x4000,
  163 +
  164 +
  165 + /// <summary>
  166 + /// Bytes of machine word are reversed.
  167 + /// </summary>
  168 + /// <remarks>
  169 + /// IMAGE_FILE_BYTES_REVERSED_HI
  170 + /// </remarks>
  171 + BYTES_REVERSED_HI = 0x8000,
  172 +
  173 +
  174 +
  175 + /// <summary>
  176 + /// Default flags that must be set in CIL-only image.
  177 + /// </summary>
  178 + /// <remarks>
  179 + /// See Partition II, 24.2.2.1
  180 + /// </remarks>
  181 + CIL_DEFAULT = LINE_NUMS_STRIPPED |
  182 + LOCAL_SYMS_STRIPPED |
  183 + DEBUG_STRIPPED
  184 + }
  185 +
  186 +}
117 Mono.PEToolkit/CheckSum.cs
... ... @@ -0,0 +1,117 @@
  1 +
  2 +//
  3 +// Permission is hereby granted, free of charge, to any person obtaining
  4 +// a copy of this software and associated documentation files (the
  5 +// "Software"), to deal in the Software without restriction, including
  6 +// without limitation the rights to use, copy, modify, merge, publish,
  7 +// distribute, sublicense, and/or sell copies of the Software, and to
  8 +// permit persons to whom the Software is furnished to do so, subject to
  9 +// the following conditions:
  10 +//
  11 +// The above copyright notice and this permission notice shall be
  12 +// included in all copies or substantial portions of the Software.
  13 +//
  14 +// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  15 +// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  16 +// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  17 +// NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
  18 +// LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
  19 +// OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
  20 +// WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  21 +//
  22 +/*
  23 + * Copyright (c) 2002 Sergey Chaban <serge@wildwestsoftware.com>
  24 + */
  25 +
  26 +// see http://cvs.winehq.com/cvsweb/wine/dlls/imagehlp/modify.c
  27 +// starting from Revision 1.8
  28 +
  29 +using System;
  30 +using System.IO;
  31 +
  32 +namespace Mono.PEToolkit {
  33 +
  34 + public sealed class CheckSum {
  35 +
  36 + private CheckSum()
  37 + {
  38 + // Never instantiated.
  39 + }
  40 +
  41 +
  42 + public static uint Calc(string peFile)
  43 + {
  44 + uint res = 0;
  45 +
  46 + FileInfo pe = new FileInfo(peFile);
  47 + if (!pe.Exists) {
  48 + throw new Exception("CheckSum : Invalid file path.");
  49 + }
  50 +
  51 + using (BinaryReader reader = new BinaryReader(pe.OpenRead())) {
  52 + if (!reader.BaseStream.CanSeek) {
  53 + throw new Exception("Can't seek.");
  54 + }
  55 +
  56 + DOSHeader dosHdr = new DOSHeader();
  57 + COFFHeader coffHdr = new COFFHeader();
  58 + PEHeader peHdr = new PEHeader();
  59 +
  60 + dosHdr.Read (reader);
  61 + reader.BaseStream.Position = dosHdr.Lfanew;
  62 + ExeSignature peSig = (ExeSignature) reader.ReadUInt16();
  63 + if (peSig != ExeSignature.NT) {
  64 + throw new BadImageException("Checksum : Invalid image format, cannot find PE signature.");
  65 + }
  66 +
  67 + peSig = (ExeSignature) reader.ReadUInt16();
  68 + if (peSig != ExeSignature.NT2) {
  69 + throw new BadImageException("Checksum : Invalid image format, cannot find PE signature.");
  70 + }
  71 +
  72 + coffHdr.Read(reader);
  73 + peHdr.Read(reader);
  74 +
  75 + uint oldSum = peHdr.CheckSum;
  76 + reader.BaseStream.Position = 0;
  77 + long len = pe.Length;
  78 + long whole = len >> 1;
  79 + uint sum = 0;
  80 + uint hi, lo;
  81 + for (long i = whole; --i >= 0;) {
  82 + sum += reader.ReadUInt16();
  83 + hi = sum >> 16;
  84 + if (hi != 0) {
  85 + sum = hi + (sum & 0xFFFF);
  86 + }
  87 + }
  88 + if ((len & 1L) != 0) {
  89 + sum += (uint) reader.ReadByte();
  90 + hi = sum >> 16;
  91 + if (hi != 0) {
  92 + sum = hi + (sum & 0xFFFF);
  93 + }
  94 + }
  95 +
  96 + // fix low word of checksum
  97 + lo = oldSum & 0xFFFF;
  98 + if ((sum & 0xFFFF) >= lo) {
  99 + sum -= lo;
  100 + } else {
  101 + sum = (((sum & 0xFFFF) - lo) & 0xFFFF) - 1;
  102 + }
  103 +
  104 + // fix high word of checksum
  105 + hi = oldSum >> 16;
  106 + if ((sum & 0xFFFF) >= hi) {
  107 + sum -= hi;
  108 + } else {
  109 + sum = (((sum & 0xFFFF) - hi) & 0xFFFF) - 1;
  110 + }
  111 + }
  112 +
  113 + return res;
  114 + }
  115 + }
  116 +
  117 +}
409 Mono.PEToolkit/CorHeader.cs
... ... @@ -0,0 +1,409 @@
  1 +
  2 +//
  3 +// Permission is hereby granted, free of charge, to any person obtaining
  4 +// a copy of this software and associated documentation files (the
  5 +// "Software"), to deal in the Software without restriction, including
  6 +// without limitation the rights to use, copy, modify, merge, publish,
  7 +// distribute, sublicense, and/or sell copies of the Software, and to
  8 +// permit persons to whom the Software is furnished to do so, subject to
  9 +// the following conditions:
  10 +//
  11 +// The above copyright notice and this permission notice shall be
  12 +// included in all copies or substantial portions of the Software.
  13 +//
  14 +// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  15 +// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  16 +// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  17 +// NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
  18 +// LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
  19 +// OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
  20 +// WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  21 +//
  22 +/*
  23 + * Copyright (c) 2002 Sergey Chaban <serge@wildwestsoftware.com>
  24 + */
  25 +
  26 +using System;
  27 +using System.IO;
  28 +using System.Runtime.InteropServices;
  29 +
  30 +using Mono.PEToolkit.Metadata;
  31 +
  32 +namespace Mono.PEToolkit {
  33 +
  34 + [Flags]
  35 + public enum CorFlags : uint {
  36 + /// <summary>
  37 + /// COMIMAGE_FLAGS_ILONLY
  38 + /// </summary>
  39 + ILONLY = 0x00000001,
  40 +
  41 + /// <summary>
  42 + /// COMIMAGE_FLAGS_32BITREQUIRED
  43 + /// </summary>
  44 + REQUIRED_32BIT = 0x00000002,
  45 +
  46 + /// <summary>
  47 + /// COMIMAGE_FLAGS_IL_LIBRARY
  48 + /// </summary>
  49 + IL_LIBRARY = 0x00000004,
  50 +
  51 + /// <summary>
  52 + /// COMIMAGE_FLAGS_TRACKDEBUGDATA
  53 + /// </summary>
  54 + TRACKDEBUGDATA = 0x00010000,
  55 + }
  56 +
  57 +
  58 + /// <summary>
  59 + /// CLR 2.0 header structure.
  60 + /// IMAGE_COR20_HEADER
  61 + /// </summary>
  62 + public class CorHeader {
  63 + // Header versioning
  64 + internal uint cb;
  65 + internal short runtimeMaj;
  66 + internal short runtimeMin;
  67 +
  68 + // Symbol table and startup information.
  69 + internal DataDir meta;
  70 + internal CorFlags flags;
  71 + internal uint entryTok;
  72 +
  73 + // Binding information.
  74 + internal DataDir rsrc;
  75 + internal DataDir strongSig;
  76 +
  77 + // Regular fixup and binding information.
  78 + internal DataDir codeManTab;
  79 + internal DataDir vtab;
  80 + internal DataDir jumps;
  81 +
  82 + // Managed Native Code.
  83 + internal DataDir eeInfo;
  84 + internal DataDir helper;
  85 + internal DataDir dynInfo;
  86 + internal DataDir delayInfo;
  87 + internal DataDir modImg;
  88 + internal DataDir extFixups;
  89 + internal DataDir ridMap;
  90 + internal DataDir dbgMap;
  91 +
  92 + // obsolete?
  93 + internal DataDir ipMap;
  94 +
  95 +
  96 + //
  97 + // Accessors
  98 + //
  99 +
  100 + public uint Size {
  101 + get {
  102 + return cb;
  103 + }
  104 + set {
  105 + cb = value;
  106 + }
  107 + }
  108 +
  109 + public short MajorRuntimeVersion {
  110 + get {
  111 + return runtimeMaj;
  112 + }
  113 + set {
  114 + runtimeMaj = value;
  115 + }
  116 + }
  117 +
  118 + public short MinorRuntimeVersion {
  119 + get {
  120 + return runtimeMin;
  121 + }
  122 + set {
  123 + runtimeMin = value;
  124 + }
  125 + }
  126 +
  127 + public string RuntimeVersion {
  128 + get {
  129 + return String.Format("{0}.{1}", runtimeMaj, runtimeMin);
  130 + }
  131 + }
  132 +
  133 + public DataDir MetaData {
  134 + get {
  135 + return meta;
  136 + }
  137 + set {
  138 + meta = value;
  139 + }
  140 + }
  141 +
  142 + public CorFlags Flags {
  143 + get {
  144 + return flags;
  145 + }
  146 + set {
  147 + flags = value;
  148 + }
  149 + }
  150 +
  151 + public MDToken EntryPointToken {
  152 + get {
  153 + return entryTok;
  154 + }
  155 + set {
  156 + entryTok = value;
  157 + }
  158 + }
  159 +
  160 + public DataDir Resources {
  161 + get {
  162 + return rsrc;
  163 + }
  164 + set {
  165 + rsrc = value;
  166 + }
  167 + }
  168 +
  169 + public DataDir StrongNameSignature {
  170 + get {
  171 + return strongSig;
  172 + }
  173 + set {
  174 + strongSig = value;
  175 + }
  176 + }
  177 +
  178 + public DataDir CodeManagerTable {
  179 + get {
  180 + return codeManTab;
  181 + }
  182 + set {
  183 + codeManTab = value;
  184 + }
  185 + }
  186 +
  187 + public DataDir VTableFixups {
  188 + get {
  189 + return vtab;
  190 + }
  191 + set {
  192 + vtab = value;
  193 + }
  194 + }
  195 +
  196 + public DataDir ExportAddressTableJumps {
  197 + get {
  198 + return jumps;
  199 + }
  200 + set {
  201 + jumps = value;
  202 + }
  203 + }
  204 +
  205 +
  206 + public DataDir EEInfoTable {
  207 + get {
  208 + return eeInfo;
  209 + }
  210 + set {
  211 + eeInfo = value;
  212 + }
  213 + }
  214 +
  215 + public DataDir HelperTable {
  216 + get {
  217 + return helper;
  218 + }
  219 + set {
  220 + helper = value;
  221 + }
  222 + }
  223 +
  224 + public DataDir DynamicInfo {
  225 + get {
  226 + return dynInfo;
  227 + }
  228 + set {
  229 + dynInfo = value;
  230 + }
  231 + }
  232 +
  233 + public DataDir DelayLoadInfo {
  234 + get {
  235 + return delayInfo;
  236 + }
  237 + set {
  238 + delayInfo = value;
  239 + }
  240 + }
  241 +
  242 + public DataDir ModuleImage {
  243 + get {
  244 + return modImg;
  245 + }
  246 + set {
  247 + modImg = value;
  248 + }
  249 + }
  250 +
  251 + public DataDir ExternalFixups {
  252 + get {
  253 + return extFixups;
  254 + }
  255 + set {
  256 + extFixups = value;
  257 + }
  258 + }
  259 +
  260 + public DataDir RidMap {
  261 + get {
  262 + return ridMap;
  263 + }
  264 + set {
  265 + ridMap = value;
  266 + }
  267 + }
  268 +
  269 + public DataDir DebugMap {
  270 + get {
  271 + return dbgMap;
  272 + }
  273 + set {
  274 + dbgMap = value;
  275 + }
  276 + }
  277 +
  278 +
  279 + public DataDir IPMap {
  280 + get {
  281 + return ipMap;
  282 + }
  283 + set {
  284 + ipMap = value;
  285 + }
  286 + }
  287 +
  288 +
  289 + public void Read(BinaryReader reader)
  290 + {
  291 + // Header versioning
  292 + cb = reader.ReadUInt32 ();
  293 +
  294 + runtimeMaj = reader.ReadInt16 ();
  295 + runtimeMin = reader.ReadInt16 ();
  296 +
  297 +
  298 + // Symbol table and startup information.
  299 + meta = new DataDir (reader);
  300 + flags = (CorFlags) reader.ReadUInt32 ();
  301 + entryTok = reader.ReadUInt32 ();
  302 +
  303 + // Binding information.
  304 + rsrc = new DataDir (reader);
  305 + strongSig = new DataDir (reader);
  306 +
  307 + // Regular fixup and binding information.
  308 + codeManTab = new DataDir (reader);
  309 + vtab = new DataDir (reader);
  310 + jumps = new DataDir (reader);
  311 +
  312 + // Managed Native Code.
  313 + eeInfo = new DataDir (reader);
  314 + helper = new DataDir (reader);
  315 + dynInfo = new DataDir (reader);
  316 + delayInfo = new DataDir (reader);
  317 + modImg = new DataDir (reader);
  318 + extFixups = new DataDir (reader);
  319 + ridMap = new DataDir (reader);
  320 + dbgMap = new DataDir (reader);
  321 +
  322 + // obsolete?
  323 + ipMap = new DataDir (reader);
  324 + }
  325 +
  326 + public void Write (BinaryWriter writer)
  327 + {
  328 + // Header versioning
  329 + writer.Write (cb);
  330 +
  331 + writer.Write (runtimeMaj);
  332 + writer.Write (runtimeMin);
  333 +
  334 + // Symbol table and startup information.
  335 + meta.Write (writer);
  336 + writer.Write ((uint)flags);
  337 + writer.Write (entryTok);
  338 +
  339 + // Binding information.
  340 + rsrc.Write (writer);
  341 + strongSig.Write (writer);
  342 +
  343 + // Regular fixup and binding information.
  344 + codeManTab.Write (writer);
  345 + vtab.Write (writer);
  346 + jumps.Write (writer);
  347 +
  348 + // Managed Native Code.
  349 + eeInfo.Write (writer);
  350 + helper.Write (writer);
  351 + dynInfo.Write (writer);
  352 + delayInfo.Write (writer);
  353 + modImg.Write (writer);
  354 + extFixups.Write (writer);
  355 + ridMap.Write (writer);
  356 + dbgMap.Write (writer);
  357 +
  358 + // obsolete?
  359 + ipMap.Write (writer);
  360 + }
  361 +
  362 +
  363 + public void Dump(TextWriter writer)
  364 + {
  365 + writer.WriteLine(String.Format (
  366 + "Header Size : {0}" + Environment.NewLine +
  367 + "Runtime Version : {1}" + Environment.NewLine +
  368 + "MetaData Root : {2}" + Environment.NewLine +
  369 + "Flags : {3}" + Environment.NewLine +
  370 + "Entry Point Token : {4}" + Environment.NewLine +
  371 + "Resources : {5}" + Environment.NewLine +
  372 + "Strong Name Signature : {6}" + Environment.NewLine +
  373 + "Code Manager Table : {7}" + Environment.NewLine +
  374 + "VTable Fixups : {8}" + Environment.NewLine +
  375 + "Export Address Table Jumps : {9}" + Environment.NewLine +
  376 + "EE Info Table : {10}" + Environment.NewLine +
  377 + "Helper Table : {11}" + Environment.NewLine +
  378 + "Dynamic Info : {12}" + Environment.NewLine +
  379 + "Delay Load Info : {13}" + Environment.NewLine +
  380 + "Module Image : {14}" + Environment.NewLine +
  381 + "External Fixups : {15}" + Environment.NewLine +
  382 + "Rid Map : {16}" + Environment.NewLine +
  383 + "Debug Map : {17}" + Environment.NewLine +
  384 + "IP Map : {18}" + Environment.NewLine +
  385 + "Runtime Major : {19}" + Environment.NewLine +
  386 + "Runtime Minor : {20}" + Environment.NewLine,
  387 + cb,
  388 + RuntimeVersion,
  389 + meta, null, EntryPointToken,
  390 + rsrc, strongSig,
  391 + codeManTab, vtab, jumps,
  392 + eeInfo, helper, dynInfo, delayInfo, modImg, extFixups,
  393 + ridMap, dbgMap, ipMap, MajorRuntimeVersion, MinorRuntimeVersion
  394 + ));
  395 +
  396 + }
  397 +
  398 + /// <summary>
  399 + /// </summary>
  400 + /// <returns></returns>
  401 + public override string ToString() {
  402 + StringWriter sw = new StringWriter();
  403 + Dump(sw);
  404 + return sw.ToString();
  405 + }
  406 +
  407 +
  408 + }
  409 +}
1  Mono.PEToolkit/DEPRECATED
... ... @@ -0,0 +1 @@
  1 +This module has not been maintained
117 Mono.PEToolkit/DOSHeader.cs
... ... @@ -0,0 +1,117 @@
  1 +
  2 +//
  3 +// Permission is hereby granted, free of charge, to any person obtaining
  4 +// a copy of this software and associated documentation files (the
  5 +// "Software"), to deal in the Software without restriction, including
  6 +// without limitation the rights to use, copy, modify, merge, publish,
  7 +// distribute, sublicense, and/or sell copies of the Software, and to
  8 +// permit persons to whom the Software is furnished to do so, subject to
  9 +// the following conditions:
  10 +//
  11 +// The above copyright notice and this permission notice shall be
  12 +// included in all copies or substantial portions of the Software.
  13 +//
  14 +// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  15 +// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  16 +// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  17 +// NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
  18 +// LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
  19 +// OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
  20 +// WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  21 +//
  22 +/*
  23 + * Copyright (c) 2002 Sergey Chaban <serge@wildwestsoftware.com>
  24 + */
  25 +
  26 +using System;
  27 +using System.IO;
  28 +using System.Runtime.InteropServices;
  29 +
  30 +namespace Mono.PEToolkit {
  31 +
  32 + public class DOSHeader {
  33 +
  34 + private readonly int OpenSize = 60;
  35 + private readonly int CloseSize = 64;
  36 +
  37 + private byte[] open_data; // First 60 bytes of data
  38 + private byte[] close_data; // Last 64 bytes of data
  39 +
  40 + // File address of new exe header.
  41 + private uint lfanew;
  42 +
  43 + public DOSHeader ()
  44 + {
  45 + Init ();
  46 + }
  47 +
  48 + public DOSHeader (BinaryReader reader)
  49 + {
  50 + Read (reader);
  51 + }
  52 +
  53 + public uint Lfanew {
  54 + get { return lfanew; }
  55 + }
  56 +
  57 + public void Read (BinaryReader reader)
  58 + {
  59 + open_data = reader.ReadBytes (OpenSize);
  60 + lfanew = reader.ReadUInt32 ();
  61 + close_data = reader.ReadBytes (CloseSize);
  62 + }
  63 +
  64 + public void Write (BinaryWriter writer)
  65 + {
  66 + writer.Write (open_data);
  67 + writer.Write (lfanew);
  68 + writer.Write (close_data);
  69 + }
  70 +
  71 + public void Init ()
  72 + {
  73 + open_data = new byte[] { 0x4D, 0x5A, 0x0, 0x0, 0xE7, 0x0, 0x0, 0x0,
  74 + 0x4, 0x0, 0x0, 0x0, 0xFF, 0xFF, 0x0, 0x0,
  75 + 0xB8, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0,
  76 + 0x40, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0,
  77 + 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0,
  78 + 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0,
  79 + 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0,
  80 + 0x0, 0x0, 0x0, 0x0 };
  81 +
  82 + close_data = new byte[] { 0xE, 0x1F, 0xBA, 0xE, 0x0, 0xB4, 0x9, 0xCD,
  83 + 0x21, 0xB8, 0x1, 0x4C, 0xCD, 0x21,0x54, 0x68,
  84 + 0x69, 0x73, 0x20, 0x70, 0x72, 0x6F, 0x67, 0x72,
  85 + 0x61, 0x6D, 0x20, 0x63, 0x61, 0x6E, 0x6E, 0x6F,
  86 + 0x74, 0x20, 0x62, 0x65, 0x20, 0x72, 0x75, 0x6E,
  87 + 0x20, 0x69, 0x6E, 0x20, 0x44, 0x4F, 0x53, 0x20,
  88 + 0x6D, 0x6F, 0x64, 0x65, 0x2E, 0xD, 0xD, 0xA,
  89 + 0x24, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0 };
  90 +
  91 + }
  92 +
  93 + /// <summary>
  94 + /// </summary>
  95 + /// <param name="writer"></param>
  96 + public void Dump(TextWriter writer)
  97 + {
  98 + writer.WriteLine(
  99 + "New header offset : {0}",
  100 + lfanew + " (0x" + lfanew.ToString("X") + ")"
  101 + );
  102 + }
  103 +
  104 + /// <summary>
  105 + /// </summary>
  106 + /// <returns></returns>
  107 + public override string ToString()
  108 + {
  109 + StringWriter sw = new StringWriter();
  110 + Dump(sw);
  111 + return sw.ToString();
  112 + }
  113 +
  114 + }
  115 +
  116 +}
  117 +
132 Mono.PEToolkit/DataDir.cs
... ... @@ -0,0 +1,132 @@
  1 +
  2 +//
  3 +// Permission is hereby granted, free of charge, to any person obtaining
  4 +// a copy of this software and associated documentation files (the
  5 +// "Software"), to deal in the Software without restriction, including
  6 +// without limitation the rights to use, copy, modify, merge, publish,
  7 +// distribute, sublicense, and/or sell copies of the Software, and to
  8 +// permit persons to whom the Software is furnished to do so, subject to
  9 +// the following conditions:
  10 +//
  11 +// The above copyright notice and this permission notice shall be
  12 +// included in all copies or substantial portions of the Software.
  13 +//
  14 +// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  15 +// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  16 +// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  17 +// NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
  18 +// LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
  19 +// OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
  20 +// WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  21 +//
  22 +/*
  23 + * Copyright (c) 2002 Sergey Chaban <serge@wildwestsoftware.com>
  24 + */
  25 +
  26 +using System;
  27 +using System.IO;
  28 +using System.Runtime.InteropServices;
  29 +
  30 +namespace Mono.PEToolkit {
  31 +
  32 + /// <summary>
  33 + /// IMAGE_DATA_DIRECTORY.
  34 + /// </summary>
  35 + public class DataDir {
  36 +
  37 + public static readonly DataDir Null;
  38 +
  39 + public RVA virtAddr;
  40 + public uint size;
  41 +
  42 + static DataDir ()
  43 + {
  44 + Null = new DataDir ();
  45 + Null.virtAddr = 0;
  46 + Null.size = 0;
  47 + }
  48 +
  49 + public DataDir () {
  50 +
  51 + }
  52 +
  53 + public DataDir (BinaryReader reader)
  54 + {
  55 + Read (reader);
  56 + }
  57 +
  58 + public void Read (BinaryReader reader)
  59 + {
  60 + virtAddr = new RVA (reader.ReadUInt32 ());
  61 + size = reader.ReadUInt32 ();
  62 + }
  63 +
  64 + public void Write (BinaryWriter writer)
  65 + {
  66 + virtAddr.Write (writer);
  67 + writer.Write (size);
  68 + }
  69 +
  70 + public RVA VirtualAddress {
  71 + get {
  72 + return virtAddr;
  73 + }
  74 + set {
  75 + virtAddr = value;
  76 + }
  77 + }
  78 +
  79 + public uint Size {
  80 + get {
  81 + return size;
  82 + }
  83 + set {
  84 + size = value;
  85 + }
  86 + }
  87 +
  88 + public bool IsNull {
  89 + get {
  90 + return (this == Null);
  91 + }
  92 + }
  93 +
  94 + public override int GetHashCode()
  95 + {
  96 + return (virtAddr.GetHashCode() ^ (int)(size << 1));
  97 + }