e32tools/e32lib/e32image/e32image.cpp
changeset 0 044383f39525
equal deleted inserted replaced
-1:000000000000 0:044383f39525
       
     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 // e32tools/e32image/e32image.cpp
       
    15 // Basic operations on E32Image files which are used by ROMBUILD.
       
    16 // These are independent of the original file format from which the
       
    17 // E32Image file was derived.
       
    18 // 
       
    19 //
       
    20 
       
    21 #include <time.h>
       
    22 #include <malloc.h>
       
    23 #include <string.h>
       
    24 #include <stdlib.h>
       
    25 #include "h_utl.h"
       
    26 
       
    27 
       
    28 #if defined (__MSVCDOTNET__) || defined(__TOOLS2__)
       
    29 #include <fstream>
       
    30 #else //!__MSVCDOTNET__
       
    31 #include <fstream.h>
       
    32 #endif //__MSVCDOTNET__
       
    33 
       
    34 #include <assert.h>
       
    35 #ifndef __LINUX__
       
    36   #include <io.h>
       
    37 #endif
       
    38 #include "h_ver.h"
       
    39 
       
    40 // get E32ImageHeader class...
       
    41 #define INCLUDE_E32IMAGEHEADER_IMPLEMENTATION
       
    42 #define RETURN_FAILURE(_r) return (fprintf(stderr, "line %d\n", __LINE__),_r)
       
    43 //#define E32IMAGEHEADER_TRACE(_t) printf _t
       
    44 #include "e32image.h"
       
    45 #include "byte_pair.h"
       
    46 
       
    47 void DeflateCompress(char* bytes, TInt size, ostream& os);
       
    48 void InflateUnCompress(unsigned char* source, int sourcesize, unsigned char* dest, int destsize);
       
    49 void CompressPages(TUint8 * bytes, TInt size, ostream &os, CBytePair *aBPE);
       
    50 int  DecompressPages(TUint8 * bytes, istream& is, CBytePair *aBPE);
       
    51 
       
    52 // needed by E32ImageHeaderV::ValidateHeader...
       
    53 void Mem::Crc32(TUint32& aCrc, const TAny* aPtr, TInt aLength)
       
    54 	{
       
    55 	HMem::Crc32(aCrc, aPtr, aLength);
       
    56 	}
       
    57 
       
    58 //
       
    59 // E32 Image files
       
    60 //
       
    61 E32ImageFile::E32ImageFile()
       
    62 	: iData(NULL), iSize(0), iOrigHdr(NULL), iHdr(NULL), iFileName(NULL)
       
    63 #ifndef __LINUX__
       
    64 	  , iWideFileName(NULL)
       
    65 #endif
       
    66 , iError(0), iSource(EE32Image), iOrigHdrOffsetAdj(0), iExportBitMap(0)
       
    67 	{}
       
    68 
       
    69 E32ImageFile::~E32ImageFile()
       
    70 	{
       
    71 
       
    72 	free(iData);
       
    73 	delete [] iFileName;
       
    74 #ifndef __LINUX__
       
    75 	delete [] iWideFileName;
       
    76 #endif
       
    77 	if (iHdr && iHdr != iOrigHdr)
       
    78 		delete iHdr;
       
    79 	free(iExportBitMap);
       
    80 	}
       
    81 
       
    82 // dummy implementation
       
    83 TBool E32ImageFile::Translate(const char*, TUint, TBool, TBool)
       
    84 	{
       
    85 	return EFalse;
       
    86 	}
       
    87 
       
    88 Int64 timeToInt64(TInt aTime)
       
    89 	{
       
    90 	aTime-=(30*365*24*60*60+7*24*60*60);	// seconds since midnight Jan 1st, 2000
       
    91 	Int64 daysTo2000AD=730497;
       
    92 	Int64 t=daysTo2000AD*24*3600+aTime;	// seconds since 0000
       
    93 	t=t+3600;								// BST (?)
       
    94 	return t*1000000;						// milliseconds
       
    95 	}
       
    96 
       
    97 class TE32ImageUids : public TCheckedUid
       
    98 	{
       
    99 public:
       
   100 	TE32ImageUids(TUint32 aUid1, TUint32 aUid2, TUint32 aUid3) : TCheckedUid(TUidType(TUid::Uid(aUid1), TUid::Uid(aUid2), TUid::Uid(aUid3))) {}
       
   101 	TUint Check() { return TCheckedUid::Check(); }
       
   102 	};
       
   103 
       
   104 void E32ImageFile::SetDefaultHeader()
       
   105 	{
       
   106 	iHdr = (E32ImageHeaderV*)iOrigHdr;
       
   107 	iHdr->iUid1 = 0;
       
   108 	iHdr->iUid2 = 0;
       
   109 	iHdr->iUid3 = 0;
       
   110 	iHdr->iHeaderCrc = 0;
       
   111 	iHdr->iSignature = 0x434f5045u;
       
   112 	iHdr->iModuleVersion = 0x00010000u;
       
   113 	iHdr->iCompressionType = 0;
       
   114 	iHdr->iToolsVersion = TVersion(MajorVersion, MinorVersion, Build);
       
   115 	Int64 time1(timeToInt64(time(0))); 
       
   116 	iHdr->iTimeLo=(TUint32)time1;
       
   117 	iHdr->iTimeHi=(TUint32)(time1>>32);
       
   118 	iHdr->iFlags = KImageHdrFmt_V;
       
   119 	iHdr->iCodeSize = 0;
       
   120 	iHdr->iDataSize = 0;
       
   121 	iHdr->iHeapSizeMin = 0;
       
   122 	iHdr->iHeapSizeMax = 0;
       
   123 	iHdr->iStackSize = 0;
       
   124 	iHdr->iBssSize = 0;
       
   125 	iHdr->iEntryPoint = 0;
       
   126 	iHdr->iCodeBase = 0;
       
   127 	iHdr->iDataBase = 0;
       
   128 	iHdr->iDllRefTableCount = 0;
       
   129 	iHdr->iExportDirOffset = 0;
       
   130 	iHdr->iExportDirCount = 0;
       
   131 	iHdr->iTextSize = 0;
       
   132 	iHdr->iCodeOffset = 0;
       
   133 	iHdr->iDataOffset = 0;
       
   134 	iHdr->iImportOffset = 0;
       
   135 	iHdr->iCodeRelocOffset = 0;
       
   136 	iHdr->iDataRelocOffset = 0;
       
   137 	iHdr->iProcessPriority = (TUint16)EPriorityForeground;
       
   138 	iHdr->iUncompressedSize = 0;
       
   139 	iHdr->iS.iSecureId = 0;
       
   140 	iHdr->iS.iVendorId = 0;
       
   141 	iHdr->iExceptionDescriptor = 0;
       
   142 	iHdr->iSpare2 = 0;
       
   143 
       
   144 	iHdr->iExportDescSize = 0;
       
   145 	iHdr->iExportDescType = KImageHdr_ExpD_NoHoles;
       
   146 	iHdr->iExportDesc[0] = 0;
       
   147 	}
       
   148 
       
   149 void E32ImageFile::SetCallEntryPoints(TInt aBool)
       
   150 	{
       
   151 
       
   152 	if (aBool)
       
   153 		iHdr->iFlags&=~KImageNoCallEntryPoint;
       
   154 	else
       
   155 		iHdr->iFlags|=KImageNoCallEntryPoint;
       
   156 	}
       
   157 
       
   158 void E32ImageFile::SetFixedAddress(TInt aBool)
       
   159 	{
       
   160 
       
   161 	if (aBool)
       
   162 		iHdr->iFlags|=KImageFixedAddressExe;
       
   163 	else
       
   164 		iHdr->iFlags&=~KImageFixedAddressExe;
       
   165 	}
       
   166 
       
   167 void E32ImageFile::SetPriority(TProcessPriority aPri)
       
   168 	{
       
   169 
       
   170 	iHdr->iProcessPriority = (TUint16)aPri;
       
   171 	}
       
   172 
       
   173 void E32ImageFile::SetCapability(SCapabilitySet& aCapabilities)
       
   174 	{
       
   175 	iHdr->iS.iCaps = aCapabilities;
       
   176 	}
       
   177 
       
   178 void E32ImageFile::SetFPU(unsigned int aFPU)
       
   179 	{
       
   180 	iHdr->iFlags &=~ KImageHWFloatMask;
       
   181 
       
   182 	if (aFPU == 1)
       
   183 		iHdr->iFlags |= KImageHWFloat_VFPv2;
       
   184 	}
       
   185 
       
   186 void E32ImageFile::Adjust(TInt aSize, TBool aAllowShrink)
       
   187 //
       
   188 // Adjust the size of allocated data and fix the member data
       
   189 //
       
   190 	{
       
   191 
       
   192 	TInt asize = ALIGN4(aSize);
       
   193 	if (asize == iSize)
       
   194 		return;
       
   195 	if (iSize == 0)
       
   196 		{
       
   197 		iSize = asize;
       
   198 		iData = (char*)malloc(iSize);
       
   199 		memset(iData, 0, iSize);
       
   200 		}
       
   201 	else if (aAllowShrink || asize > iSize)
       
   202 		{
       
   203 		TInt oldsize = iSize;
       
   204 		iSize = asize;
       
   205 		iData = (char*)realloc(iData, iSize);
       
   206 		if (iSize > oldsize)
       
   207 			memset(iData+oldsize, 0, iSize-oldsize);
       
   208 		}
       
   209 	if (!iData)
       
   210 		iSize = 0;
       
   211 	if (iHdr && iHdr == iOrigHdr)
       
   212 		iHdr = (E32ImageHeaderV*)iData;
       
   213 	iOrigHdr = (E32ImageHeader*)iData;
       
   214 	}
       
   215 
       
   216 TInt E32ImageFile::ReadHeader(ifstream& is)
       
   217 	{
       
   218 	Adjust(sizeof(E32ImageHeader), EFalse);
       
   219 	is.read(iData, sizeof(E32ImageHeader));
       
   220 	TInt hdrsz = iOrigHdr->TotalSize();
       
   221 	if (hdrsz > 0x10000 || hdrsz <= 0)
       
   222 		return KErrCorrupt;	// sanity check
       
   223 	if (hdrsz > (TInt)sizeof(E32ImageHeader))
       
   224 		{
       
   225 		Adjust(hdrsz, EFalse);
       
   226 		is.read(iData+sizeof(E32ImageHeader), hdrsz-sizeof(E32ImageHeader));
       
   227 		}
       
   228 	TUint32 uncompressedSize;
       
   229 	TInt r = iOrigHdr->ValidateHeader(iFileSize,uncompressedSize);
       
   230 	if (r != KErrNone)
       
   231 		{
       
   232 		fprintf(stderr, "Integrity check failed %d\n", r);
       
   233 		return r;
       
   234 		}
       
   235 	iHdr = (E32ImageHeaderV*)iOrigHdr;
       
   236 	return KErrNone;
       
   237 	}
       
   238 
       
   239 void E32ImageFile::SetStackSize(TInt aSize)
       
   240 	{
       
   241 	iHdr->iStackSize=aSize;
       
   242 	}
       
   243 
       
   244 void E32ImageFile::SetHeapSizeMin(TInt aSize)
       
   245 	{
       
   246 	iHdr->iHeapSizeMin=aSize;
       
   247 	}
       
   248 
       
   249 void E32ImageFile::SetHeapSizeMax(TInt aSize)
       
   250 	{
       
   251 	iHdr->iHeapSizeMax=aSize;
       
   252 	}
       
   253 
       
   254 TUint E32ImageFile::TextOffset()
       
   255 //
       
   256 // Return the offset of the text section
       
   257 //
       
   258 	{
       
   259 	return 0;
       
   260 	}
       
   261 
       
   262 TUint E32ImageFile::DataOffset()
       
   263 //
       
   264 // return the offset of the initialised data
       
   265 //
       
   266 	{
       
   267 	return iHdr->iCodeSize;
       
   268 	}
       
   269 
       
   270 TUint E32ImageFile::BssOffset()
       
   271 //
       
   272 // return the offset from the start of code where the bss is linked
       
   273 //
       
   274 	{
       
   275 	return DataOffset()+iHdr->iDataSize;
       
   276 	}
       
   277 
       
   278 
       
   279 TInt E32ImageFile::IsDll()
       
   280 //
       
   281 //
       
   282 //
       
   283 	{
       
   284 	return iHdr->iFlags&KImageDll;
       
   285 	}
       
   286 
       
   287 
       
   288 void E32ImageFile::RelocateSection(char* aPtr, char* aRelocs, TUint aCodeDelta, TUint aDataDelta, char* aImagePtr, TLinAddr** aIATRefs, TBool keepIAT)
       
   289 //
       
   290 // Relocates the section data at aPtr
       
   291 //	
       
   292 	{
       
   293 
       
   294 	TUint codeStart=iHdr->iCodeBase;
       
   295 	TUint codeFinish=codeStart+iHdr->iCodeSize;
       
   296 	TUint iatStart = aIATRefs ? codeStart+iHdr->iTextSize : 0;
       
   297 	TUint iatFinish = aIATRefs ? iatStart+NumberOfImports()*sizeof(TUint) : 0;
       
   298 //	Print(ELog,"IAT: %x->%x\n",iatStart,iatFinish);
       
   299 	char* relocs=aRelocs;
       
   300 	TUint page=0;
       
   301 	TInt size=0;
       
   302 	TInt i=((E32RelocSection *)relocs)->iNumberOfRelocs;
       
   303 	relocs+=sizeof(E32RelocSection);
       
   304 	while (i>0)
       
   305 		{
       
   306 		if (size>0)
       
   307 			{
       
   308 			TUint offset=*(TUint16 *)relocs;
       
   309 			relocs+=2;
       
   310 			if (offset!=0)
       
   311 				{ // its a reloc
       
   312 				TUint va=page+(offset&0x0fff);
       
   313 				TUint relocType=offset&0xf000;
       
   314 				TUint *dataptr=(TUint *)(aPtr+va);
       
   315 				assert((char *)dataptr < aRelocs);
       
   316 				TUint data=*dataptr;
       
   317 //				Print(ELog,"data %x\n",data);
       
   318 				if (relocType == KTextRelocType) 
       
   319 					*dataptr=data+aCodeDelta; // points to text/rdata section
       
   320 				else if (relocType == KDataRelocType)
       
   321 					*dataptr=data+aDataDelta;
       
   322 				else 
       
   323 					{
       
   324 					if (relocType != KInferredRelocType)
       
   325 						Print(EError,"Unrecognized relocation type %x\n",relocType);
       
   326 
       
   327 					if (data>=iatStart && data<iatFinish)
       
   328 						{
       
   329 
       
   330 						TUint iatNum = (data-iatStart)/sizeof(TLinAddr);
       
   331 
       
   332 						// If "keepIAT" is used then the importing instruction must import through the IAT entry,
       
   333 						// but otherwise we change the IAT entry to point to the bit of code doing the importing
       
   334 						// and do the real fix-up later on in TRomBuilderEntry::FixupImports.
       
   335 						// NB: We always want to do this for X86 or data exports dont work.
       
   336 						if (keepIAT || (iHdr->iCpuIdentifier & 0x1000) /*denotes X86*/) 
       
   337 							*dataptr=data+aCodeDelta; 
       
   338 						else 
       
   339 							{
       
   340 							if ((TUint)aIATRefs[iatNum]>65535)
       
   341 								Print(EWarning, "Multiple relocations for IAT entry %d (0x%x, 0x%x)\n",
       
   342 										iatNum, aIATRefs[iatNum], dataptr);
       
   343 							else
       
   344 								aIATRefs[iatNum] = (TLinAddr*)(aImagePtr+va);	// ROM image address of importing pointer					
       
   345 							}
       
   346 						}
       
   347 					else if (data>=codeStart && data<codeFinish)
       
   348 						*dataptr=data+aCodeDelta; // points to text/rdata section
       
   349 					else
       
   350 						*dataptr=data+aDataDelta; // points to data section
       
   351 					}
       
   352 				--i;
       
   353 				}
       
   354 			size-=2;
       
   355 			}
       
   356 		else
       
   357 			{ // next page of relocs
       
   358 			page=*(TUint *)relocs;
       
   359 			relocs+=4;
       
   360 			size=*(TUint *)relocs;
       
   361 			relocs+=4;
       
   362 			size-=8;
       
   363 			}
       
   364 		}
       
   365 	}
       
   366 
       
   367 void E32ImageFile::SetUids(TUid aUid1, TUid aUid2, TUid aUid3)
       
   368 	{
       
   369 	iHdr->iUid1=aUid1.iUid;
       
   370 	iHdr->iUid2=aUid2.iUid;
       
   371 	iHdr->iUid3=aUid3.iUid;
       
   372 	}
       
   373 
       
   374 void E32ImageFile::SetSecureId(TUint32 aId)
       
   375 	{
       
   376 	((E32ImageHeaderV*)iHdr)->iS.iSecureId = aId;
       
   377 	}
       
   378 
       
   379 void E32ImageFile::SetVendorId(TUint32 aId)
       
   380 	{
       
   381 	((E32ImageHeaderV*)iHdr)->iS.iVendorId = aId;
       
   382 	}
       
   383 
       
   384 void E32ImageFile::UpdateHeaderCrc()
       
   385 	{
       
   386 	TE32ImageUids u(iHdr->iUid1, iHdr->iUid2, iHdr->iUid3);
       
   387 	iHdr->iUidChecksum = u.Check();
       
   388 	TInt hdrsz = iHdr->TotalSize();
       
   389 	TInt orighdrsz = iOrigHdr->TotalSize();
       
   390 	iHdr->iUncompressedSize = iSize - orighdrsz;
       
   391 	iHdr->iHeaderCrc = KImageCrcInitialiser;
       
   392 	TUint32 crc = 0;
       
   393 	HMem::Crc32(crc, iHdr, hdrsz);
       
   394 	iHdr->iHeaderCrc = crc;
       
   395 	}
       
   396 
       
   397 TInt E32ImageFile::NumberOfImports()
       
   398 //
       
   399 // Return the number of imports made by this image
       
   400 //
       
   401 	{
       
   402 
       
   403 	if (iHdr->iDllRefTableCount==0 || iHdr->iImportOffset==0)
       
   404 		return 0;
       
   405 
       
   406 	TUint impfmt = iHdr->ImportFormat();
       
   407 	const E32ImportSection* isection = (const E32ImportSection*)(iData + iOrigHdr->iImportOffset);
       
   408 	TInt d;
       
   409 	TInt nImports = 0;
       
   410 	const E32ImportBlock* b = (const E32ImportBlock*)(isection+1);
       
   411 	for (d=0; d<iHdr->iDllRefTableCount; d++)
       
   412 		{
       
   413 		nImports += b->iNumberOfImports;
       
   414 		b = b->NextBlock(impfmt);
       
   415 		}
       
   416 
       
   417 	if (impfmt==KImageImpFmt_PE || impfmt==KImageImpFmt_PE2)
       
   418 		{
       
   419 		TUint *imports=(TUint *)(iData + iOrigHdr->iCodeOffset + iHdr->iTextSize);
       
   420 		TInt i=0;
       
   421 		while (*imports++)
       
   422 			i++;
       
   423 		assert(i==nImports);
       
   424 		}
       
   425 	
       
   426 	return nImports;
       
   427 	}
       
   428 
       
   429 // Work out which exports are missing from the export directory
       
   430 void E32ImageFile::CreateExportBitMap()
       
   431 	{
       
   432 	TInt nexp = iOrigHdr->iExportDirCount;
       
   433 	TInt memsz = (nexp + 7) >> 3;
       
   434 	iExportBitMap = (TUint8*)malloc(memsz);
       
   435 	memset(iExportBitMap, 0xff, memsz);
       
   436 	TUint* exports = (TUint*)(iData + iOrigHdr->iExportDirOffset);
       
   437 	TUint absoluteEntryPoint = iOrigHdr->iEntryPoint + iOrigHdr->iCodeBase;
       
   438 	TUint impfmt = iOrigHdr->ImportFormat();
       
   439 	TUint hdrfmt = iOrigHdr->HeaderFormat();
       
   440 	TUint absentVal = (impfmt == KImageImpFmt_ELF) ? absoluteEntryPoint : iOrigHdr->iEntryPoint;
       
   441 	TInt i;
       
   442 	iMissingExports = 0;
       
   443 	for (i=0; i<nexp; ++i)
       
   444 		{
       
   445 		if (exports[i] == absentVal)
       
   446 			{
       
   447 			iExportBitMap[i>>3] &= ~(1u << (i & 7));
       
   448 			++iMissingExports;
       
   449 			}
       
   450 		}
       
   451 	if (hdrfmt < KImageHdrFmt_V && iMissingExports)
       
   452 		{
       
   453 		fprintf(stderr, "Bad exports\n");
       
   454 		exit(999);
       
   455 		}
       
   456 	}
       
   457 
       
   458 // Append an export description to the E32ImageHeader if necessary
       
   459 void E32ImageFile::AddExportDescription()
       
   460 	{
       
   461 	if (iMissingExports == 0)
       
   462 		return;	// nothing to do
       
   463 	TInt nexp = iOrigHdr->iExportDirCount;
       
   464 	TInt memsz = (nexp + 7) >> 3;	// size of complete bitmap
       
   465 	TInt mbs = (memsz + 7) >> 3;	// size of meta-bitmap
       
   466 	TInt nbytes = 0;
       
   467 	TInt i;
       
   468 	for (i=0; i<memsz; ++i)
       
   469 		if (iExportBitMap[i] != 0xff)
       
   470 			++nbytes;				// number of groups of 8
       
   471 	TUint8 edt = KImageHdr_ExpD_FullBitmap;
       
   472 	TInt extra_space = memsz - 1;
       
   473 	if (mbs + nbytes < memsz)
       
   474 		{
       
   475 		edt = KImageHdr_ExpD_SparseBitmap8;
       
   476 		extra_space = mbs + nbytes - 1;
       
   477 		}
       
   478 	extra_space = (extra_space + sizeof(TUint) - 1) &~ (sizeof(TUint) - 1);
       
   479 	TInt hdrsz = sizeof(E32ImageHeaderV) + extra_space;
       
   480 	iHdr = (E32ImageHeaderV*)malloc(hdrsz);
       
   481 	memcpy(iHdr, iOrigHdr, sizeof(E32ImageHeaderV));
       
   482 	iHdr->iExportDescType = edt;
       
   483 	if (edt == KImageHdr_ExpD_FullBitmap)
       
   484 		{
       
   485 		iHdr->iExportDescSize = (TUint16)memsz;
       
   486 		memcpy(iHdr->iExportDesc, iExportBitMap, memsz);
       
   487 		}
       
   488 	else
       
   489 		{
       
   490 		iHdr->iExportDescSize = (TUint16)(mbs + nbytes);
       
   491 		memset(iHdr->iExportDesc, 0, extra_space + 1);
       
   492 		TUint8* mptr = iHdr->iExportDesc;
       
   493 		TUint8* gptr = mptr + mbs;
       
   494 		for (i=0; i<memsz; ++i)
       
   495 			{
       
   496 			if (iExportBitMap[i] != 0xff)
       
   497 				{
       
   498 				mptr[i>>3] |= (1u << (i&7));
       
   499 				*gptr++ = iExportBitMap[i];
       
   500 				}
       
   501 			}
       
   502 		}
       
   503 	iHdr->iCodeOffset += extra_space;
       
   504 	if (iHdr->iDataOffset)
       
   505 		iHdr->iDataOffset += extra_space;
       
   506 	if (iHdr->iCodeRelocOffset)
       
   507 		iHdr->iCodeRelocOffset += extra_space;
       
   508 	if (iHdr->iDataRelocOffset)
       
   509 		iHdr->iDataRelocOffset += extra_space;
       
   510 	if (iHdr->iImportOffset)
       
   511 		iHdr->iImportOffset += extra_space;
       
   512 	if (iHdr->iExportDirOffset)
       
   513 		iHdr->iExportDirOffset += extra_space;
       
   514 	}
       
   515 
       
   516 // Check the export description is consistent with the export directory
       
   517 TInt E32ImageFile::CheckExportDescription()
       
   518 	{
       
   519 	TUint hdrfmt = iOrigHdr->HeaderFormat();
       
   520 	if (hdrfmt < KImageHdrFmt_V && iMissingExports)
       
   521 		return KErrCorrupt;
       
   522 	if (iHdr->iExportDescType == KImageHdr_ExpD_NoHoles)
       
   523 		{
       
   524 		return iMissingExports ? KErrCorrupt : KErrNone;
       
   525 		}
       
   526 	TInt nexp = iOrigHdr->iExportDirCount;
       
   527 	TInt memsz = (nexp + 7) >> 3;	// size of complete bitmap
       
   528 	TInt mbs = (memsz + 7) >> 3;	// size of meta-bitmap
       
   529 	TInt eds = iHdr->iExportDescSize;
       
   530 	if (iHdr->iExportDescType == KImageHdr_ExpD_FullBitmap)
       
   531 		{
       
   532 		if (eds != memsz)
       
   533 			return KErrCorrupt;
       
   534 		if (memcmp(iHdr->iExportDesc, iExportBitMap, eds) == 0)
       
   535 			return KErrNone;
       
   536 		return KErrCorrupt;
       
   537 		}
       
   538 	if (iHdr->iExportDescType != KImageHdr_ExpD_SparseBitmap8)
       
   539 		return KErrNotSupported;
       
   540 	TInt nbytes = 0;
       
   541 	TInt i;
       
   542 	for (i=0; i<memsz; ++i)
       
   543 		if (iExportBitMap[i] != 0xff)
       
   544 			++nbytes;				// number of groups of 8
       
   545 	TInt exp_extra = mbs + nbytes;
       
   546 	if (eds != exp_extra)
       
   547 		return KErrCorrupt;
       
   548 	const TUint8* mptr = iHdr->iExportDesc;
       
   549 	const TUint8* gptr = mptr + mbs;
       
   550 	for (i=0; i<memsz; ++i)
       
   551 		{
       
   552 		TUint mbit = mptr[i>>3] & (1u << (i&7));
       
   553 		if (iExportBitMap[i] != 0xff)
       
   554 			{
       
   555 			if (!mbit || *gptr++ != iExportBitMap[i])
       
   556 				return KErrCorrupt;
       
   557 			}
       
   558 		else if (mbit)
       
   559 			return KErrCorrupt;
       
   560 		}
       
   561 	return KErrNone;
       
   562 	}
       
   563 
       
   564 
       
   565 TInt E32ImageFile::Validate()
       
   566 	{
       
   567 	TInt orighdrsz = iOrigHdr->TotalSize();
       
   568 	TInt r = iHdr->ValidateWholeImage(iData+orighdrsz,iSize-orighdrsz);
       
   569 	if(r!=KErrNone)
       
   570 		return r;
       
   571 	return r;
       
   572 	}
       
   573 
       
   574 
       
   575 ostream& operator<<(ostream& os, const E32ImageFile& aImage)
       
   576 //
       
   577 // Output an E32ImageFile
       
   578 //
       
   579 	{
       
   580             CBytePair bpe(EFalse);
       
   581 	E32ImageHeaderV* h = aImage.iHdr;
       
   582 	TUint hdrfmt = h->HeaderFormat();
       
   583 	if (hdrfmt != KImageHdrFmt_V)
       
   584 		return os;	// don't generate old binary formats
       
   585 	TInt hdrsz = h->TotalSize();
       
   586 	TInt orighdrsz = aImage.iOrigHdr->TotalSize();
       
   587 
       
   588 	os.write((const char*)aImage.iHdr, hdrsz);
       
   589 
       
   590 	TUint compression = h->CompressionType();
       
   591 	if (compression == KUidCompressionDeflate)
       
   592 		{
       
   593 		int srcsize = aImage.iSize - orighdrsz;
       
   594 		DeflateCompress(aImage.iData + orighdrsz, srcsize, os);
       
   595 		}
       
   596 	else if (compression == KUidCompressionBytePair)
       
   597 		{
       
   598 		// Compress and write out code part
       
   599 		int srcStart = orighdrsz;
       
   600 		CompressPages( (TUint8*)aImage.iData + srcStart, aImage.iOrigHdr->iCodeSize, os, &bpe);
       
   601 		
       
   602 		
       
   603 		// Compress and write out data part
       
   604 		srcStart += aImage.iOrigHdr->iCodeSize;
       
   605 		int srcLen = aImage.iSize - srcStart;
       
   606 		//Print(EWarning," Data part start:0x%08x, len:%d (0x%08x)\n", srcStart, aImage.iOrigHdr->iDataSize, aImage.iOrigHdr->iDataSize);
       
   607 		//CompressPages((TUint8*)aImage.iData + srcStart, aImage.iOrigHdr->iDataSize, os);		
       
   608 
       
   609 		CompressPages((TUint8*)aImage.iData + srcStart, srcLen, os, &bpe);		
       
   610 
       
   611 		}
       
   612 	else if (compression == KFormatNotCompressed)
       
   613 		{
       
   614 		int srcsize = aImage.iSize - orighdrsz;
       
   615 		os.write(aImage.iData + orighdrsz, srcsize); // image not to be compressed
       
   616 		}
       
   617 	return os;
       
   618 	}
       
   619 
       
   620 ifstream& operator>>(ifstream& is, E32ImageFile& aImage)
       
   621 //
       
   622 // Input an E32ImageFile
       
   623 //
       
   624 	{
       
   625             CBytePair bpe(EFalse);
       
   626 	aImage.iError = aImage.ReadHeader(is);
       
   627 	if (aImage.iError != KErrNone)
       
   628 		return is;
       
   629 	E32ImageHeader* oh = aImage.iOrigHdr;
       
   630 	TInt orighdrsz = oh->TotalSize();
       
   631 	int remainder = aImage.iSize - orighdrsz;
       
   632 	TUint compression = oh->CompressionType();
       
   633 	if (compression == 0)
       
   634 		{
       
   635 		is.read(aImage.iData + orighdrsz, remainder);
       
   636 		}
       
   637 	else if (compression == KUidCompressionDeflate)
       
   638 		{ //Uncompress
       
   639 		aImage.iError = KErrNoMemory;
       
   640 		unsigned int uncompsize = ((E32ImageHeaderComp*)aImage.iOrigHdr)->iUncompressedSize;
       
   641 		aImage.Adjust(uncompsize + orighdrsz);
       
   642 		if (aImage.iData==NULL)
       
   643 			return is;
       
   644 		oh = aImage.iOrigHdr;
       
   645 		unsigned char* compressedData = new unsigned char[remainder];
       
   646 		if (compressedData==NULL)
       
   647 			return is;
       
   648 		is.read(reinterpret_cast<char *>(compressedData), remainder);
       
   649 		unsigned int destsize = uncompsize;
       
   650 		InflateUnCompress( compressedData, remainder, (unsigned char*)(aImage.iData + orighdrsz), destsize);
       
   651 		if (destsize != uncompsize)
       
   652 			Print(EWarning, "Inconsistent sizes discovered during uncompression.\n");
       
   653 		delete [] compressedData;
       
   654 		if ((TUint)orighdrsz > oh->iCodeOffset)
       
   655 			{
       
   656 			// need to adjust code offsets in original
       
   657 			aImage.iOrigHdrOffsetAdj = (TUint)orighdrsz - oh->iCodeOffset;
       
   658 			aImage.OffsetAdjust(oh->iCodeOffset);
       
   659 			aImage.OffsetAdjust(oh->iDataOffset);
       
   660 			aImage.OffsetAdjust(oh->iCodeRelocOffset);
       
   661 			aImage.OffsetAdjust(oh->iDataRelocOffset);
       
   662 			aImage.OffsetAdjust(oh->iImportOffset);
       
   663 			aImage.OffsetAdjust(oh->iExportDirOffset);
       
   664 			}
       
   665 		aImage.iError = KErrNone;
       
   666 		}
       
   667 	else if(compression == KUidCompressionBytePair)
       
   668 	{ // Uncompress
       
   669 		aImage.iError = KErrNoMemory;
       
   670 		unsigned int uncompsize = ((E32ImageHeaderComp*)aImage.iOrigHdr)->iUncompressedSize;
       
   671 		aImage.Adjust(uncompsize + orighdrsz);
       
   672 		if (aImage.iData==NULL)
       
   673 			return is;
       
   674 		oh = aImage.iOrigHdr;
       
   675 
       
   676 		// Read and decompress code part of the image
       
   677 
       
   678 		unsigned int uncompressedCodeSize = DecompressPages((TUint8 *) (aImage.iData + orighdrsz), is, &bpe);
       
   679 
       
   680 		
       
   681 		// Read and decompress data part of the image
       
   682 
       
   683 		unsigned int uncompressedDataSize = DecompressPages((TUint8 *) (aImage.iData + orighdrsz + uncompressedCodeSize), is, &bpe);
       
   684 
       
   685 		if (uncompressedCodeSize + uncompressedDataSize != uncompsize)
       
   686 			Print(EWarning, "Inconsistent sizes discovered during uncompression.\n");
       
   687 
       
   688 		if ((TUint)orighdrsz > oh->iCodeOffset)
       
   689 			{
       
   690 			// need to adjust code offsets in original
       
   691 			aImage.iOrigHdrOffsetAdj = (TUint)orighdrsz - oh->iCodeOffset;
       
   692 			aImage.OffsetAdjust(oh->iCodeOffset);
       
   693 			aImage.OffsetAdjust(oh->iDataOffset);
       
   694 			aImage.OffsetAdjust(oh->iCodeRelocOffset);
       
   695 			aImage.OffsetAdjust(oh->iDataRelocOffset);
       
   696 			aImage.OffsetAdjust(oh->iImportOffset);
       
   697 			aImage.OffsetAdjust(oh->iExportDirOffset);
       
   698 			}
       
   699 		aImage.iError = KErrNone;
       
   700 	}
       
   701 	aImage.CreateExportBitMap();
       
   702 	return is;
       
   703 	}
       
   704 
       
   705 TInt E32ImageFile::IsE32ImageFile(char *aFileName)
       
   706 	{
       
   707 
       
   708 #ifdef __LINUX__
       
   709 	E32ImageFile f;
       
   710 	struct stat buf;
       
   711 	if (stat(aFileName, &buf) < 0) 
       
   712 		{
       
   713 		return FALSE;
       
   714 		}
       
   715 	f.iFileSize = buf.st_size;
       
   716 #else
       
   717 	_finddata_t fileinfo;
       
   718 	int ret=_findfirst((char *)aFileName,&fileinfo);
       
   719 	if (ret==-1) 
       
   720 		return FALSE;
       
   721 	E32ImageFile f;
       
   722 	f.iFileSize = fileinfo.size;
       
   723 #endif
       
   724 	ifstream ifile(aFileName, ios::in | ios::binary);
       
   725 	if(!ifile.is_open())
       
   726 		return FALSE;
       
   727 	TInt r = f.ReadHeader(ifile);
       
   728 	ifile.close();
       
   729 	return (r == KErrNone);
       
   730 	}
       
   731 
       
   732 TInt E32ImageFile::IsValid()
       
   733 	{
       
   734 	return (iError == KErrNone);
       
   735 	}
       
   736 
       
   737 TInt E32ImageFile::Open(const char* aFileName)
       
   738 //
       
   739 // Open an E32 Image file
       
   740 //
       
   741 	{
       
   742 #ifdef __LINUX__
       
   743 	struct stat buf;
       
   744 	if (stat(aFileName, &buf) < 0) 
       
   745 		{
       
   746 		Print(EError,"Cannot open %s for input.\n",aFileName);
       
   747 		return 1;
       
   748 		}
       
   749 	iFileSize = buf.st_size;
       
   750 #else
       
   751 	_finddata_t fileinfo;
       
   752 	int ret=_findfirst((char *)aFileName,&fileinfo);
       
   753 	if (ret==-1) 
       
   754 		{
       
   755 		Print(EError,"Cannot open %s for input.\n",aFileName);
       
   756 		return 1;
       
   757 		}
       
   758 	iFileSize = fileinfo.size;
       
   759 #endif
       
   760 	Adjust(iFileSize);
       
   761 	ifstream ifile((char *)aFileName, ios::in | ios::binary);
       
   762 	if(!ifile.is_open())
       
   763 		{
       
   764 		Print(EError,"Cannot open %s for input.\n",aFileName);
       
   765 		return 1;
       
   766 		}
       
   767 	ifile >> *this;
       
   768 	ifile.close();
       
   769 	if (iError != KErrNone)
       
   770 		return iError;
       
   771 	iFileName=strdup((char *)aFileName);
       
   772 	if (iFileName==NULL)
       
   773 		return KErrNoMemory;
       
   774 	return KErrNone;
       
   775 	}
       
   776 
       
   777 #ifndef __LINUX__
       
   778 TInt E32ImageFile::Open(const wchar_t* aFileName)
       
   779 //
       
   780 // Open an E32 Image file
       
   781 //
       
   782 	{
       
   783 	_wfinddata_t fileinfo;
       
   784 	int ret=_wfindfirst(aFileName,&fileinfo);
       
   785 	if (ret==-1) 
       
   786 		{
       
   787 		Print(EError,"Cannot open %ls for input.\n",aFileName);
       
   788 		return 1;
       
   789 		}
       
   790 	iFileSize = fileinfo.size;
       
   791 	Adjust(iFileSize);
       
   792 
       
   793 	FILE* file = _wfopen(aFileName, L"rb");
       
   794 
       
   795 	if(!file)
       
   796 		{
       
   797 		Print(EError,"Cannot open %ls for input.\n",aFileName);
       
   798 		return 1;
       
   799 		}
       
   800 	
       
   801 	#ifdef __TOOLS2__
       
   802 	char *tmp;  				// Convert wide character name to char *, then open a file.
       
   803 	wcstombs(tmp,aFileName,100);
       
   804 	ifstream ifile(tmp, ios::in | ios::binary);
       
   805 	#else
       
   806 	ifstream ifile(fileno(file));
       
   807 	#endif
       
   808 	
       
   809 	if(!ifile.is_open())
       
   810 		{
       
   811 		Print(EError,"Cannot open %ls for input,\n",aFileName);
       
   812 		return 1;
       
   813 		}
       
   814 		
       
   815 	ifile >> *this;
       
   816 	ifile.close();
       
   817 	fclose(file);
       
   818 	if (iError != KErrNone)
       
   819 		return iError;
       
   820 	iWideFileName=wcsdup(aFileName);
       
   821 	if (iWideFileName==NULL)
       
   822 		return KErrNoMemory;
       
   823 	return KErrNone;
       
   824 	}
       
   825 #endif
       
   826 
       
   827 TUint E32ImageFile::VaOfOrdinal(TUint aOrdinal)
       
   828 // return the offset of the exported symbol
       
   829 	{
       
   830 	TUint* exportdir = (TUint*)(iData + iOrigHdr->iExportDirOffset);
       
   831 	return exportdir[aOrdinal-KOrdinalBase];
       
   832 	}
       
   833 
       
   834 // Determine the type of entry point in this module and set the flags
       
   835 // in the E32Image header accordingly.
       
   836 TInt E32ImageFile::DetermineEntryPointType()
       
   837 	{
       
   838 	TUint cpu = iHdr->CpuIdentifier();
       
   839 	if (cpu != ECpuArmV4 && cpu != ECpuArmV5)
       
   840 		return KErrNone;	// if not ARM, leave EPT as 0
       
   841 	TUint epOffset = iHdr->iEntryPoint;
       
   842 	if (epOffset & 3)
       
   843 		return KErrNone;	// if entry point not 4 byte aligned, must be old style
       
   844 	TUint fileOffset = epOffset + iHdr->iCodeOffset;
       
   845 	if (fileOffset+4 > (TUint)iSize)
       
   846 		return KErrCorrupt;	// entry point is past the end of the file??
       
   847 	TInt ept = 0;			// old style if first instruction not recognised
       
   848 	unsigned char* p = (unsigned char*)iData + fileOffset + 4;
       
   849 	TUint32 x = *--p;
       
   850 	x<<=8;
       
   851 	x|=*--p;
       
   852 	x<<=8;
       
   853 	x|=*--p;
       
   854 	x<<=8;
       
   855 	x|=*--p;
       
   856 	if ((x & 0xffffff00) == 0xe31f0000)
       
   857 		{
       
   858 		// starts with tst pc, #n - new entry point
       
   859 		ept = (x & 0xff) + 1;
       
   860 		}
       
   861 	if (ept>7)
       
   862 		return KErrNotSupported;
       
   863 	iHdr->iFlags |= (ept<<KImageEptShift);
       
   864 	return KErrNone;
       
   865 	}
       
   866