imgtools/imgcheck/libimgutils/src/rofsreader.cpp
changeset 0 044383f39525
child 590 360bd6b35136
equal deleted inserted replaced
-1:000000000000 0:044383f39525
       
     1 /*
       
     2 * Copyright (c) 2007-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 *
       
    16 */
       
    17 
       
    18 
       
    19 /**
       
    20  @file
       
    21  @internalComponent
       
    22  @released
       
    23 */
       
    24 #include "rofsreader.h"
       
    25 #include "r_romnode.h"
       
    26 
       
    27 
       
    28 /** 
       
    29 Constructor intializes the class pointer members.
       
    30 
       
    31 @internalComponent
       
    32 @released
       
    33 
       
    34 @param aFile - image file name
       
    35 @param aImageType - image type
       
    36 */
       
    37 RofsReader::RofsReader(char* aFile, EImageType aImageType)
       
    38 :ImageReader(aFile), iImageType(aImageType)
       
    39 {
       
    40 	iImageReader = new RCoreImageReader(aFile);
       
    41 	iImage = new RofsImage(iImageReader);
       
    42 	iInputStream.open(aFile, Ios::binary | Ios::in);
       
    43 }
       
    44 
       
    45 /** 
       
    46 Destructor deletes the class pointer members.
       
    47 
       
    48 @internalComponent
       
    49 @released
       
    50 */
       
    51 RofsReader::~RofsReader()
       
    52 {
       
    53 	ExeVsE32ImageMap::iterator e32ImageBegin = iExeVsE32ImageMap.begin();
       
    54     ExeVsE32ImageMap::iterator e32ImageEnd  = iExeVsE32ImageMap.end();
       
    55 	while(e32ImageBegin != e32ImageEnd)
       
    56 	{
       
    57 		DELETE(e32ImageBegin->second);
       
    58 		++e32ImageBegin;
       
    59 	}
       
    60 	iRootDirEntry->Destroy();
       
    61 	iExeVsOffsetMap.clear();
       
    62 	DELETE(iImageReader);
       
    63 	iInputStream.close();
       
    64 	iExeVsE32ImageMap.clear();
       
    65 }
       
    66 
       
    67 /** 
       
    68 Dummy function for compatibility
       
    69 
       
    70 @internalComponent
       
    71 @released
       
    72 */
       
    73 void RofsReader::ReadImage()
       
    74 {
       
    75 }
       
    76 
       
    77 /** 
       
    78 Function responsible to 
       
    79 1. Invoke E32Imagefile process method which will read the header part and identifies the 
       
    80    compression method.
       
    81 2. Prepare executable vs E32Image map, which will be used later to read the E32Image contents.
       
    82 
       
    83 @internalComponent
       
    84 @released
       
    85 */
       
    86 void RofsReader::ProcessImage()
       
    87 {
       
    88 	int retVal = iImage->ProcessImage();
       
    89 	if(retVal != KErrNone)
       
    90 	{
       
    91 		exit(retVal);
       
    92 	}
       
    93 	iRootDirEntry = iImage->RootDirectory();
       
    94 	PrepareExeVsE32ImageMap(iRootDirEntry, iImage, iImageType, iInputStream, iExeVsE32ImageMap, iExeVsOffsetMap, iHiddenExeList);
       
    95 }
       
    96 
       
    97 /** 
       
    98 Function to check whether the node is an executable or not.
       
    99 
       
   100 @internalComponent
       
   101 @released
       
   102 
       
   103 @param aName - Executable name
       
   104 */
       
   105 bool RofsReader::IsExecutable(String aName)
       
   106 {
       
   107 	unsigned int extOffset = aName.find_last_of('.');
       
   108 	if(extOffset != String::npos)
       
   109 	{
       
   110 		aName = aName.substr(extOffset);
       
   111 		if(aName.length() <= 4)
       
   112 		{
       
   113 			ReaderUtil::ToLower(aName);
       
   114 			if (aName.find(".exe") != String::npos || aName.find(".dll") != String::npos || 
       
   115 				aName.find(".prt") != String::npos || aName.find(".nif") != String::npos || 
       
   116 				aName.find(".tsy") != String::npos || aName.find(".pdl") != String::npos || 
       
   117 				aName.find(".csy") != String::npos || aName.find(".agt") != String::npos || 
       
   118 				aName.find(".ani") != String::npos || aName.find(".loc") != String::npos || 
       
   119 				aName.find(".pdd") != String::npos || aName.find(".ldd") != String::npos ||
       
   120 				aName.find(".drv") != String::npos) 
       
   121 			{
       
   122 				return true;
       
   123 			}
       
   124 		}
       
   125 	}
       
   126 	return false;
       
   127 }
       
   128 
       
   129 /** 
       
   130 Function responsible to prepare iExeVsE32ImageMap by traversing the tree recursively.
       
   131 
       
   132 @internalComponent
       
   133 @released
       
   134 
       
   135 @param aEntry - Root directory entry
       
   136 @param aImage - core image
       
   137 @param aImageType - Image type
       
   138 @param aInputStream - Input stream to read image file
       
   139 @param aExeVsE32ImageMap - Container to be filled
       
   140 @param aExeVsOffsetMap - Container to be filled
       
   141 @param aHiddenExeList - Hidden executables filled here.
       
   142 */
       
   143 void RofsReader::PrepareExeVsE32ImageMap(TRomNode* aEntry, CCoreImage *aImage, EImageType aImageType, Ifstream& aInputStream, ExeVsE32ImageMap& aExeVsE32ImageMap, ExeVsOffsetMap& aExeVsOffsetMap, StringList& aHiddenExeList)
       
   144 {
       
   145     String name((char*)aEntry->iName);
       
   146 	bool insideRofs = false;
       
   147     E32Image* e32Image;
       
   148     if(IsExecutable(name))
       
   149     {
       
   150 		iExeAvailable = true;
       
   151 		//V9.1 images has hidden file offset as 0x0
       
   152 		//V9.2 to V9.6 has hidden file offset as 0xFFFFFFFFF
       
   153         if(aEntry->iEntry->iFileOffset != KFileHidden && aEntry->iEntry->iFileOffset != KFileHidden_9_1)
       
   154         {
       
   155             long fileOffset = 0;
       
   156             if(aImageType == ERofsExImage)
       
   157             {
       
   158 				if(aEntry->iEntry->iFileOffset > (long)((RofsImage*)aImage)->iAdjustment)
       
   159 				{
       
   160 	            // File exists in Rofs extension 
       
   161 		            fileOffset = aEntry->iEntry->iFileOffset - ((RofsImage*)aImage)->iAdjustment;
       
   162 				}
       
   163 				else
       
   164 				{
       
   165 					insideRofs = true;
       
   166 				}
       
   167             }
       
   168             else
       
   169             {
       
   170 	            // For rofs files
       
   171 	            fileOffset = aEntry->iEntry->iFileOffset;
       
   172             }
       
   173 	            
       
   174             aInputStream.seekg(fileOffset, Ios::beg);
       
   175             /*
       
   176             Due to the complexities involved in sending the physical file size to E32Reader class, 
       
   177             here we avoided using it for gathering dependencies. Hence class E32ImageFile is used
       
   178             directly.
       
   179             */
       
   180             e32Image = new E32Image();
       
   181             e32Image->iFileSize = aEntry->iSize;
       
   182             e32Image->Adjust(aEntry->iSize); //Initialise the data pointer to the file size
       
   183             aInputStream >> *e32Image; //Input the E32 file to E32ImageFile class
       
   184             aExeVsOffsetMap[ReaderUtil::ToLower(name)] = fileOffset;
       
   185 			if(!insideRofs)
       
   186 			{
       
   187 				aExeVsE32ImageMap.insert(std::make_pair(ReaderUtil::ToLower(name), e32Image));
       
   188 			}
       
   189         }
       
   190         else
       
   191         {
       
   192             aHiddenExeList.push_back(ReaderUtil::ToLower(name));
       
   193         }
       
   194     }
       
   195 
       
   196     if(aEntry->Currentchild())
       
   197     {
       
   198         PrepareExeVsE32ImageMap(aEntry->Currentchild(), aImage, aImageType, aInputStream, aExeVsE32ImageMap, aExeVsOffsetMap, aHiddenExeList);
       
   199     }
       
   200     if(aEntry->Currentsibling())
       
   201     {
       
   202         PrepareExeVsE32ImageMap(aEntry->Currentsibling(), aImage, aImageType, aInputStream, aExeVsE32ImageMap, aExeVsOffsetMap, aHiddenExeList);
       
   203     }
       
   204 }
       
   205 
       
   206 /** 
       
   207 Function responsible to the executable lists using the container iExeVsE32ImageMap.
       
   208 
       
   209 @internalComponent
       
   210 @released
       
   211 */
       
   212 void RofsReader::PrepareExecutableList()
       
   213 {
       
   214     ExeVsE32ImageMap::iterator e32ImageBegin = iExeVsE32ImageMap.begin();
       
   215     ExeVsE32ImageMap::iterator e32ImageEnd  = iExeVsE32ImageMap.end();
       
   216     E32Image* entry;
       
   217     String name;
       
   218     while(e32ImageBegin != e32ImageEnd)
       
   219     {
       
   220         entry = e32ImageBegin->second;
       
   221         name = e32ImageBegin->first;
       
   222         iExecutableList.push_back(name);
       
   223         ++e32ImageBegin;
       
   224     }
       
   225 	DeleteHiddenExecutableVsE32ImageEntry();
       
   226 }
       
   227 
       
   228 /** 
       
   229 Function responsible to delete the hidden executable nodes, in order to
       
   230 avoid the dependency data collection for the same.
       
   231 
       
   232 @internalComponent
       
   233 @released
       
   234 */
       
   235 void RofsReader::DeleteHiddenExecutableVsE32ImageEntry()
       
   236 {
       
   237 	StringList::iterator hExeBegin = iHiddenExeList.begin();
       
   238 	StringList::iterator hExeEnd = iHiddenExeList.end();
       
   239 	ExeVsE32ImageMap::iterator loc;
       
   240 
       
   241 	while(hExeBegin != hExeEnd)
       
   242 	{
       
   243 		//Remove the hidden executable entry from executables vs RomNode Map
       
   244 		loc = iExeVsE32ImageMap.find(*hExeBegin);
       
   245 		if(loc != iExeVsE32ImageMap.end())
       
   246 		{
       
   247 			iExeVsE32ImageMap.erase(loc);
       
   248 		}
       
   249 		++hExeBegin;
       
   250 	}
       
   251 }
       
   252 
       
   253 /** 
       
   254 Function responsible to gather dependencies for all the executables using the container iExeVsE32ImageMap.
       
   255 
       
   256 @internalComponent
       
   257 @released
       
   258 
       
   259 @return iImageVsDepList - returns all executable's dependencies
       
   260 */
       
   261 ExeNamesVsDepListMap& RofsReader::GatherDependencies()
       
   262 {
       
   263 	ExeVsE32ImageMap::iterator begin = iExeVsE32ImageMap.begin();
       
   264 	ExeVsE32ImageMap::iterator end = iExeVsE32ImageMap.end();
       
   265 
       
   266 	StringList executableList;
       
   267 	while(begin != end)
       
   268 	{
       
   269 		PrepareExeDependencyList((*begin).second, executableList);
       
   270 		iImageVsDepList.insert(std::make_pair((*begin).first, executableList));
       
   271 		executableList.clear();
       
   272 		++begin;
       
   273 	}
       
   274 	return iImageVsDepList;
       
   275 }
       
   276 
       
   277 /** 
       
   278 Function responsible to prepare the dependency list.
       
   279 This function can handle ROFS and ROFS extension images.
       
   280 
       
   281 @internalComponent
       
   282 @released
       
   283 
       
   284 @param - aE32Image, Using this, can get all the information about the executable
       
   285 @param - aExecutableList, Excutables placed into this list
       
   286 */
       
   287 void RofsReader::PrepareExeDependencyList(E32Image* aE32Image, StringList& aExecutableList)
       
   288 {
       
   289 	int count = 0;
       
   290 	char** nameList = aE32Image->GetImportExecutableNames(count);
       
   291 	int i = 0;
       
   292 	String dependency;
       
   293 	for(; i < count; ++i)
       
   294 	{
       
   295 		dependency.assign(nameList[i]);
       
   296 		aExecutableList.push_back(ReaderUtil::ToLower(dependency));
       
   297 	}
       
   298 	DELETE(nameList);
       
   299 }
       
   300 
       
   301 /** 
       
   302 Function responsible to say whether it is an ROFS image or not.
       
   303 
       
   304 @internalComponent
       
   305 @released
       
   306 
       
   307 @param - aWord which has the identifier string
       
   308 */
       
   309 bool RofsReader::IsRofsImage(String& aWord)
       
   310 {
       
   311 	if(aWord.find(KRofsImageIdentifier) == 0) //Identifier should start at the beginning
       
   312 	{
       
   313 		return true;
       
   314 	}
       
   315 	return false;
       
   316 }
       
   317 
       
   318 /** 
       
   319 Function responsible to say whether it is an ROFS extension image or not.
       
   320 
       
   321 @internalComponent
       
   322 @released
       
   323 
       
   324 @param - aWord which has the identifier string
       
   325 */
       
   326 bool RofsReader::IsRofsExtImage(String& aWord)
       
   327 {
       
   328 	if(aWord.find(KRofsExtImageIdentifier) == 0) //Identifier should start at the beginning
       
   329 	{
       
   330 		return true;
       
   331 	}
       
   332 	return false;
       
   333 }
       
   334 
       
   335 /** 
       
   336 Function responsible to traverse through the the map using the container iExeVsE32ImageMap to collect 
       
   337 iExeVsIdData.
       
   338 
       
   339 @internalComponent
       
   340 @released
       
   341 */
       
   342 void RofsReader::PrepareExeVsIdMap()
       
   343 {
       
   344     ExeVsE32ImageMap::iterator begin = iExeVsE32ImageMap.begin();
       
   345     ExeVsE32ImageMap::iterator end = iExeVsE32ImageMap.end();
       
   346     String exeName;
       
   347     E32Image* e32Image;
       
   348     IdData* id;
       
   349     if(iExeVsIdData.size() == 0) //Is not already prepared
       
   350     {
       
   351         while(begin != end)
       
   352         {
       
   353             exeName = begin->first;
       
   354             e32Image = begin->second;
       
   355 			id = new IdData;
       
   356 			id->iUid = e32Image->iOrigHdr->iUid1;
       
   357 			id->iDbgFlag = (e32Image->iOrigHdr->iFlags & KImageDebuggable)? true : false;
       
   358             TUint aHeaderFmt = E32ImageHeader::HdrFmtFromFlags(e32Image->iOrigHdr->iFlags);
       
   359 	        if (aHeaderFmt >= KImageHdrFmt_V)
       
   360 	        {
       
   361                 E32ImageHeaderV* v = e32Image->iHdr;
       
   362                 id->iSid = v->iS.iSecureId;
       
   363                 id->iVid = v->iS.iVendorId;
       
   364 	        }
       
   365 			id->iFileOffset = iExeVsOffsetMap[exeName];
       
   366 			iExeVsIdData[exeName] = id;
       
   367             ++begin;
       
   368         }
       
   369     }
       
   370 	id = KNull;
       
   371 }
       
   372 
       
   373 /** 
       
   374 Function responsible to return the Executable versus IdData container. 
       
   375 
       
   376 @internalComponent
       
   377 @released
       
   378 
       
   379 @return - returns iExeVsIdData
       
   380 */
       
   381 const ExeVsIdDataMap& RofsReader::GetExeVsIdMap() const
       
   382 {
       
   383     return iExeVsIdData;
       
   384 }