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:
11 // The above copyright notice and this permission notice shall be
12 // included in all copies or substantial portions of the Software.
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.
23 * Copyright (c) 2002 Sergey Chaban <serge@wildwestsoftware.com>
\r
28 using System.Runtime.InteropServices;
\r
30 namespace Mono.PEToolkit {
\r
32 public class DOSHeader {
\r
34 private readonly int OpenSize = 60;
\r
35 private readonly int CloseSize = 64;
\r
37 private byte[] open_data; // First 60 bytes of data
\r
38 private byte[] close_data; // Last 64 bytes of data
\r
40 // File address of new exe header.
\r
41 private uint lfanew;
\r
48 public DOSHeader (BinaryReader reader)
\r
53 public uint Lfanew {
\r
54 get { return lfanew; }
\r
57 public void Read (BinaryReader reader)
\r
59 open_data = reader.ReadBytes (OpenSize);
\r
60 lfanew = reader.ReadUInt32 ();
\r
61 close_data = reader.ReadBytes (CloseSize);
\r
64 public void Write (BinaryWriter writer)
\r
66 writer.Write (open_data);
\r
67 writer.Write (lfanew);
\r
68 writer.Write (close_data);
\r
73 open_data = new byte[] { 0x4D, 0x5A, 0x0, 0x0, 0xE7, 0x0, 0x0, 0x0,
\r
74 0x4, 0x0, 0x0, 0x0, 0xFF, 0xFF, 0x0, 0x0,
\r
75 0xB8, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0,
\r
76 0x40, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0,
\r
77 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0,
\r
78 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0,
\r
79 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0,
\r
80 0x0, 0x0, 0x0, 0x0 };
\r
82 close_data = new byte[] { 0xE, 0x1F, 0xBA, 0xE, 0x0, 0xB4, 0x9, 0xCD,
\r
83 0x21, 0xB8, 0x1, 0x4C, 0xCD, 0x21,0x54, 0x68,
\r
84 0x69, 0x73, 0x20, 0x70, 0x72, 0x6F, 0x67, 0x72,
\r
85 0x61, 0x6D, 0x20, 0x63, 0x61, 0x6E, 0x6E, 0x6F,
\r
86 0x74, 0x20, 0x62, 0x65, 0x20, 0x72, 0x75, 0x6E,
\r
87 0x20, 0x69, 0x6E, 0x20, 0x44, 0x4F, 0x53, 0x20,
\r
88 0x6D, 0x6F, 0x64, 0x65, 0x2E, 0xD, 0xD, 0xA,
\r
89 0x24, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0 };
\r
95 /// <param name="writer"></param>
\r
96 public void Dump(TextWriter writer)
\r
99 "New header offset : {0}",
\r
100 lfanew + " (0x" + lfanew.ToString("X") + ")"
\r
106 /// <returns></returns>
\r
107 public override string ToString()
\r
109 StringWriter sw = new StringWriter();
\r
111 return sw.ToString();
\r