|
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 "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 errnumber; |
|
66 }; |
|
67 |
|
68 PeFile::PeFile() |
|
69 //: iDosHeader(NULL), iHeaders(NULL), iSectionHeaders(NULL), errno(KErrNone) |
|
70 //commented out as SBSv2 reports error: anachronistic old-style base class initializer |
|
71 { |
|
72 iDosHeader = NULL ; |
|
73 iHeaders = NULL ; |
|
74 iSectionHeaders = NULL ; |
|
75 errnumber = KErrNone; |
|
76 |
|
77 return; |
|
78 } |
|
79 |
|
80 PeFile::~PeFile() |
|
81 { |
|
82 delete iDosHeader; |
|
83 delete iHeaders; |
|
84 delete [] iSectionHeaders; |
|
85 } |
|
86 |
|
87 int PeFile::Open(char *aFileName) |
|
88 { |
|
89 #if defined(__MSVCDOTNET__) || defined(__TOOLS2__) |
|
90 iFile.open(aFileName, ios::in | ios::binary); |
|
91 #else //!__MSVCDOTNET__ |
|
92 iFile.open(aFileName, ios::in | ios::binary | ios::nocreate); |
|
93 #endif //__MSVCDOTNET__ |
|
94 if (!iFile.is_open()) |
|
95 { |
|
96 errnumber = KErrNotFound; |
|
97 return KErrNotFound; |
|
98 } |
|
99 return KErrNone; |
|
100 } |
|
101 |
|
102 void PeFile::Close() |
|
103 { |
|
104 iFile.close(); |
|
105 } |
|
106 |
|
107 |
|
108 PIMAGE_DOS_HEADER PeFile::DosHeader() |
|
109 { |
|
110 if (iDosHeader) |
|
111 return iDosHeader; |
|
112 |
|
113 iDosHeader=new IMAGE_DOS_HEADER; |
|
114 if (!iDosHeader) { |
|
115 errnumber = KErrNoMemory; |
|
116 return NULL; |
|
117 } |
|
118 |
|
119 memset(iDosHeader, 0, sizeof(IMAGE_DOS_HEADER)); |
|
120 |
|
121 iFile.seekg(0); |
|
122 iFile.read((char *)iDosHeader, sizeof(IMAGE_DOS_HEADER)); |
|
123 if (iDosHeader->e_magic!=IMAGE_DOS_SIGNATURE) { |
|
124 errnumber = KErrCorrupt; |
|
125 return NULL; |
|
126 } |
|
127 return iDosHeader; |
|
128 } |
|
129 |
|
130 PIMAGE_NT_HEADERS PeFile::Headers() |
|
131 { |
|
132 if (iHeaders) |
|
133 return iHeaders; |
|
134 PIMAGE_DOS_HEADER d=DosHeader(); |
|
135 if (d==NULL) |
|
136 return NULL; |
|
137 |
|
138 iHeaders=new IMAGE_NT_HEADERS; |
|
139 if (!iHeaders) { |
|
140 errnumber = KErrNoMemory; |
|
141 return NULL; |
|
142 } |
|
143 |
|
144 memset(iHeaders, 0, sizeof(IMAGE_NT_HEADERS)); |
|
145 |
|
146 iFile.seekg(d->e_lfanew); |
|
147 if (iFile.eof()) { |
|
148 errnumber = KErrCorrupt; // File size is too small. |
|
149 return NULL; |
|
150 } |
|
151 |
|
152 iFile.read((char *)iHeaders, sizeof(IMAGE_NT_HEADERS)); |
|
153 return iHeaders; |
|
154 } |
|
155 |
|
156 PIMAGE_SECTION_HEADER PeFile::SectionHeaders() |
|
157 { |
|
158 if (iSectionHeaders) |
|
159 return iSectionHeaders; |
|
160 PIMAGE_NT_HEADERS h=Headers(); |
|
161 if (h==NULL) { |
|
162 errnumber = KErrNoMemory; |
|
163 return NULL; |
|
164 } |
|
165 |
|
166 int numSec; |
|
167 if ((numSec = NumberOfSections()) < 0) { |
|
168 return NULL; |
|
169 } |
|
170 else { |
|
171 iSectionHeaders=new IMAGE_SECTION_HEADER [numSec]; |
|
172 if (!iSectionHeaders) { |
|
173 errnumber = KErrNoMemory; |
|
174 return NULL; |
|
175 } |
|
176 } |
|
177 |
|
178 iFile.seekg(DosHeader()->e_lfanew+sizeof(IMAGE_NT_HEADERS)); |
|
179 int i=sizeof(IMAGE_SECTION_HEADER)*numSec; |
|
180 iFile.read((char *)iSectionHeaders, i); |
|
181 if (iFile.gcount() != i) { // The size of header is incorrect. |
|
182 printf("Error: Cannot load section headers in offset: 0x%x \n", |
|
183 (int) (DosHeader()->e_lfanew + sizeof(IMAGE_NT_HEADERS))); |
|
184 errnumber = KErrCorrupt; |
|
185 return NULL; |
|
186 } |
|
187 |
|
188 return iSectionHeaders; |
|
189 } |
|
190 |
|
191 int PeFile::NumberOfSections() |
|
192 { |
|
193 if (Headers()) |
|
194 return iHeaders->FileHeader.NumberOfSections; |
|
195 else |
|
196 return -1; |
|
197 } |
|
198 |
|
199 char *PeFile::LoadSection(PIMAGE_SECTION_HEADER h) |
|
200 { |
|
201 char *section=new char [h->SizeOfRawData]; |
|
202 if (section==NULL){ |
|
203 errnumber = KErrNoMemory; |
|
204 return NULL; |
|
205 } |
|
206 |
|
207 try |
|
208 { |
|
209 memset(section, 0, h->SizeOfRawData); |
|
210 } |
|
211 catch(...) |
|
212 { |
|
213 } |
|
214 |
|
215 iFile.seekg(h->PointerToRawData); |
|
216 iFile.read(section, h->SizeOfRawData); |
|
217 |
|
218 // Guarantee we have loaded the section correctly. |
|
219 unsigned int actNum = iFile.gcount(); |
|
220 if (actNum != h->SizeOfRawData) { // The size of section is incorrect. |
|
221 printf("Error: Cannot load section in offset: 0x%x \n", |
|
222 (int)h->PointerToRawData); |
|
223 errnumber = KErrCorrupt; |
|
224 return NULL; |
|
225 } |
|
226 |
|
227 return section; |
|
228 } |
|
229 |
|
230 char* PeFile::LoadExtraData(int aOffset, TUint aFileSize) |
|
231 { |
|
232 TUint sizeOfExtraData = 0; |
|
233 if(aFileSize >= (TUint)aOffset) |
|
234 sizeOfExtraData=aFileSize-aOffset; |
|
235 if(sizeOfExtraData>0){ |
|
236 char* buffer=new char [sizeOfExtraData]; |
|
237 if (!buffer) { |
|
238 errnumber = KErrNoMemory; |
|
239 return NULL; |
|
240 } |
|
241 |
|
242 memset(buffer, 0, sizeOfExtraData); |
|
243 iFile.seekg(aOffset); |
|
244 iFile.read(buffer, sizeOfExtraData); // Should be OK if the file size is correct. |
|
245 |
|
246 // Guarantee we have loaded the data correctly. |
|
247 unsigned int actNum = iFile.gcount(); |
|
248 if (actNum != sizeOfExtraData){ // Shouldn't be here is the file size is correct. |
|
249 printf("Error: Cannot load extra section in offset: 0x%x \n", aOffset); |
|
250 errnumber = KErrCorrupt; |
|
251 return NULL; |
|
252 } |
|
253 |
|
254 return buffer; |
|
255 } |
|
256 else { |
|
257 errnumber = KErrCorrupt; |
|
258 return NULL; |
|
259 } |
|
260 |
|
261 } |
|
262 |
|
263 int PeFile::Errno() { |
|
264 return errnumber; |
|
265 } |
|
266 |
|
267 int PeFile::Errno(int aErrno) { |
|
268 return (errnumber = aErrno); |
|
269 } |
|
270 |
|
271 |
|
272 void dump(TUint *aData, TInt aLength) |
|
273 { |
|
274 TUint *p=aData; |
|
275 TInt i=0; |
|
276 char line[256]; |
|
277 unsigned char *cp=(unsigned char*)aData; |
|
278 TInt j=0; |
|
279 memset(line,' ',sizeof(line)); |
|
280 while (i<aLength) |
|
281 { |
|
282 TInt ccount=0; |
|
283 char* linep=&line[8*9+2]; |
|
284 printf("%06x:", i); |
|
285 while (i<aLength && ccount<8) |
|
286 { |
|
287 printf(" %08x", *p++); |
|
288 i+=4; |
|
289 ccount++; |
|
290 for (j=0; j<4; j++) |
|
291 { |
|
292 unsigned char c=*cp++; |
|
293 if (c<32 || c>127) |
|
294 { |
|
295 c = '.'; |
|
296 } |
|
297 *linep++ = c; |
|
298 } |
|
299 } |
|
300 *linep='\0'; |
|
301 printf("%s\n", line+(ccount*9)); |
|
302 } |
|
303 } |
|
304 |
|
305 int pecmp(char *aFileName) |
|
306 { |
|
307 |
|
308 PeFile peFile; |
|
309 if (peFile.Open(aFileName)!=KErrNone) |
|
310 { |
|
311 cout << "Cannot open file '"<<aFileName<<"' for input.\n"; |
|
312 return KErrNotFound; |
|
313 } |
|
314 |
|
315 PIMAGE_DOS_HEADER dosHeader=peFile.DosHeader(); |
|
316 if (dosHeader==NULL) |
|
317 { |
|
318 switch(peFile.Errno()){ |
|
319 case KErrNoMemory: |
|
320 cout << "Error: Out of memory\n"; |
|
321 return KErrNoMemory; |
|
322 default: |
|
323 cout << aFileName << " does not have a valid DOS header.\n"; |
|
324 } |
|
325 return KErrGeneral; |
|
326 } |
|
327 |
|
328 PIMAGE_NT_HEADERS headers=peFile.Headers(); |
|
329 if (headers==NULL) |
|
330 { |
|
331 switch(peFile.Errno()){ |
|
332 case KErrNoMemory: |
|
333 cout << "Error: Out of memory\n"; |
|
334 return KErrNoMemory; |
|
335 default: |
|
336 cout << aFileName << " is too small to be a PE file.\n"; |
|
337 } |
|
338 return KErrGeneral; |
|
339 } |
|
340 |
|
341 headers->FileHeader.TimeDateStamp=0; |
|
342 headers->OptionalHeader.CheckSum = 0; // Fix for TOOLS2 |
|
343 |
|
344 printf("NT Headers:\n"); |
|
345 dump((TUint*)headers, sizeof(IMAGE_NT_HEADERS)); |
|
346 |
|
347 int numberofsections=peFile.NumberOfSections(); |
|
348 if (numberofsections==-1) |
|
349 { |
|
350 cout << "Not a valid PE file.\n"; |
|
351 return KErrGeneral; |
|
352 } |
|
353 |
|
354 PIMAGE_SECTION_HEADER sectionheaders=peFile.SectionHeaders(); |
|
355 |
|
356 printf("Section Headers:\n"); |
|
357 dump((TUint*)sectionheaders, sizeof(IMAGE_SECTION_HEADER)*peFile.NumberOfSections()); |
|
358 |
|
359 TUint exportDirVa=headers->OptionalHeader.DataDirectory[IMAGE_DIRECTORY_ENTRY_EXPORT].VirtualAddress; |
|
360 TUint debugDirVa=headers->OptionalHeader.DataDirectory[IMAGE_DIRECTORY_ENTRY_DEBUG].VirtualAddress; |
|
361 int SizeOfHeaders=headers->OptionalHeader.SizeOfHeaders; |
|
362 int offsetToExtraData=0; |
|
363 int i; |
|
364 for (i=0; i<numberofsections; i++) |
|
365 { |
|
366 PIMAGE_SECTION_HEADER h=§ionheaders[i]; |
|
367 if(h->SizeOfRawData == 0) |
|
368 { |
|
369 continue; |
|
370 } |
|
371 char *section=peFile.LoadSection(h); |
|
372 if (section==NULL) |
|
373 { |
|
374 switch(peFile.Errno()){ |
|
375 case KErrNoMemory: |
|
376 cout << "Error: Out of memory\n"; |
|
377 return KErrNoMemory; |
|
378 default: |
|
379 cout << "Not a valid PE file.\n"; |
|
380 } |
|
381 return KErrGeneral; |
|
382 } |
|
383 char name[9]; |
|
384 for (int j=0; j<9; j++) |
|
385 name[j]=h->Name[j]; |
|
386 name[8]=0; |
|
387 |
|
388 if (debugDirVa>=h->VirtualAddress && debugDirVa<h->VirtualAddress+h->SizeOfRawData) |
|
389 { |
|
390 // Debug data in this section |
|
391 PIMAGE_DEBUG_DIRECTORY dd=(PIMAGE_DEBUG_DIRECTORY)(section+debugDirVa-h->VirtualAddress); |
|
392 TInt debugDirSize=headers->OptionalHeader.DataDirectory[IMAGE_DIRECTORY_ENTRY_DEBUG].Size; |
|
393 while (debugDirSize>0) |
|
394 { |
|
395 dd->TimeDateStamp=0; |
|
396 // size & location in file of debug data is not significant |
|
397 // unless it's also mapped (AddressOfRawData != 0). If that's |
|
398 // true, then the data will be visible in one of the sections |
|
399 // anyway... |
|
400 dd->SizeOfData=0; |
|
401 dd->PointerToRawData=0; |
|
402 dd++; |
|
403 debugDirSize-=sizeof(IMAGE_DEBUG_DIRECTORY); |
|
404 } |
|
405 } |
|
406 if (exportDirVa>=h->VirtualAddress && exportDirVa<h->VirtualAddress+h->SizeOfRawData) |
|
407 { |
|
408 // Export directory in this section |
|
409 PIMAGE_EXPORT_DIRECTORY ed=(PIMAGE_EXPORT_DIRECTORY)(section+exportDirVa-h->VirtualAddress); |
|
410 ed->TimeDateStamp=0; |
|
411 } |
|
412 if (strcmp(".rsrc",name)==0) |
|
413 { |
|
414 int *t=(int *)(section+4); |
|
415 *t=0; |
|
416 } |
|
417 |
|
418 offsetToExtraData=offsetToExtraData+h->SizeOfRawData; |
|
419 printf("Raw data:\n"); |
|
420 dump((TUint*)section, h->SizeOfRawData); |
|
421 |
|
422 delete section; |
|
423 } |
|
424 struct stat buf; |
|
425 stat (aFileName,&buf); |
|
426 TUint fileSize=buf.st_size; |
|
427 offsetToExtraData=offsetToExtraData+SizeOfHeaders; |
|
428 TUint sizeOfExtraData = 0; |
|
429 if(buf.st_size >= offsetToExtraData) |
|
430 sizeOfExtraData=buf.st_size-offsetToExtraData; |
|
431 char* extraData=peFile.LoadExtraData(offsetToExtraData, fileSize); |
|
432 if(sizeOfExtraData>0){ |
|
433 char* nsisSign; |
|
434 nsisSign=extraData+8; |
|
435 if(strncmp (nsisSign,"NullsoftInst",12) == 0){ |
|
436 printf("\n\n Extra Data:\n"); |
|
437 dump((TUint*)extraData, sizeOfExtraData); |
|
438 } |
|
439 } |
|
440 peFile.Close(); |
|
441 return KErrNone; |
|
442 } |
|
443 |
|
444 int main(int argc, char *argv[]) |
|
445 { |
|
446 int r=KErrArgument; |
|
447 if (argc==2) |
|
448 { |
|
449 r=pecmp(argv[1]); |
|
450 } |
|
451 if (r==KErrArgument) |
|
452 { |
|
453 cout << "\nPE_DUMP - PE file dumper V"; |
|
454 cout << MajorVersion << '.' << setfill('0') << setw(2) << MinorVersion; |
|
455 cout << '(' << setw(3) << Build << ")\n"; |
|
456 cout << Copyright; |
|
457 cout << "Syntax: "<<argv[0]<<" pefile\n"; |
|
458 } |
|
459 return r; |
|
460 } |