IKVM11  11
Java SE 11 Virtual Machine for .NET
Loading...
Searching...
No Matches
PEReader.cs
Go to the documentation of this file.
1/*
2 Copyright (C) 2009 Jeroen Frijters
3
4 This software is provided 'as-is', without any express or implied
5 warranty. In no event will the authors be held liable for any damages
6 arising from the use of this software.
7
8 Permission is granted to anyone to use this software for any purpose,
9 including commercial applications, and to alter it and redistribute it
10 freely, subject to the following restrictions:
11
12 1. The origin of this software must not be misrepresented; you must not
13 claim that you wrote the original software. If you use this software
14 in a product, an acknowledgment in the product documentation would be
15 appreciated but is not required.
16 2. Altered source versions must be plainly marked as such, and must not be
17 misrepresented as being the original software.
18 3. This notice may not be removed or altered from any source distribution.
19
20 Jeroen Frijters
21 jeroen@frijters.net
22
23*/
24using System.IO;
25
26using DWORD = System.UInt32;
27
29{
30
31 sealed class PEReader
32 {
33
34 MSDOS_HEADER msdos = new MSDOS_HEADER();
35 IMAGE_NT_HEADERS headers = new IMAGE_NT_HEADERS();
36 SectionHeader[] sections;
37 bool mapped;
38
39 internal void Read(BinaryReader br, bool mapped)
40 {
41 this.mapped = mapped;
42 msdos.signature = br.ReadUInt16();
43 br.BaseStream.Seek(58, SeekOrigin.Current);
44 msdos.peSignatureOffset = br.ReadUInt32();
45
46 if (msdos.signature != MSDOS_HEADER.MAGIC_MZ)
47 throw new BadImageFormatException();
48
49 br.BaseStream.Seek(msdos.peSignatureOffset, SeekOrigin.Begin);
50 headers.Read(br);
51 sections = new SectionHeader[headers.FileHeader.NumberOfSections];
52 for (int i = 0; i < sections.Length; i++)
53 {
54 sections[i] = new SectionHeader();
55 sections[i].Read(br);
56 }
57 }
58
59 internal IMAGE_FILE_HEADER FileHeader
60 {
61 get { return headers.FileHeader; }
62 }
63
64 internal IMAGE_OPTIONAL_HEADER OptionalHeader
65 {
66 get { return headers.OptionalHeader; }
67 }
68
69 internal DWORD GetComDescriptorVirtualAddress()
70 {
72 }
73
74 internal void GetDataDirectoryEntry(int index, out int rva, out int length)
75 {
76 rva = (int)headers.OptionalHeader.DataDirectory[index].VirtualAddress;
77 length = (int)headers.OptionalHeader.DataDirectory[index].Size;
78 }
79
80 internal long RvaToFileOffset(DWORD rva)
81 {
82 if (mapped)
83 return rva;
84
85 for (int i = 0; i < sections.Length; i++)
86 if (rva >= sections[i].VirtualAddress && rva < sections[i].VirtualAddress + sections[i].VirtualSize)
87 return sections[i].PointerToRawData + rva - sections[i].VirtualAddress;
88
89 throw new BadImageFormatException();
90 }
91
92 internal bool GetSectionInfo(int rva, out string name, out int characteristics, out int virtualAddress, out int virtualSize, out int pointerToRawData, out int sizeOfRawData)
93 {
94 for (int i = 0; i < sections.Length; i++)
95 {
96 if (rva >= sections[i].VirtualAddress && rva < sections[i].VirtualAddress + sections[i].VirtualSize)
97 {
98 name = sections[i].Name;
99 characteristics = (int)sections[i].Characteristics;
100 virtualAddress = (int)sections[i].VirtualAddress;
101 virtualSize = (int)sections[i].VirtualSize;
102 pointerToRawData = (int)sections[i].PointerToRawData;
103 sizeOfRawData = (int)sections[i].SizeOfRawData;
104 return true;
105 }
106 }
107
108 name = null;
109 characteristics = 0;
110 virtualAddress = 0;
111 virtualSize = 0;
112 pointerToRawData = 0;
113 sizeOfRawData = 0;
114 return false;
115 }
116
117 }
118
119}
System.UInt32 DWORD