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