* CodeGenerator.cs, ObjectReader.cs, ObjectWriter.cs: Serialize decimals
[mono.git] / mcs / class / Mono.PEToolkit / DOSHeader.cs
1 /*\r
2  * Copyright (c) 2002 Sergey Chaban <serge@wildwestsoftware.com>\r
3  */\r
4 \r
5 using System;\r
6 using System.IO;\r
7 using System.Runtime.InteropServices;\r
8 \r
9 namespace Mono.PEToolkit {\r
10 \r
11         public class DOSHeader {\r
12                 \r
13                 private readonly int OpenSize = 60;\r
14                 private readonly int CloseSize = 64;\r
15 \r
16                 private byte[] open_data;       // First 60 bytes of data\r
17                 private byte[] close_data;      // Last 64 bytes of data\r
18 \r
19                 // File address of new exe header.\r
20                 private uint lfanew;\r
21 \r
22                 public DOSHeader ()\r
23                 {\r
24                         Init ();\r
25                 }\r
26 \r
27                 public DOSHeader (BinaryReader reader)\r
28                 {\r
29                         Read (reader);\r
30                 }\r
31 \r
32                 public uint Lfanew {\r
33                         get { return lfanew; }\r
34                 }\r
35 \r
36                 public void Read (BinaryReader reader)\r
37                 {\r
38                         open_data = reader.ReadBytes (OpenSize);\r
39                         lfanew = reader.ReadUInt32 ();\r
40                         close_data = reader.ReadBytes (CloseSize);\r
41                 }\r
42 \r
43                 public void Write (BinaryWriter writer)\r
44                 {\r
45                         writer.Write (open_data);\r
46                         writer.Write (lfanew);\r
47                         writer.Write (close_data);\r
48                 }\r
49 \r
50                 public void Init ()\r
51                 {\r
52                         open_data = new byte[] { 0x4D, 0x5A, 0x0, 0x0, 0xE7, 0x0, 0x0, 0x0, \r
53                                                  0x4, 0x0, 0x0, 0x0, 0xFF, 0xFF, 0x0, 0x0, \r
54                                                  0xB8, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, \r
55                                                  0x40, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, \r
56                                                  0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, \r
57                                                  0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, \r
58                                                  0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, \r
59                                                  0x0, 0x0, 0x0, 0x0 };\r
60                         \r
61                         close_data = new byte[] { 0xE, 0x1F, 0xBA, 0xE, 0x0, 0xB4, 0x9, 0xCD, \r
62                                                   0x21, 0xB8, 0x1, 0x4C, 0xCD, 0x21,0x54, 0x68, \r
63                                                   0x69, 0x73, 0x20, 0x70, 0x72, 0x6F, 0x67, 0x72, \r
64                                                   0x61, 0x6D, 0x20, 0x63, 0x61, 0x6E, 0x6E, 0x6F, \r
65                                                   0x74, 0x20, 0x62, 0x65, 0x20, 0x72, 0x75, 0x6E, \r
66                                                   0x20, 0x69, 0x6E, 0x20, 0x44, 0x4F, 0x53, 0x20, \r
67                                                   0x6D, 0x6F, 0x64, 0x65, 0x2E, 0xD, 0xD, 0xA, \r
68                                                   0x24, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0 };\r
69 \r
70                 }\r
71 \r
72                 /// <summary>\r
73                 /// </summary>\r
74                 /// <param name="writer"></param>\r
75                 public void Dump(TextWriter writer)\r
76                 {\r
77                         writer.WriteLine(\r
78                                 "New header offset   : {0}",\r
79                                 lfanew + " (0x" + lfanew.ToString("X") + ")"\r
80                         );\r
81                 }\r
82 \r
83                 /// <summary>\r
84                 /// </summary>\r
85                 /// <returns></returns>\r
86                 public override string ToString()\r
87                 {\r
88                         StringWriter sw = new StringWriter();\r
89                         Dump(sw);\r
90                         return sw.ToString();\r
91                 }\r
92 \r
93         }\r
94 \r
95 }\r
96 \r