migrate substitution keywords to SVN
[reactos.git] / reactos / lib / rtl / image.c
1 /* $Id$
2 *
3 * COPYRIGHT: See COPYING in the top level directory
4 * PROJECT: ReactOS kernel
5 * FILE: lib/ntdll/rtl/image.c
6 * PURPOSE: Image handling functions
7 * PROGRAMMER: Eric Kohl
8 * UPDATE HISTORY:
9 * 17/03/2000 Created
10 */
11
12 #include <ddk/ntddk.h>
13 #include <ntdll/rtl.h>
14
15 #define NDEBUG
16 #include <ntdll/ntdll.h>
17
18 /* FUNCTIONS ****************************************************************/
19
20 /*
21 * @implemented
22 */
23 PIMAGE_NT_HEADERS STDCALL
24 RtlImageNtHeader (IN PVOID BaseAddress)
25 {
26 PIMAGE_NT_HEADERS NtHeader;
27 PIMAGE_DOS_HEADER DosHeader = (PIMAGE_DOS_HEADER)BaseAddress;
28
29 if (DosHeader && DosHeader->e_magic != IMAGE_DOS_SIGNATURE)
30 {
31 DPRINT1("DosHeader->e_magic %x\n", DosHeader->e_magic);
32 DPRINT1("NtHeader %x\n", (BaseAddress + DosHeader->e_lfanew));
33 }
34
35 if (DosHeader && DosHeader->e_magic == IMAGE_DOS_SIGNATURE)
36 {
37 NtHeader = (PIMAGE_NT_HEADERS)(BaseAddress + DosHeader->e_lfanew);
38 if (NtHeader->Signature == IMAGE_NT_SIGNATURE)
39 return NtHeader;
40 }
41
42 return NULL;
43 }
44
45
46 /*
47 * @implemented
48 */
49 PVOID
50 STDCALL
51 RtlImageDirectoryEntryToData (
52 PVOID BaseAddress,
53 BOOLEAN bFlag,
54 ULONG Directory,
55 PULONG Size
56 )
57 {
58 PIMAGE_NT_HEADERS NtHeader;
59 ULONG Va;
60
61 NtHeader = RtlImageNtHeader (BaseAddress);
62 if (NtHeader == NULL)
63 return NULL;
64
65 if (Directory >= NtHeader->OptionalHeader.NumberOfRvaAndSizes)
66 return NULL;
67
68 Va = NtHeader->OptionalHeader.DataDirectory[Directory].VirtualAddress;
69 if (Va == 0)
70 return NULL;
71
72 if (Size)
73 *Size = NtHeader->OptionalHeader.DataDirectory[Directory].Size;
74
75 if (bFlag)
76 return (PVOID)(BaseAddress + Va);
77
78 /* image mapped as ordinary file, we must find raw pointer */
79 return (PVOID)RtlImageRvaToVa (NtHeader, BaseAddress, Va, NULL);
80 }
81
82
83 /*
84 * @implemented
85 */
86 PIMAGE_SECTION_HEADER
87 STDCALL
88 RtlImageRvaToSection (
89 PIMAGE_NT_HEADERS NtHeader,
90 PVOID BaseAddress,
91 ULONG Rva
92 )
93 {
94 PIMAGE_SECTION_HEADER Section;
95 ULONG Va;
96 ULONG Count;
97
98 Count = NtHeader->FileHeader.NumberOfSections;
99 Section = (PIMAGE_SECTION_HEADER)((ULONG)&NtHeader->OptionalHeader +
100 NtHeader->FileHeader.SizeOfOptionalHeader);
101 while (Count)
102 {
103 Va = Section->VirtualAddress;
104 if ((Va <= Rva) &&
105 (Rva < Va + Section->SizeOfRawData))
106 return Section;
107 Section++;
108 }
109 return NULL;
110 }
111
112
113 /*
114 * @implemented
115 */
116 ULONG
117 STDCALL
118 RtlImageRvaToVa (
119 PIMAGE_NT_HEADERS NtHeader,
120 PVOID BaseAddress,
121 ULONG Rva,
122 PIMAGE_SECTION_HEADER *SectionHeader
123 )
124 {
125 PIMAGE_SECTION_HEADER Section = NULL;
126
127 if (SectionHeader)
128 Section = *SectionHeader;
129
130 if (Section == NULL ||
131 Rva < Section->VirtualAddress ||
132 Rva >= Section->VirtualAddress + Section->SizeOfRawData)
133 {
134 Section = RtlImageRvaToSection (NtHeader, BaseAddress, Rva);
135 if (Section == NULL)
136 return 0;
137
138 if (SectionHeader)
139 *SectionHeader = Section;
140 }
141
142 return (ULONG)(BaseAddress +
143 Rva +
144 Section->PointerToRawData -
145 Section->VirtualAddress);
146 }
147
148 /* EOF */