bintools/petools/pediff/pediff.cpp
changeset 600 6d08f4a05d93
equal deleted inserted replaced
599:fa7a3cc6effd 600:6d08f4a05d93
       
     1 // Copyright (c) 1996-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 "e32image.h"
       
    17 
       
    18 #include "h_ver.h"
       
    19 #include "h_utl.h"
       
    20 #include "pe_defs.h"
       
    21 
       
    22 #ifdef __MSVCDOTNET__
       
    23  #include <fstream>
       
    24  #include <iomanip>
       
    25  #include <string>
       
    26  using namespace std;
       
    27 #else //!__MSVCDOTNET__
       
    28  #include <fstream.h>
       
    29  #include <iomanip.h>
       
    30  #include <string.h>
       
    31 #endif //__MSVCDOTNET__
       
    32 
       
    33 const int KDiffIdentical=0;
       
    34 const int KDiffDifferent=2;
       
    35 
       
    36 class PeFile
       
    37 	{
       
    38 public:
       
    39 	PeFile();
       
    40 	~PeFile();
       
    41 	int Open(char *aFileName);
       
    42 	void Close();
       
    43 	PIMAGE_DOS_HEADER DosHeader();
       
    44 	PIMAGE_NT_HEADERS Headers();
       
    45 	PIMAGE_SECTION_HEADER SectionHeaders();
       
    46 	char *LoadSection(PIMAGE_SECTION_HEADER aSectionHeader);
       
    47 	int NumberOfSections();
       
    48 public:
       
    49 	fstream iFile;
       
    50 	PIMAGE_DOS_HEADER iDosHeader;
       
    51 	PIMAGE_NT_HEADERS iHeaders;
       
    52 	PIMAGE_SECTION_HEADER iSectionHeaders;
       
    53 	};
       
    54 
       
    55 PeFile::PeFile()
       
    56 	: iDosHeader(NULL), iHeaders(NULL), iSectionHeaders(NULL)
       
    57 	{}
       
    58 
       
    59 PeFile::~PeFile()
       
    60 	{
       
    61 	
       
    62 	delete iDosHeader;
       
    63 	delete iHeaders;
       
    64 	delete [] iSectionHeaders;
       
    65 	}
       
    66 
       
    67 int PeFile::Open(char *aFileName)
       
    68 	{
       
    69 	iFile.open(aFileName, ios::in | ios::binary);
       
    70 	if (!iFile.is_open())
       
    71 		return KErrNotFound;
       
    72 	return KErrNone;
       
    73 	}
       
    74 
       
    75 void PeFile::Close()
       
    76 	{
       
    77 
       
    78 	iFile.close();
       
    79 	}
       
    80 
       
    81 
       
    82 PIMAGE_DOS_HEADER PeFile::DosHeader()
       
    83 	{
       
    84 	if (iDosHeader)
       
    85 		return iDosHeader;
       
    86 	iDosHeader=new IMAGE_DOS_HEADER;
       
    87 	iFile.seekg(0);
       
    88 	iFile.read((char *)iDosHeader, sizeof(IMAGE_DOS_HEADER));
       
    89 	if (iDosHeader->e_magic!=IMAGE_DOS_SIGNATURE)
       
    90 		return NULL;
       
    91 	return iDosHeader;
       
    92 	}
       
    93 
       
    94 PIMAGE_NT_HEADERS PeFile::Headers()
       
    95 	{
       
    96 	if (iHeaders)
       
    97 		return iHeaders;
       
    98 	PIMAGE_DOS_HEADER d=DosHeader();
       
    99 	if (d==NULL)
       
   100 		return NULL;
       
   101 	iHeaders=new IMAGE_NT_HEADERS;
       
   102 	iFile.seekg(d->e_lfanew);
       
   103 	if (iFile.eof())
       
   104 		return NULL;
       
   105 	iFile.read((char *)iHeaders, sizeof(IMAGE_NT_HEADERS));
       
   106 	return iHeaders;
       
   107 	}
       
   108 
       
   109 PIMAGE_SECTION_HEADER PeFile::SectionHeaders()
       
   110 	{
       
   111 	if (iSectionHeaders)
       
   112 		return iSectionHeaders;
       
   113 	PIMAGE_NT_HEADERS h=Headers();
       
   114 	if (h==NULL)
       
   115 		return NULL;
       
   116 	iSectionHeaders=new IMAGE_SECTION_HEADER [NumberOfSections()];
       
   117 	iFile.seekg(DosHeader()->e_lfanew+sizeof(IMAGE_NT_HEADERS));
       
   118 	int i=sizeof(IMAGE_SECTION_HEADER)*NumberOfSections();
       
   119 	iFile.read((char *)iSectionHeaders, i);
       
   120 	return iSectionHeaders;
       
   121 	}
       
   122 
       
   123 int PeFile::NumberOfSections()
       
   124 	{
       
   125 	if (Headers())
       
   126 		return iHeaders->FileHeader.NumberOfSections;
       
   127 	else
       
   128 		return -1;
       
   129 	}
       
   130 
       
   131 char *PeFile::LoadSection(PIMAGE_SECTION_HEADER h)
       
   132 	{
       
   133 	char *section=new char [h->SizeOfRawData];
       
   134 	memset(section, 0, h->SizeOfRawData);
       
   135 	if (section==NULL)
       
   136 		return NULL;
       
   137 	iFile.seekg(h->PointerToRawData);
       
   138 	iFile.read(section, h->SizeOfRawData);
       
   139 	return section;
       
   140 	}
       
   141 
       
   142 int pecmp(char *aFileName1, char *aFileName2)
       
   143 	{
       
   144 
       
   145 	PeFile file1, file2;
       
   146 	if (file1.Open(aFileName1)!=KErrNone)
       
   147 		{
       
   148 		cout << "Cannot open file '"<<aFileName1<<"' for input.\n";
       
   149 		return KErrNotFound;
       
   150 		}
       
   151 	if (file2.Open(aFileName2)!=KErrNone)
       
   152 		{
       
   153 		cout << "Cannot open file '"<<aFileName2<<"' for input.\n";
       
   154 		return KErrNotFound;
       
   155 		}
       
   156 
       
   157 	PIMAGE_DOS_HEADER dosheader1=file1.DosHeader();
       
   158 	if (dosheader1==NULL)
       
   159 		{
       
   160 		cout << aFileName1 << " does not have a valid DOS header.\n";
       
   161 		return KErrGeneral;
       
   162 		}
       
   163 	PIMAGE_DOS_HEADER dosheader2=file2.DosHeader();
       
   164 	if (dosheader2==NULL)
       
   165 		{
       
   166 		cout << aFileName2 << " does not have a valid DOS header.\n";
       
   167 		return KErrGeneral;
       
   168 		}
       
   169 
       
   170 	PIMAGE_NT_HEADERS headers1=file1.Headers();
       
   171 	if (headers1==NULL)
       
   172 		{
       
   173 		cout << aFileName1 << " is too small to be a PE file.\n";
       
   174 		return KErrGeneral;
       
   175 		}
       
   176 	PIMAGE_NT_HEADERS headers2=file2.Headers();
       
   177 	if (headers2==NULL)
       
   178 		{
       
   179 		cout << aFileName2 << " is too small to be a PE file.\n";
       
   180 		return KErrGeneral;
       
   181 		}
       
   182 
       
   183 	int sameTime=(headers1->FileHeader.TimeDateStamp==headers2->FileHeader.TimeDateStamp);
       
   184 	headers1->FileHeader.TimeDateStamp=0;
       
   185 	headers2->FileHeader.TimeDateStamp=0;
       
   186 
       
   187 	int r=memcmp(headers1, headers2, sizeof(IMAGE_NT_HEADERS));
       
   188 	if (r)
       
   189 		{
       
   190 		cout << "PE file headers are different.\n";
       
   191 		return KDiffDifferent;
       
   192 		}
       
   193 
       
   194 	PIMAGE_SECTION_HEADER sectionheaders1=file1.SectionHeaders();
       
   195 	PIMAGE_SECTION_HEADER sectionheaders2=file2.SectionHeaders();
       
   196 	// file one and two have the same number of sections
       
   197 	int numberofsections=file1.NumberOfSections();
       
   198 	if (numberofsections==-1)
       
   199 		{
       
   200 		cout << "Not a valid PE file.\n";
       
   201 		return KErrGeneral;
       
   202 		}
       
   203 	r=memcmp(sectionheaders1, sectionheaders2, sizeof(IMAGE_SECTION_HEADER)*file1.NumberOfSections());
       
   204 	if (r)
       
   205 		{
       
   206 		cout << "The files are different:  PE section headers are different.\n";
       
   207 		return KDiffDifferent;
       
   208 		}
       
   209 
       
   210 	TUint exportDirVa=headers1->OptionalHeader.DataDirectory[IMAGE_DIRECTORY_ENTRY_EXPORT].VirtualAddress;
       
   211 	TUint debugDirVa=headers1->OptionalHeader.DataDirectory[IMAGE_DIRECTORY_ENTRY_DEBUG].VirtualAddress;
       
   212 
       
   213 	int i;
       
   214 	for (i=0; i<numberofsections; i++)
       
   215 		{
       
   216 		PIMAGE_SECTION_HEADER h=&sectionheaders1[i];
       
   217 		char *section1=file1.LoadSection(h);
       
   218 		char *section2=file2.LoadSection(h);
       
   219 		if ((section1==NULL) || (section2==NULL))
       
   220 			{
       
   221 			cout << "Error:  Out of memory\n";
       
   222 			return KErrNoMemory;
       
   223 			}
       
   224 		char name[9];
       
   225 		for (int j=0; j<9; j++)
       
   226 			name[j]=h->Name[j];
       
   227 		name[8]=0;
       
   228 
       
   229 		if (debugDirVa>=h->VirtualAddress && debugDirVa<h->VirtualAddress+h->SizeOfRawData)
       
   230 			{
       
   231 			// Debug data in this section
       
   232 			PIMAGE_DEBUG_DIRECTORY dd1=(PIMAGE_DEBUG_DIRECTORY)(section1+debugDirVa-h->VirtualAddress);
       
   233 			PIMAGE_DEBUG_DIRECTORY dd2=(PIMAGE_DEBUG_DIRECTORY)(section2+debugDirVa-h->VirtualAddress);
       
   234 			TInt debugDirSize=headers1->OptionalHeader.DataDirectory[IMAGE_DIRECTORY_ENTRY_DEBUG].Size;
       
   235 			while (debugDirSize>0)
       
   236 				{
       
   237 				if (sameTime)
       
   238 					sameTime=(dd1->TimeDateStamp==dd2->TimeDateStamp);
       
   239 				dd1->TimeDateStamp=0;
       
   240 				dd2->TimeDateStamp=0;
       
   241 				// size & location in file of debug data is not significant
       
   242 				// unless it's also mapped (AddressOfRawData != 0). If that's
       
   243 				// true, then the data will be visible in one of the sections
       
   244 				// anyway...
       
   245 				dd1->SizeOfData=0;
       
   246 				dd2->SizeOfData=0;
       
   247 				dd1->PointerToRawData=0;
       
   248 				dd2->PointerToRawData=0;
       
   249 				dd1++;
       
   250 				dd2++;
       
   251 				debugDirSize-=sizeof(IMAGE_DEBUG_DIRECTORY);
       
   252 				}
       
   253 			}
       
   254 		if (exportDirVa>=h->VirtualAddress && exportDirVa<h->VirtualAddress+h->SizeOfRawData)
       
   255 			{
       
   256 			// Export directory in this section
       
   257 			PIMAGE_EXPORT_DIRECTORY ed1=(PIMAGE_EXPORT_DIRECTORY)(section1+exportDirVa-h->VirtualAddress);
       
   258 			PIMAGE_EXPORT_DIRECTORY ed2=(PIMAGE_EXPORT_DIRECTORY)(section2+exportDirVa-h->VirtualAddress);
       
   259 			if (sameTime)
       
   260 				sameTime=(ed1->TimeDateStamp==ed2->TimeDateStamp);
       
   261 			ed1->TimeDateStamp=0;
       
   262 			ed2->TimeDateStamp=0;
       
   263 			}
       
   264 		if (strcmp(".rsrc",name)==0)
       
   265 			{
       
   266 			int *t1=(int *)(section1+4);
       
   267 			int *t2=(int *)(section2+4);
       
   268 			if (sameTime)
       
   269 				sameTime=(*t1==*t2);
       
   270 			*t1=0;
       
   271 			*t2=0;
       
   272 			}
       
   273 
       
   274 		r=memcmp(section1, section2, h->SizeOfRawData);
       
   275 		if (r)
       
   276 			{
       
   277 			cout << name << " sections are different.\n";
       
   278 			return KDiffDifferent;
       
   279 			}
       
   280 		delete section1;
       
   281 		delete section2;
       
   282 		}
       
   283 
       
   284 	if (sameTime)
       
   285 		cout << "PE files are identical (time/data stamps also identical).\n";
       
   286 	else
       
   287 		cout << "PE files are identical except for time/date stamps.\n";
       
   288 	file1.Close();
       
   289 	file2.Close();
       
   290 	return KDiffIdentical;
       
   291 	}
       
   292 
       
   293 int e32cmp(char *aFileName1, char *aFileName2)
       
   294 	{
       
   295 	char* f1 = NormaliseFileName(aFileName1);
       
   296 	char* f2 = NormaliseFileName(aFileName2);
       
   297 	
       
   298 	E32ImageFile e32image1;
       
   299 	E32ImageFile e32image2;
       
   300 	TInt r = e32image1.Open(f1);
       
   301 	if (r != KErrNone)
       
   302 		{
       
   303 		if (r<0)
       
   304 			fprintf(stderr, "%s is not a valid E32Image file (error %d)\n", f1, r);
       
   305 		else
       
   306 			r = -1;
       
   307 		return r;
       
   308 		}
       
   309 	r = e32image2.Open(f2);
       
   310 	if (r != KErrNone)
       
   311 		{
       
   312 		if (r<0)
       
   313 			fprintf(stderr, "%s is not a valid E32Image file (error %d)\n", f2, r);
       
   314 		else
       
   315 			r = -1;
       
   316 		return r;
       
   317 		}
       
   318 
       
   319 
       
   320 
       
   321 	int sametime=(e32image1.iHdr->iTimeLo==e32image2.iHdr->iTimeLo)
       
   322 		&&(e32image1.iHdr->iTimeHi==e32image2.iHdr->iTimeHi);
       
   323 	e32image1.iHdr->iTimeLo=0;
       
   324 	e32image1.iHdr->iTimeHi=0;
       
   325 	e32image1.iOrigHdr->iTimeLo=0;
       
   326 	e32image1.iOrigHdr->iTimeHi=0;
       
   327 	e32image2.iHdr->iTimeLo=0;
       
   328 	e32image2.iHdr->iTimeHi=0;
       
   329 	e32image2.iOrigHdr->iTimeLo=0;
       
   330 	e32image2.iOrigHdr->iTimeHi=0;
       
   331 	e32image1.iHdr->iToolsVersion=e32image2.iHdr->iToolsVersion;
       
   332 	e32image1.iOrigHdr->iToolsVersion=e32image2.iOrigHdr->iToolsVersion;
       
   333 	e32image1.iHdr->iHeaderCrc=0;
       
   334 	e32image2.iHdr->iHeaderCrc=0;
       
   335 
       
   336 	int ohs1 = e32image1.iOrigHdr->TotalSize();
       
   337 	int ohs2 = e32image2.iOrigHdr->TotalSize();
       
   338 	int orighdrcmp = (ohs1==ohs2) ? memcmp(e32image1.iOrigHdr, e32image2.iOrigHdr, ohs1) : 1;
       
   339 
       
   340 	int hs1 = e32image1.iHdr->TotalSize();
       
   341 	int hs2 = e32image2.iHdr->TotalSize();
       
   342 	int hdrcmp = (hs1==hs2) ? memcmp(e32image1.iHdr, e32image2.iHdr, hs1) : 1;
       
   343 
       
   344 	int rs1 = e32image1.iSize - ohs1;
       
   345 	int rs2 = e32image2.iSize - ohs2;
       
   346 	int rcmp = (rs1==rs2) ? memcmp(e32image1.iData + ohs1, e32image2.iData + ohs2, rs1) : 1;
       
   347 
       
   348 	if (orighdrcmp==0 && rcmp==0)
       
   349 		{
       
   350 		if (sametime)
       
   351 			printf("E32 image files are identical\n");
       
   352 		else
       
   353 			printf("E32 image files are identical apart from timestamps and Header CRC \n");
       
   354 		return KDiffIdentical;
       
   355 		}
       
   356 	if (hdrcmp==0 && rcmp==0)
       
   357 		{
       
   358 		printf("E32 image files are functionally equivalent but have different headers\n");
       
   359 		return KDiffDifferent;
       
   360 		}
       
   361 	printf("E32 image files are different\n");
       
   362 	return KDiffDifferent;
       
   363 	}
       
   364 
       
   365 
       
   366 int main(int argc, char *argv[])
       
   367 	{
       
   368 	cout << "\nPEDIFF - PE file compare V";
       
   369 	cout << MajorVersion << '.' << setfill('0') << setw(2) << MinorVersion;
       
   370 	cout << '(' << setw(3) << Build << ")\n";
       
   371 	cout << Copyright;
       
   372 	
       
   373 	int r=KErrArgument;
       
   374 	if ((argc==3) || (argc==4))
       
   375 		{
       
   376 		if (argc==3)
       
   377 			r=pecmp(argv[1], argv[2]);
       
   378 		else if (strcmp("-e32", argv[1])==0)
       
   379 			r=e32cmp(argv[2], argv[3]);
       
   380 		}
       
   381 	if (r==KErrArgument)
       
   382 		{
       
   383 		cout << "Syntax: "<<argv[0]<<" pefile pefile\n";
       
   384 		cout << "        "<<argv[0]<<" -e32 e32imagefile e32imagefile\n";
       
   385 		}
       
   386 	return r;
       
   387 	}
       
   388