imgtools/imgcheck/libimgutils/src/utils.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 
       
    25 #include "typedefs.h"
       
    26 #include <e32def.h>
       
    27 #include "h_utl.h"
       
    28 #include "r_obey.h"
       
    29 #include "r_romnode.h"
       
    30 #include <algorithm>
       
    31 
       
    32 ECompression gCompress = ECompressionUnknown;
       
    33 unsigned int  gCompressionMethod = 0;
       
    34 TBool gPagingOverride = 0;
       
    35 TInt gCodePagingOverride = -1;
       
    36 TInt gDataPagingOverride = -1;
       
    37 TInt gLogLevel = 0;
       
    38 
       
    39 /** 
       
    40 Function receives an UID type of an executable and identifies whether it is a
       
    41 1. EXE or not,
       
    42 2. DLL or not
       
    43 3. Executable or not.
       
    44 
       
    45 @internalComponent
       
    46 @released
       
    47 
       
    48 @param Uids1 - Uid1 of a E32 executable
       
    49 @param aType - Type to be compared against aUids1. 
       
    50 */
       
    51 bool ReaderUtil::IsExecutable(unsigned char* aUids1, int aType)
       
    52 {
       
    53 	//In the little-endian world
       
    54 	if( aUids1[3] == 0x10 && aUids1[2] == 0x0 && aUids1[1] == 0x0 )
       
    55 		{
       
    56 			switch(aType)
       
    57 			{
       
    58 			case EExe:
       
    59 				if(aUids1[0] == 0x7a)
       
    60 				{
       
    61 					return true;
       
    62 				}
       
    63 				break;
       
    64 			case EDll:
       
    65 				if(aUids1[0] == 0x79)
       
    66 				{
       
    67 					return true;
       
    68 				}
       
    69 				break;
       
    70 			case EAll:
       
    71 				if((aUids1[0] == 0x79) || (aUids1[0] == 0x7a))
       
    72 				{
       
    73 					return true;
       
    74 				}
       
    75 				break;
       
    76 			}
       
    77 		}
       
    78 	return false;
       
    79 }
       
    80 
       
    81 /** 
       
    82 Function receives an UID type of an executable and identifies whether it is a EXE or not.
       
    83 
       
    84 @internalComponent
       
    85 @released
       
    86 
       
    87 @param aType - Type to be compared against aUids1.
       
    88 */
       
    89 bool ReaderUtil::IsExe(unsigned long* aUids1)
       
    90 {
       
    91 	return IsExecutable((unsigned char*)aUids1, EExe);
       
    92 }
       
    93 
       
    94 /** 
       
    95 Function receives an UID type of an executable and identifies whether it is a DLL or not,
       
    96 
       
    97 @internalComponent
       
    98 @released
       
    99 
       
   100 @param aType - Type to be compared against aUids1.
       
   101 */
       
   102 bool ReaderUtil::IsDll(unsigned long* aUids1)
       
   103 {
       
   104 	return IsExecutable((unsigned char*)aUids1, EDll);
       
   105 }
       
   106 
       
   107 /** 
       
   108 Function responsible to convert lower case strings to upper case
       
   109 
       
   110 @internalComponent
       
   111 @released
       
   112 
       
   113 @param aString - String which needs to be inserted
       
   114 */
       
   115 const String&  ReaderUtil::ToLower(String& aString)
       
   116 {
       
   117 	unsigned int stringLength = aString.length();
       
   118 	unsigned char stringChar;
       
   119 	for(unsigned int stringIndex = 0; stringIndex < stringLength; stringIndex++)
       
   120 	{
       
   121 		stringChar = aString.at(stringIndex);
       
   122 		if( stringChar >= KUpperCaseAsciiValOfCharA && stringChar <= KUpperCaseAsciiValOfCharZ )
       
   123 		{
       
   124 			stringChar += KUpperAndLowerAsciiDiff; //Upper case alphabets case changed to lower case
       
   125 		}
       
   126 		aString[stringIndex] = stringChar;
       
   127 	}
       
   128 	return aString;
       
   129 }
       
   130 
       
   131 /** 
       
   132 Function responsible to convert integer to ASCII characters with respect to its base value.
       
   133 Function takes the integer value with its base.
       
   134 Calculates the first reminder by dividing the value with its base, put this value into result string .
       
   135 Do the same until the value becomes zero.
       
   136 
       
   137 Regular itoa() function from stdlib.h, definition is not available in linux.
       
   138 
       
   139 @internalComponent
       
   140 @released
       
   141 
       
   142 @param aString - String which needs to be inserted
       
   143 */
       
   144 const String ReaderUtil::IntToAscii(const int aValue, const int aBase)
       
   145 {
       
   146 	String result;
       
   147 	// check that the base if valid, the valid range is between 2 and 16
       
   148 	if (aBase < EBase2 || aBase > EBase16) 
       
   149 	{ 
       
   150 		return result; 
       
   151 	}
       
   152 	int quotient = aValue;
       
   153 	do 
       
   154 	{
       
   155 	#ifdef __TOOLS__
       
   156 		result += "0123456789abcdef"[abs(quotient % aBase)];
       
   157 	#else
       
   158 		result += "0123456789abcdef"[std::abs(quotient % aBase)];
       
   159 	#endif
       
   160 		quotient /= aBase;
       
   161 	} while (quotient);
       
   162 	
       
   163 	// Only apply negative sign for base 10
       
   164 	if (aValue < 0 && aBase == EBase10) 
       
   165 	{
       
   166 		result += '-';
       
   167 	}
       
   168 	std::reverse(result.begin(), result.end());
       
   169 	return result;
       
   170 }
       
   171 
       
   172 /** 
       
   173 Function responsible to convert string to integer.
       
   174 Regular atoi() function from stdlib.h, definition is not available in linux.
       
   175 
       
   176 @internalComponent
       
   177 @released
       
   178 
       
   179 @param aString - String which needs to be converted.
       
   180 */
       
   181 Long64 ReaderUtil::DecStrToInt(String& aString)
       
   182 {
       
   183 	Long64 val = 0;
       
   184 	std::string::iterator strBegIter = aString.begin();
       
   185 	std::string::iterator strEndIter = aString.end();
       
   186 
       
   187 	while(strBegIter != strEndIter)
       
   188 	{
       
   189 		val *= EBase10;
       
   190 		val += *strBegIter - KAsciiValueOfZero;
       
   191 		++strBegIter;
       
   192 	}
       
   193 	return val;
       
   194 }
       
   195 
       
   196 /**
       
   197 Function to convert String to any numeric type.
       
   198 
       
   199 @internalComponent
       
   200 @released
       
   201 
       
   202 @param aStringVal - the string which has to be converted.
       
   203 @return - returns the coverted value.
       
   204 */
       
   205 unsigned int ReaderUtil::HexStrToInt(String& aStringVal)
       
   206 {
       
   207 	IStringStream inputStrStream(aStringVal);
       
   208 	unsigned int intVal = 0;
       
   209 	inputStrStream >> std::hex >> intVal;
       
   210 	return intVal;
       
   211 }