source: trunk/src/win32k/pe2lx/pefile.cpp@ 847

Last change on this file since 847 was 847, checked in by bird, 26 years ago

Initial checkin of Win32k. (not tested & pe2lx not up-to-date!)

File size: 7.7 KB
Line 
1/* $Id: pefile.cpp,v 1.1 1999-09-06 02:20:08 bird Exp $ */
2
3/*
4 * PE2LX PE utility functions
5 *
6 * Project Odin Software License can be found in LICENSE.TXT
7 *
8 */
9
10/*******************************************************************************
11* Defined Constants And Macros *
12*******************************************************************************/
13#include "pe2lx.h"
14
15#ifdef __WATCOMC__
16 #include <mem.h>
17#endif
18
19#define MAX_RES 17
20extern char *ResTypes[MAX_RES];
21
22//******************************************************************************
23//******************************************************************************
24static char *UnicodeToFixedAsciiString(int length, WCHAR *NameString)
25{
26static char asciistring[256];
27int i;
28
29 if(length >= 255) length = 255;
30 for(i=0;i<length;i++) {
31 asciistring[i] = NameString[i] & 0xFF;
32 }
33 asciistring[length] = 0;
34 return(asciistring);
35}
36//******************************************************************************
37//******************************************************************************
38BOOL GetPEFileHeader (LPVOID lpFile, PIMAGE_FILE_HEADER pHeader)
39{
40 if(*(USHORT *)lpFile == IMAGE_DOS_SIGNATURE &&
41 *(DWORD *)PE_HEADER (lpFile) == IMAGE_NT_SIGNATURE)
42 {
43 memcpy ((LPVOID)pHeader, PEHEADEROFF (lpFile), sizeof (IMAGE_FILE_HEADER));
44 return TRUE;
45 }
46 else return FALSE;
47}
48
49/** copy optional header info to structure */
50BOOL GetPEOptionalHeader (LPVOID lpFile, PIMAGE_OPTIONAL_HEADER pHeader)
51{
52 if(*(USHORT *)lpFile == IMAGE_DOS_SIGNATURE &&
53 *(DWORD *)PE_HEADER (lpFile) == IMAGE_NT_SIGNATURE)
54 {
55 memcpy ((LPVOID)pHeader, OPTHEADEROFF (lpFile), sizeof (IMAGE_OPTIONAL_HEADER));
56 return TRUE;
57 }
58 else return FALSE;
59}
60
61
62LPVOID ImageDirectoryOffset (LPVOID lpFile, DWORD dwIMAGE_DIRECTORY)
63{
64 PIMAGE_OPTIONAL_HEADER poh = (PIMAGE_OPTIONAL_HEADER)OPTHEADEROFF (lpFile);
65 IMAGE_SECTION_HEADER sh;
66
67 if (dwIMAGE_DIRECTORY >= poh->NumberOfRvaAndSizes)
68 return NULL;
69
70 if(GetSectionHdrByRVA(lpFile, &sh, poh->DataDirectory[dwIMAGE_DIRECTORY].VirtualAddress) == FALSE)
71 {
72 return NULL;
73 }
74
75 return (LPVOID)(((ULONG)lpFile + (ULONG)(poh->DataDirectory[dwIMAGE_DIRECTORY].VirtualAddress -
76 sh.VirtualAddress) + (ULONG)sh.PointerToRawData));
77}
78//******************************************************************************
79//******************************************************************************
80BOOL GetSectionHdrByImageDir(LPVOID lpFile, DWORD dwIMAGE_DIRECTORY, PIMAGE_SECTION_HEADER pSect)
81{
82 PIMAGE_OPTIONAL_HEADER poh = (PIMAGE_OPTIONAL_HEADER)OPTHEADEROFF (lpFile);
83
84 if (dwIMAGE_DIRECTORY >= poh->NumberOfRvaAndSizes)
85 return FALSE;
86
87 return GetSectionHdrByRVA(lpFile, pSect, poh->DataDirectory[dwIMAGE_DIRECTORY].VirtualAddress);
88 }
89//******************************************************************************
90//******************************************************************************
91BOOL IsImportSection(LPVOID lpFile, PIMAGE_SECTION_HEADER psh)
92{
93 PIMAGE_OPTIONAL_HEADER poh = (PIMAGE_OPTIONAL_HEADER)OPTHEADEROFF (lpFile);
94 int i = 0;
95 DWORD ImageDirVA;
96
97 ImageDirVA = poh->DataDirectory[IMAGE_DIRECTORY_ENTRY_IMPORT].VirtualAddress;
98
99 if(psh->VirtualAddress <= ImageDirVA &&
100 psh->VirtualAddress + max(psh->Misc.VirtualSize,psh->SizeOfRawData) > ImageDirVA &&
101 strcmp(psh->Name, ".idata") == 0)
102 {
103 return TRUE;
104 }
105 return FALSE;
106}
107//******************************************************************************
108//******************************************************************************
109BOOL GetSectionHdrByName (LPVOID lpFile, IMAGE_SECTION_HEADER *sh, char *szSection)
110{
111 PIMAGE_SECTION_HEADER psh;
112 int nSections = NR_SECTIONS (lpFile);
113 int i;
114
115 if((psh = (PIMAGE_SECTION_HEADER)SECTIONHDROFF (lpFile)) != NULL)
116 {
117 for (i=0; i<nSections; i++)
118 {
119 if(strcmp (psh->Name, szSection) == 0)
120 {
121 memcpy ((LPVOID)sh, (LPVOID)psh, sizeof (IMAGE_SECTION_HEADER));
122 return TRUE;
123 }
124 else psh++;
125 }
126 }
127 return FALSE;
128}
129
130
131/* function gets the function header for a section identified by type */
132BOOL GetSectionHdrByType (LPVOID lpFile, IMAGE_SECTION_HEADER *sh, int type)
133{
134 PIMAGE_SECTION_HEADER psh;
135 int nSections = NR_SECTIONS (lpFile);
136 int i;
137
138 if((psh = (PIMAGE_SECTION_HEADER)SECTIONHDROFF (lpFile)) != NULL)
139 {
140 for (i=0; i<nSections; i++)
141 {
142 if (psh->Characteristics & type)
143 {
144 memcpy ((LPVOID)sh, (LPVOID)psh, sizeof (IMAGE_SECTION_HEADER));
145 return TRUE;
146 }
147 else psh++;
148 }
149 }
150 return FALSE;
151}
152//******************************************************************************
153//******************************************************************************
154int GetNumberOfResources(LPVOID lpFile)
155{
156 PIMAGE_RESOURCE_DIRECTORY prdRoot, prdType;
157 PIMAGE_RESOURCE_DIRECTORY_ENTRY prde;
158 int nCnt=0, i, j, id;
159 char *resname;
160
161 if ((prdRoot = (PIMAGE_RESOURCE_DIRECTORY)ImageDirectoryOffset
162 (lpFile, IMAGE_DIRECTORY_ENTRY_RESOURCE)) == NULL)
163 return 0;
164
165 prde = (PIMAGE_RESOURCE_DIRECTORY_ENTRY)((DWORD)prdRoot + sizeof (IMAGE_RESOURCE_DIRECTORY));
166
167 for (i=0; i<prdRoot->NumberOfNamedEntries+prdRoot->NumberOfIdEntries; i++)
168 {
169 prdType = (PIMAGE_RESOURCE_DIRECTORY)((ULONG)prdRoot + (ULONG)prde->u2.OffsetToData);
170
171 if(i<prdRoot->NumberOfNamedEntries) {
172 //SvL: 30-10-'97, high bit is set, so clear to get real offset
173 prde->u1.Name &= ~0x80000000;
174 for(j=0;j< MAX_RES ;j++) {
175 resname = UnicodeToFixedAsciiString(*(WCHAR *)((ULONG)prdRoot + (ULONG)prde->u1.Name), (WCHAR *)((ULONG)prdRoot + (ULONG)prde->u1.Name + sizeof(WCHAR))); // first word = string length
176 if (strcmp(resname, ResTypes[j]) == 0)
177 break;
178 }
179 if(j == MAX_RES) {
180 //SvL: 30-10-'97, not found = custom resource type (correct?)
181 id = NTRT_RCDATA;
182 }
183 else id = j;
184 }
185 else id = prde->u1.Id;
186
187 prdType = (PIMAGE_RESOURCE_DIRECTORY)((DWORD)prdType ^ 0x80000000);
188
189 if(id == NTRT_STRING) {
190 //String tables can contain up to 16 individual resources!
191 nCnt += prdType->NumberOfNamedEntries*16 + prdType->NumberOfIdEntries*16;
192 }
193 else {
194 //Only icon groups are stored as resources in the LX file
195 //Icon groups contain one or more icons
196 if (id != NTRT_ICON)
197 nCnt += prdType->NumberOfNamedEntries + prdType->NumberOfIdEntries;
198 }
199 prde++;
200 }
201
202 return nCnt;
203}
204/**
205 * Get Section Header for the given RVA - returns boolean according to the result.<br>
206 * knut [Jul 22 1998 2:53am]
207 */
208BOOL GetSectionHdrByRVA (LPVOID lpFile, IMAGE_SECTION_HEADER *sh, ULONG rva)
209{
210 PIMAGE_SECTION_HEADER psh;
211 int nSections = NR_SECTIONS (lpFile);
212 int i;
213
214 if ((psh = (PIMAGE_SECTION_HEADER)SECTIONHDROFF (lpFile)) != NULL)
215 {
216 for (i=0; i<nSections; i++)
217 {
218 if (rva >= psh->VirtualAddress && rva < psh->VirtualAddress + max(psh->Misc.VirtualSize,psh->SizeOfRawData))
219 {
220 memcpy (sh, psh, sizeof(IMAGE_SECTION_HEADER));
221 return TRUE;
222}
223 else psh++;
224 }
225 }
226 return FALSE;
227}
228//******************************************************************************
229//******************************************************************************
Note: See TracBrowser for help on using the repository browser.