|
1 // Copyright (c) 2003-2009 Nokia Corporation and/or its subsidiary(-ies). |
|
2 // All rights reserved. |
|
3 // This component and the accompanying materials are made available |
|
4 // under the terms of the License "Eclipse Public License v1.0" |
|
5 // which accompanies this distribution, and is available |
|
6 // at the URL "http://www.eclipse.org/legal/epl-v10.html". |
|
7 // |
|
8 // Initial Contributors: |
|
9 // Nokia Corporation - initial contribution. |
|
10 // |
|
11 // Contributors: |
|
12 // |
|
13 // Description: |
|
14 // |
|
15 |
|
16 #include <e32def.h> |
|
17 #include <e32def_private.h> |
|
18 #include <e32err.h> |
|
19 |
|
20 #include "h_ver.h" |
|
21 #include "pe_defs.h" |
|
22 #include <sys\stat.h> |
|
23 |
|
24 #if defined(__MSVCDOTNET__) || defined (__TOOLS2__) |
|
25 #include <iostream> |
|
26 #include <iomanip> |
|
27 #include <fstream> |
|
28 #include <string> |
|
29 using namespace std; |
|
30 #else //__MSVCDOTNET__ |
|
31 #include <iostream.h> |
|
32 #include <iomanip.h> |
|
33 #include <fstream.h> |
|
34 #include <string.h> |
|
35 #endif //__MSVCDOTNET__ |
|
36 |
|
37 #include <stdio.h> |
|
38 |
|
39 const int KDiffIdentical=0; |
|
40 const int KDiffDifferent=2; |
|
41 |
|
42 class PeFile |
|
43 { |
|
44 public: |
|
45 PeFile(); |
|
46 ~PeFile(); |
|
47 int Open(char *aFileName); |
|
48 void Close(); |
|
49 PIMAGE_DOS_HEADER DosHeader(); |
|
50 PIMAGE_NT_HEADERS Headers(); |
|
51 PIMAGE_SECTION_HEADER SectionHeaders(); |
|
52 char *LoadSection(PIMAGE_SECTION_HEADER aSectionHeader); |
|
53 int NumberOfSections(); |
|
54 char *LoadExtraData(int aOffsetToExtraData, TUint aFileSize); |
|
55 |
|
56 int Errno(); // Should be invoked only if previous call of PeFile's |
|
57 // member function returns incorrectly |
|
58 int Errno(int aErrno); // Assign error number for special purpose. |
|
59 public: |
|
60 fstream iFile; |
|
61 PIMAGE_DOS_HEADER iDosHeader; |
|
62 PIMAGE_NT_HEADERS iHeaders; |
|
63 PIMAGE_SECTION_HEADER iSectionHeaders; |
|
64 private: |
|
65 int errno; |
|
66 }; |
|
67 |
|
68 PeFile::PeFile() |
|
69 : iDosHeader(NULL), iHeaders(NULL), iSectionHeaders(NULL), errno(KErrNone) |
|
70 {} |
|
71 |
|
72 PeFile::~PeFile() |
|
73 { |
|
74 delete iDosHeader; |
|
75 delete iHeaders; |
|
76 delete [] iSectionHeaders; |
|
77 } |
|
78 |
|
79 int PeFile::Open(char *aFileName) |
|
80 { |
|
81 #if defined(__MSVCDOTNET__) || defined(__TOOLS2__) |
|
82 iFile.open(aFileName, ios::in | ios::binary); |
|
83 #else //!__MSVCDOTNET__ |
|
84 iFile.open(aFileName, ios::in | ios::binary | ios::nocreate); |
|
85 #endif //__MSVCDOTNET__ |
|
86 if (!iFile.is_open()) { |
|
87 this->errno = KErrNotFound; |
|
88 return KErrNotFound; |
|
89 } |
|
90 return KErrNone; |
|
91 } |
|
92 |
|
93 void PeFile::Close() |
|
94 { |
|
95 iFile.close(); |
|
96 } |
|
97 |
|
98 |
|
99 PIMAGE_DOS_HEADER PeFile::DosHeader() |
|
100 { |
|
101 if (iDosHeader) |
|
102 return iDosHeader; |
|
103 |
|
104 iDosHeader=new IMAGE_DOS_HEADER; |
|
105 if (!iDosHeader) { |
|
106 this->errno = KErrNoMemory; |
|
107 return NULL; |
|
108 } |
|
109 |
|
110 memset(iDosHeader, 0, sizeof(IMAGE_DOS_HEADER)); |
|
111 |
|
112 iFile.seekg(0); |
|
113 iFile.read((char *)iDosHeader, sizeof(IMAGE_DOS_HEADER)); |
|
114 if (iDosHeader->e_magic!=IMAGE_DOS_SIGNATURE) { |
|
115 this->errno = KErrCorrupt; |
|
116 return NULL; |
|
117 } |
|
118 return iDosHeader; |
|
119 } |
|
120 |
|
121 PIMAGE_NT_HEADERS PeFile::Headers() |
|
122 { |
|
123 if (iHeaders) |
|
124 return iHeaders; |
|
125 PIMAGE_DOS_HEADER d=DosHeader(); |
|
126 if (d==NULL) |
|
127 return NULL; |
|
128 |
|
129 iHeaders=new IMAGE_NT_HEADERS; |
|
130 if (!iHeaders) { |
|
131 this->errno = KErrNoMemory; |
|
132 return NULL; |
|
133 } |
|
134 |
|
135 memset(iHeaders, 0, sizeof(IMAGE_NT_HEADERS)); |
|
136 |
|
137 iFile.seekg(d->e_lfanew); |
|
138 if (iFile.eof()) { |
|
139 this->errno = KErrCorrupt; // File size is too small. |
|
140 return NULL; |
|
141 } |
|
142 |
|
143 iFile.read((char *)iHeaders, sizeof(IMAGE_NT_HEADERS)); |
|
144 return iHeaders; |
|
145 } |
|
146 |
|
147 PIMAGE_SECTION_HEADER PeFile::SectionHeaders() |
|
148 { |
|
149 if (iSectionHeaders) |
|
150 return iSectionHeaders; |
|
151 PIMAGE_NT_HEADERS h=Headers(); |
|
152 if (h==NULL) { |
|
153 this->errno = KErrNoMemory; |
|
154 return NULL; |
|
155 } |
|
156 |
|
157 int numSec; |
|
158 if ((numSec = NumberOfSections()) < 0) { |
|
159 return NULL; |
|
160 } |
|
161 else { |
|
162 iSectionHeaders=new IMAGE_SECTION_HEADER [numSec]; |
|
163 if (!iSectionHeaders) { |
|
164 this->errno = KErrNoMemory; |
|
165 return NULL; |
|
166 } |
|
167 } |
|
168 |
|
169 iFile.seekg(DosHeader()->e_lfanew+sizeof(IMAGE_NT_HEADERS)); |
|
170 int i=sizeof(IMAGE_SECTION_HEADER)*numSec; |
|
171 iFile.read((char *)iSectionHeaders, i); |
|
172 if (iFile.gcount() != i) { // The size of header is incorrect. |
|
173 printf("Error: Cannot load section headers in offset: 0x%x \n", |
|
174 (int) (DosHeader()->e_lfanew + sizeof(IMAGE_NT_HEADERS))); |
|
175 this->errno = KErrCorrupt; |
|
176 return NULL; |
|
177 } |
|
178 |
|
179 return iSectionHeaders; |
|
180 } |
|
181 |
|
182 int PeFile::NumberOfSections() |
|
183 { |
|
184 if (Headers()) |
|
185 return iHeaders->FileHeader.NumberOfSections; |
|
186 else |
|
187 return -1; |
|
188 } |
|
189 |
|
190 char *PeFile::LoadSection(PIMAGE_SECTION_HEADER h) |
|
191 { |
|
192 char *section=new char [h->SizeOfRawData]; |
|
193 if (section==NULL){ |
|
194 this->errno = KErrNoMemory; |
|
195 return NULL; |
|
196 } |
|
197 |
|
198 try |
|
199 { |
|
200 memset(section, 0, h->SizeOfRawData); |
|
201 } |
|
202 catch(...) |
|
203 { |
|
204 } |
|
205 |
|
206 iFile.seekg(h->PointerToRawData); |
|
207 iFile.read(section, h->SizeOfRawData); |
|
208 |
|
209 // Guarantee we have loaded the section correctly. |
|
210 unsigned int actNum = iFile.gcount(); |
|
211 if (actNum != h->SizeOfRawData) { // The size of section is incorrect. |
|
212 printf("Error: Cannot load section in offset: 0x%x \n", |
|
213 (int)h->PointerToRawData); |
|
214 this->errno = KErrCorrupt; |
|
215 return NULL; |
|
216 } |
|
217 |
|
218 return section; |
|
219 } |
|
220 |
|
221 char* PeFile::LoadExtraData(int aOffset, TUint aFileSize) |
|
222 { |
|
223 TUint sizeOfExtraData = 0; |
|
224 if(aFileSize >= (TUint)aOffset) |
|
225 sizeOfExtraData=aFileSize-aOffset; |
|
226 if(sizeOfExtraData>0){ |
|
227 char* buffer=new char [sizeOfExtraData]; |
|
228 if (!buffer) { |
|
229 this->errno = KErrNoMemory; |
|
230 return NULL; |
|
231 } |
|
232 |
|
233 memset(buffer, 0, sizeOfExtraData); |
|
234 iFile.seekg(aOffset); |
|
235 iFile.read(buffer, sizeOfExtraData); // Should be OK if the file size is correct. |
|
236 |
|
237 // Guarantee we have loaded the data correctly. |
|
238 unsigned int actNum = iFile.gcount(); |
|
239 if (actNum != sizeOfExtraData){ // Shouldn't be here is the file size is correct. |
|
240 printf("Error: Cannot load extra section in offset: 0x%x \n", aOffset); |
|
241 this->errno = KErrCorrupt; |
|
242 return NULL; |
|
243 } |
|
244 |
|
245 return buffer; |
|
246 } |
|
247 else { |
|
248 this->errno = KErrCorrupt; |
|
249 return NULL; |
|
250 } |
|
251 |
|
252 } |
|
253 |
|
254 int PeFile::Errno() { |
|
255 return this->errno; |
|
256 } |
|
257 |
|
258 int PeFile::Errno(int aErrno) { |
|
259 return (this->errno = aErrno); |
|
260 } |
|
261 |
|
262 |
|
263 void dump(TUint *aData, TInt aLength) |
|
264 { |
|
265 TUint *p=aData; |
|
266 TInt i=0; |
|
267 char line[256]; |
|
268 char *cp=(char*)aData; |
|
269 TInt j=0; |
|
270 memset(line,' ',sizeof(line)); |
|
271 while (i<aLength) |
|
272 { |
|
273 TInt ccount=0; |
|
274 char* linep=&line[8*9+2]; |
|
275 printf("%06x:", i); |
|
276 while (i<aLength && ccount<8) |
|
277 { |
|
278 printf(" %08x", *p++); |
|
279 i+=4; |
|
280 ccount++; |
|
281 for (j=0; j<4; j++) |
|
282 { |
|
283 char c=*cp++; |
|
284 if (c<32 || c>127) |
|
285 { |
|
286 c = '.'; |
|
287 } |
|
288 *linep++ = c; |
|
289 } |
|
290 } |
|
291 *linep='\0'; |
|
292 printf("%s\n", line+(ccount*9)); |
|
293 } |
|
294 } |
|
295 |
|
296 int pecmp(char *aFileName) |
|
297 { |
|
298 |
|
299 PeFile peFile; |
|
300 if (peFile.Open(aFileName)!=KErrNone) |
|
301 { |
|
302 cout << "Cannot open file '"<<aFileName<<"' for input.\n"; |
|
303 return KErrNotFound; |
|
304 } |
|
305 |
|
306 PIMAGE_DOS_HEADER dosHeader=peFile.DosHeader(); |
|
307 if (dosHeader==NULL) |
|
308 { |
|
309 switch(peFile.Errno()){ |
|
310 case KErrNoMemory: |
|
311 cout << "Error: Out of memory\n"; |
|
312 return KErrNoMemory; |
|
313 default: |
|
314 cout << aFileName << " does not have a valid DOS header.\n"; |
|
315 } |
|
316 return KErrGeneral; |
|
317 } |
|
318 |
|
319 PIMAGE_NT_HEADERS headers=peFile.Headers(); |
|
320 if (headers==NULL) |
|
321 { |
|
322 switch(peFile.Errno()){ |
|
323 case KErrNoMemory: |
|
324 cout << "Error: Out of memory\n"; |
|
325 return KErrNoMemory; |
|
326 default: |
|
327 cout << aFileName << " is too small to be a PE file.\n"; |
|
328 } |
|
329 return KErrGeneral; |
|
330 } |
|
331 |
|
332 headers->FileHeader.TimeDateStamp=0; |
|
333 headers->OptionalHeader.CheckSum = 0; // Fix for TOOLS2 |
|
334 |
|
335 printf("NT Headers:\n"); |
|
336 dump((TUint*)headers, sizeof(IMAGE_NT_HEADERS)); |
|
337 |
|
338 int numberofsections=peFile.NumberOfSections(); |
|
339 if (numberofsections==-1) |
|
340 { |
|
341 cout << "Not a valid PE file.\n"; |
|
342 return KErrGeneral; |
|
343 } |
|
344 |
|
345 PIMAGE_SECTION_HEADER sectionheaders=peFile.SectionHeaders(); |
|
346 |
|
347 printf("Section Headers:\n"); |
|
348 dump((TUint*)sectionheaders, sizeof(IMAGE_SECTION_HEADER)*peFile.NumberOfSections()); |
|
349 |
|
350 TUint exportDirVa=headers->OptionalHeader.DataDirectory[IMAGE_DIRECTORY_ENTRY_EXPORT].VirtualAddress; |
|
351 TUint debugDirVa=headers->OptionalHeader.DataDirectory[IMAGE_DIRECTORY_ENTRY_DEBUG].VirtualAddress; |
|
352 int SizeOfHeaders=headers->OptionalHeader.SizeOfHeaders; |
|
353 int offsetToExtraData=0; |
|
354 int i; |
|
355 for (i=0; i<numberofsections; i++) |
|
356 { |
|
357 PIMAGE_SECTION_HEADER h=§ionheaders[i]; |
|
358 char *section=peFile.LoadSection(h); |
|
359 if (section==NULL) |
|
360 { |
|
361 switch(peFile.Errno()){ |
|
362 case KErrNoMemory: |
|
363 cout << "Error: Out of memory\n"; |
|
364 return KErrNoMemory; |
|
365 default: |
|
366 cout << "Not a valid PE file.\n"; |
|
367 } |
|
368 return KErrGeneral; |
|
369 } |
|
370 char name[9]; |
|
371 for (int j=0; j<9; j++) |
|
372 name[j]=h->Name[j]; |
|
373 name[8]=0; |
|
374 |
|
375 if (debugDirVa>=h->VirtualAddress && debugDirVa<h->VirtualAddress+h->SizeOfRawData) |
|
376 { |
|
377 // Debug data in this section |
|
378 PIMAGE_DEBUG_DIRECTORY dd=(PIMAGE_DEBUG_DIRECTORY)(section+debugDirVa-h->VirtualAddress); |
|
379 TInt debugDirSize=headers->OptionalHeader.DataDirectory[IMAGE_DIRECTORY_ENTRY_DEBUG].Size; |
|
380 while (debugDirSize>0) |
|
381 { |
|
382 dd->TimeDateStamp=0; |
|
383 // size & location in file of debug data is not significant |
|
384 // unless it's also mapped (AddressOfRawData != 0). If that's |
|
385 // true, then the data will be visible in one of the sections |
|
386 // anyway... |
|
387 dd->SizeOfData=0; |
|
388 dd->PointerToRawData=0; |
|
389 dd++; |
|
390 debugDirSize-=sizeof(IMAGE_DEBUG_DIRECTORY); |
|
391 } |
|
392 } |
|
393 if (exportDirVa>=h->VirtualAddress && exportDirVa<h->VirtualAddress+h->SizeOfRawData) |
|
394 { |
|
395 // Export directory in this section |
|
396 PIMAGE_EXPORT_DIRECTORY ed=(PIMAGE_EXPORT_DIRECTORY)(section+exportDirVa-h->VirtualAddress); |
|
397 ed->TimeDateStamp=0; |
|
398 } |
|
399 if (strcmp(".rsrc",name)==0) |
|
400 { |
|
401 int *t=(int *)(section+4); |
|
402 *t=0; |
|
403 } |
|
404 |
|
405 offsetToExtraData=offsetToExtraData+h->SizeOfRawData; |
|
406 printf("Raw data:\n"); |
|
407 dump((TUint*)section, h->SizeOfRawData); |
|
408 |
|
409 delete section; |
|
410 } |
|
411 struct stat buf; |
|
412 stat (aFileName,&buf); |
|
413 TUint fileSize=buf.st_size; |
|
414 offsetToExtraData=offsetToExtraData+SizeOfHeaders; |
|
415 TUint sizeOfExtraData = 0; |
|
416 if(buf.st_size >= offsetToExtraData) |
|
417 sizeOfExtraData=buf.st_size-offsetToExtraData; |
|
418 char* extraData=peFile.LoadExtraData(offsetToExtraData, fileSize); |
|
419 if(sizeOfExtraData>0){ |
|
420 char* nsisSign; |
|
421 nsisSign=extraData+8; |
|
422 if(strncmp (nsisSign,"NullsoftInst",12) == 0){ |
|
423 printf("\n\n Extra Data:\n"); |
|
424 dump((TUint*)extraData, sizeOfExtraData); |
|
425 } |
|
426 } |
|
427 peFile.Close(); |
|
428 return KErrNone; |
|
429 } |
|
430 |
|
431 int main(int argc, char *argv[]) |
|
432 { |
|
433 int r=KErrArgument; |
|
434 if (argc==2) |
|
435 { |
|
436 r=pecmp(argv[1]); |
|
437 } |
|
438 if (r==KErrArgument) |
|
439 { |
|
440 cout << "\nPE_DUMP - PE file dumper V"; |
|
441 cout << MajorVersion << '.' << setfill('0') << setw(2) << MinorVersion; |
|
442 cout << '(' << setw(3) << Build << ")\n"; |
|
443 cout << Copyright; |
|
444 cout << "Syntax: "<<argv[0]<<" pefile\n"; |
|
445 } |
|
446 return r; |
|
447 } |