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