source: trunk/tools/common/kFilePE.cpp@ 3830

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

Initial checkin.

File size: 7.5 KB
Line 
1/*
2 * kFilePE - PE files.
3 *
4 * Copyright (c) 1999 knut st. osmundsen
5 *
6 */
7
8/*******************************************************************************
9* Defined Constants *
10*******************************************************************************/
11/* emx fixups */
12#ifdef __EMX__
13 #define __stdcall
14 #define max(a,b) (((a) > (b)) ? (a) : (b))
15 #define min(a,b) (((a) < (b)) ? (a) : (b))
16#endif
17
18/******************************************************************************
19* Header Files *
20******************************************************************************/
21#ifdef __EMX__
22#define INT INT_
23#define PCHAR PCHAR_
24#endif
25#include <os2.h>
26#ifdef __EMX__
27#undef PCHAR
28#undef INT
29#endif
30#include <stdio.h>
31#include <stdlib.h>
32#include <string.h>
33#include <malloc.h>
34#include <assert.h>
35#include <peexe.h>
36#include "kFileFormatBase.h"
37#include "kFilePe.h"
38
39
40/**
41 * Constructs a kFilePE object for a file.
42 * @param phFile File to create object from.
43 * @remark throws errorcode (TODO: errorhandling.)
44 */
45kFilePE::kFilePE(FILE *phFile) throw(int) : pvBase(NULL),
46 pDosHdr(NULL), pFileHdr(NULL), pOptHdr(NULL), paDataDir(NULL), paSectionHdr(NULL),
47 pExportDir(NULL)
48{
49 IMAGE_DOS_HEADER doshdr;
50
51 /* read dos-header - assumes there is one */
52 if (!fseek(phFile, 0, SEEK_SET)
53 && fread(&doshdr, sizeof(doshdr), 1, phFile) == 1
54 && doshdr.e_magic == IMAGE_DOS_SIGNATURE
55 && doshdr.e_lfanew > sizeof(doshdr)
56 )
57 {
58 IMAGE_NT_HEADERS pehdr;
59
60 /* read pe headers */
61 if (!fseek(phFile, doshdr.e_lfanew, SEEK_SET)
62 && fread(&pehdr, sizeof(pehdr), 1, phFile) == 1
63 && pehdr.Signature == IMAGE_NT_SIGNATURE
64 && pehdr.FileHeader.SizeOfOptionalHeader == sizeof(IMAGE_OPTIONAL_HEADER)
65 && pehdr.OptionalHeader.Magic == IMAGE_NT_OPTIONAL_HDR_MAGIC)
66 {
67 /* create mapping */
68 pvBase = malloc((size_t)pehdr.OptionalHeader.SizeOfImage);
69 if (pvBase != NULL)
70 {
71 memset(pvBase, 0, (size_t)pehdr.OptionalHeader.SizeOfImage);
72 /*
73 printf("%ld\n", pehdr.OptionalHeader.SizeOfHeaders);
74 printf("%ld\n", sizeof(IMAGE_NT_HEADERS) + sizeof(IMAGE_SECTION_HEADER) * pehdr.FileHeader.NumberOfSections);
75 assert(pehdr.OptionalHeader.SizeOfHeaders ==
76 sizeof(IMAGE_NT_HEADERS) + sizeof(IMAGE_SECTION_HEADER) * pehdr.FileHeader.NumberOfSections);
77 */
78 if (!fseek(phFile, 0, SEEK_SET)
79 && fread(pvBase, (size_t)pehdr.OptionalHeader.SizeOfHeaders, 1, phFile) == 1
80 )
81 {
82 /* read sections */
83 for (int i = 0; i < pehdr.FileHeader.NumberOfSections; i++)
84 {
85 ULONG cbSection;
86 PIMAGE_SECTION_HEADER pSectionHdr =
87 #if 0
88 IMAGE_FIRST_SECTION(((ULONG)pvBase + ((PIMAGE_DOS_HEADER)pvBase)->e_lfanew));
89 #else
90 (PIMAGE_SECTION_HEADER) ( (ULONG)pvBase + doshdr.e_lfanew + sizeof(IMAGE_NT_HEADERS) );
91 #endif
92 pSectionHdr += i;
93
94 cbSection = min(pSectionHdr->Misc.VirtualSize, pSectionHdr->SizeOfRawData);
95 if (fseek(phFile, pSectionHdr->PointerToRawData, SEEK_SET)
96 ||
97 fread((void*)((ULONG)pvBase + pSectionHdr->VirtualAddress), (size_t)cbSection, 1, phFile) != 1
98 )
99 {
100 /* error */
101 free(pvBase);
102 pvBase = NULL;
103 throw(6);
104 }
105 }
106
107 /* set pointers */
108 if (*(unsigned short*)pvBase == IMAGE_DOS_SIGNATURE)
109 {
110 pDosHdr = (PIMAGE_DOS_HEADER)pvBase;
111 pFileHdr = (PIMAGE_FILE_HEADER)((DWORD)pvBase + pDosHdr->e_lfanew + 4);
112 }
113 else
114 pFileHdr = (PIMAGE_FILE_HEADER)((DWORD)pvBase + 4);
115
116 pOptHdr = (PIMAGE_OPTIONAL_HEADER)((int)pFileHdr + sizeof(*pFileHdr));
117 paDataDir = (PIMAGE_DATA_DIRECTORY)((int)pOptHdr + pFileHdr->SizeOfOptionalHeader
118 - pOptHdr->NumberOfRvaAndSizes*sizeof(*paDataDir));
119 paSectionHdr = (PIMAGE_SECTION_HEADER)((int)paDataDir +
120 pOptHdr->NumberOfRvaAndSizes*sizeof(*paDataDir));
121
122 if (paDataDir[IMAGE_DIRECTORY_ENTRY_EXPORT].VirtualAddress)
123 pExportDir = (PIMAGE_EXPORT_DIRECTORY)((int)pvBase + paDataDir[IMAGE_DIRECTORY_ENTRY_EXPORT].VirtualAddress);
124 }
125 else
126 throw(4);
127 }
128 else
129 throw(3);
130 }
131 else
132 throw(2);
133 }
134 else
135 throw(1);
136}
137
138
139/**
140 * Destructor.
141 */
142kFilePE::~kFilePE()
143{
144 if (pvBase)
145 delete pvBase;
146}
147
148
149/**
150 * Query for the module name.
151 * @returns Success indicator. TRUE / FALSE.
152 * @param pszBuffer Pointer to buffer which to put the name into.
153 */
154BOOL kFilePE::queryModuleName(char *pszBuffer)
155{
156 if (pExportDir && pExportDir->Name)
157 strcpy(pszBuffer, (char*)((int)pExportDir->Name + (int)pvBase));
158 else
159 return FALSE;
160
161 return TRUE;
162}
163
164
165/**
166 * Finds the first exports.
167 * @returns Success indicator. TRUE / FALSE.
168 * @param pExport Pointer to export structure.
169 * @remark
170 */
171BOOL kFilePE::findFirstExport(PEXPORTENTRY pExport)
172{
173 if (pExportDir && pExportDir->NumberOfFunctions)
174 {
175 memset(pExport, 0, sizeof(EXPORTENTRY));
176 pExport->ulOrdinal = pExportDir->Base - 1;
177 return findNextExport(pExport);
178 }
179
180 return FALSE;
181}
182
183
184/**
185 * Finds the next export.
186 * @returns Success indicator. TRUE / FALSE.
187 * @param pExport Pointer to export structure.
188 * @remark
189 */
190BOOL kFilePE::findNextExport(PEXPORTENTRY pExport)
191{
192 if (pExportDir && pExportDir->NumberOfFunctions)
193 {
194 void **ppv = (void**)((int)pExportDir->AddressOfFunctions + (int)pvBase);
195
196 ++pExport->ulOrdinal -= pExportDir->Base;
197 while (ppv[pExport->ulOrdinal] == NULL && pExport->ulOrdinal < pExportDir->NumberOfFunctions)
198 pExport->ulOrdinal++;
199
200 if (pExport->ulOrdinal < pExportDir->NumberOfFunctions)
201 {
202 int iName = 0;
203 unsigned short *pawNameOrdinals = (unsigned short *)
204 ((int)pExportDir->AddressOfNameOrdinals + (int)pvBase);
205
206 /* look for name */
207 while (iName < (int)pExportDir->NumberOfNames &&
208 pawNameOrdinals[iName] != pExport->ulOrdinal)
209 iName++;
210 if (iName < (int)pExportDir->NumberOfNames)
211 strcpy(&pExport->achName[0],
212 (char*)((int)pvBase + ((int*)((int)pvBase + (int)pExportDir->AddressOfNames))[iName]));
213 else
214 pExport->achName[0] = '\0';
215 pExport->ulOrdinal += pExportDir->Base;
216 }
217 else
218 return FALSE;
219 }
220 else
221 return FALSE;
222
223 pExport->achIntName[0] = '\0';
224 pExport->pv = NULL;
225 return TRUE;
226}
227
228
Note: See TracBrowser for help on using the repository browser.