File: AssemblyReader.cs

package info (click to toggle)
mono 4.6.2.7+dfsg-1
  • links: PTS, VCS
  • area: main
  • in suites: stretch
  • size: 778,148 kB
  • ctags: 914,052
  • sloc: cs: 5,779,509; xml: 2,773,713; ansic: 432,645; sh: 14,749; makefile: 12,361; perl: 2,488; python: 1,434; cpp: 849; asm: 531; sql: 95; sed: 16; php: 1
file content (168 lines) | stat: -rw-r--r-- 4,151 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
// ****************************************************************
// Copyright 2007, Charlie Poole
// This is free software licensed under the NUnit license. You may
// obtain a copy of the license at http://nunit.org/?p=license&r=2.4
// ****************************************************************
using System;
using System.Reflection;
using System.Text;
using System.IO;

namespace NUnit.Core
{
	/// <summary>
	/// AssemblyReader knows how to find various things in an assembly header
	/// </summary>
	public class AssemblyReader : IDisposable
	{
		private string assemblyPath;
		private BinaryReader rdr;
		private FileStream fs;

		UInt16 dos_magic = 0xffff;
		uint pe_signature = 0xffffffff;
		UInt16 numDataSections;
		UInt16 optionalHeaderSize;

		private uint peHeader = 0;
		private uint fileHeader = 0;
		private uint optionalHeader = 0;
		private uint dataDirectory = 0;
		private uint dataSections = 0;

		private struct Section
		{
			public uint virtualAddress;
			public uint virtualSize;
			public uint fileOffset;
		};

		private Section[] sections;

		public AssemblyReader( string assemblyPath )
		{
			this.assemblyPath = assemblyPath;
			CalcHeaderOffsets();
		}

		public AssemblyReader( Assembly assembly )
		{
			this.assemblyPath = TestFixtureBuilder.GetAssemblyPath( assembly );
			CalcHeaderOffsets();
		}

		private void CalcHeaderOffsets()
		{
			this.fs = new FileStream( assemblyPath, FileMode.Open, FileAccess.Read );
			this.rdr = new BinaryReader( fs );
			dos_magic = rdr.ReadUInt16();
			if ( dos_magic == 0x5a4d )
			{
				fs.Position = 0x3c;
				peHeader = rdr.ReadUInt32();
				fileHeader = peHeader + 4;
				optionalHeader = fileHeader + 20;
				dataDirectory = optionalHeader + 96;
				// dotNetDirectoryEntry = dataDirectory + 14 * 8;

				fs.Position = peHeader;
				pe_signature = rdr.ReadUInt32();
				rdr.ReadUInt16(); // machine
				numDataSections = rdr.ReadUInt16();
				fs.Position += 12;
				optionalHeaderSize = rdr.ReadUInt16();
				dataSections = optionalHeader + optionalHeaderSize;

				sections = new Section[numDataSections];
				fs.Position = dataSections;
				for( int i = 0; i < numDataSections; i++ )
				{
					fs.Position += 8;
					sections[i].virtualSize = rdr.ReadUInt32();
					sections[i].virtualAddress = rdr.ReadUInt32();
					uint rawDataSize = rdr.ReadUInt32();
					sections[i].fileOffset = rdr.ReadUInt32();
					if ( sections[i].virtualSize == 0 )
						sections[i].virtualSize = rawDataSize;

					fs.Position += 16;
				}
			}
		}

		private uint DataDirectoryRva( int n )
		{
			fs.Position = dataDirectory + n * 8;
			return rdr.ReadUInt32();
		}

		private uint RvaToLfa( uint rva )
		{
			for( int i = 0; i < numDataSections; i++ )
				if ( rva >= sections[i].virtualAddress && rva < sections[i].virtualAddress + sections[i].virtualSize )
					return rva - sections[i].virtualAddress + sections[i].fileOffset;

			return 0;
		}

		public string AssemblyPath
		{
			get { return assemblyPath; }
		}

		public bool IsValidPeFile
		{
			get { return dos_magic == 0x5a4d && pe_signature == 0x00004550; }
		}

		public bool IsDotNetFile
		{
			get { return IsValidPeFile && DataDirectoryRva(14) != 0; }
		}

		public string ImageRuntimeVersion
		{
			get 
			{
				string runtimeVersion = string.Empty;

				uint rva = DataDirectoryRva(14);
				if ( rva != 0 )
				{
					fs.Position = RvaToLfa( rva ) + 8;
					uint metadata = rdr.ReadUInt32();
					fs.Position = RvaToLfa( metadata );
					if ( rdr.ReadUInt32() == 0x424a5342 )
					{
						// Copy string representing runtime version
						fs.Position += 12;
						StringBuilder sb = new StringBuilder();
						char c;
						while ((c = rdr.ReadChar())!= '\0')
							sb.Append(c);

						if (sb[0] == 'v') // Last sanity check
							runtimeVersion = sb.ToString();

						// Could do fixups here for bad values in older files
						// like 1.x86, 1.build, etc. But we are only using
						// the major version anyway
					}
				}

				return runtimeVersion; 
			}
		}

		public void Dispose()
		{
			if ( fs != null )
				fs.Close();
			if ( rdr != null )
				rdr.Close();

			fs = null;
			rdr = null;
		}
	}
}