e32tools/elf2e32/source/h_utl.cpp
changeset 0 044383f39525
equal deleted inserted replaced
-1:000000000000 0:044383f39525
       
     1 // Copyright (c) 2005-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 // Implementation of the e32 image version for e32 image dump for the elf2e32 tool
       
    15 // @internalComponent
       
    16 // @released
       
    17 // 
       
    18 //
       
    19 
       
    20 #include <stdlib.h>
       
    21 #include <stdio.h>
       
    22 #include "h_utl.h"
       
    23 
       
    24 /**
       
    25 Function to check bracketed hex i.e '{', '}'
       
    26 
       
    27 @internalComponent
       
    28 @released
       
    29 
       
    30 @param aTemp
       
    31 @param aBrackets
       
    32 @param aDigits
       
    33 @param aValue
       
    34 
       
    35 @return True if the value passed in is a bracketed hex.
       
    36 */
       
    37 TBool IsBracketedHex(const char* aTemp, const char* aBrackets, TInt aDigits, TUint32& aValue)
       
    38 {
       
    39 	if (aTemp[0]!=aBrackets[0] || aTemp[1+aDigits]!=aBrackets[1])
       
    40 		return 0;
       
    41 	TInt i;
       
    42 	TUint32 x = 0;
       
    43 	for (i=1; i<=aDigits; ++i)
       
    44 	{
       
    45 		TInt c = aTemp[i];
       
    46 		if (c>='a' && c<='z') c-=32;
       
    47 		if (c<'0' || (c>'9' && c<'A') || c>'F')
       
    48 			return 0;
       
    49 		c-='0';
       
    50 		if (c>9)
       
    51 			c-=7;
       
    52 		x = (x<<4) | (TUint32)c;
       
    53 	}
       
    54 	aValue = x;
       
    55 	
       
    56 	return 1;
       
    57 }
       
    58 
       
    59 /**
       
    60 Function to check the decimal version
       
    61 
       
    62 @internalComponent
       
    63 @released
       
    64 
       
    65 @param aBegin
       
    66 Beginning of the version information
       
    67 @param aTemp
       
    68 @param aValue
       
    69 Holds the hexadecimal value
       
    70 @return the checked value.
       
    71 */
       
    72 TInt CheckForDecimalVersion(const char* aBegin, const char* aTemp, TUint32& aValue)
       
    73 {
       
    74 	aValue = 0;
       
    75 	if (aTemp <= aBegin || *aTemp != '}')
       
    76 		return 0;
       
    77 	TUint32 v[2] = {0,0};
       
    78 	TUint32 m = 1;
       
    79 	TInt pos = 0;
       
    80 	const char* s0 = aTemp + 1;
       
    81 	for (--aTemp; aTemp >= aBegin; --aTemp)
       
    82 	{
       
    83 		int c = *aTemp;
       
    84 		if (c >= '0' && c <= '9')
       
    85 		{
       
    86 			v[pos] += m * (c - '0');
       
    87 			if (v[pos] >= 65536u)
       
    88 				return 0;
       
    89 			m *= 10;
       
    90 		}
       
    91 		else if (c == '.')
       
    92 		{
       
    93 			m = 1;
       
    94 			if (++pos >= 2)
       
    95 				return 0;
       
    96 		}
       
    97 		else if (c == '{')
       
    98 			break;
       
    99 		else
       
   100 			return 0;
       
   101 	}
       
   102 	if (aTemp < aBegin)
       
   103 		return 0;
       
   104 	
       
   105 	aValue = (v[1] << 16) | v[0];
       
   106 	
       
   107 	return s0 - aTemp;
       
   108 }
       
   109 
       
   110 /**
       
   111 Function to Parse a filename and convert decimal version number to hex
       
   112 
       
   113 @internalComponent
       
   114 @released
       
   115 
       
   116 @param aName
       
   117 Filename to be parsed
       
   118 
       
   119 @return the converted name wherein the decimal number is converted to hex.
       
   120 */
       
   121 char* NormaliseFileName(const char* aName)
       
   122 {
       
   123 	TFileNameInfo f(aName, 0);
       
   124 	TInt nl = f.iBaseLength;
       
   125 	TInt el = f.iTotalLength - f.iExtPos;
       
   126 	TInt tl = nl + el;
       
   127 	if (f.iFlags & EVerPresent)
       
   128 		tl += 10;
       
   129 	char* t = new char[tl + 1];
       
   130 	if (t)
       
   131 	{
       
   132 		memcpy(t, aName, nl);
       
   133 		if (f.iFlags & EVerPresent)
       
   134 			sprintf(t + nl, "{%08x}%s", (TInt)f.iModuleVersion, aName + f.iExtPos);
       
   135 		else if (el)
       
   136 			memcpy(t + nl, aName + f.iExtPos, el);
       
   137 		t[tl] = 0;
       
   138 	}
       
   139 	
       
   140 	return t;
       
   141 }
       
   142 
       
   143 /**
       
   144 Constructor for Class TFileNameInfo
       
   145 
       
   146 @internalComponent
       
   147 @released
       
   148 
       
   149 @param aFileName
       
   150 Filename to be parsed
       
   151 @param aLookForUid
       
   152 */
       
   153 TFileNameInfo::TFileNameInfo(const char* aFileName, TBool aLookForUid)
       
   154 {
       
   155 	iFileName = aFileName;
       
   156 	TInt l = strlen(aFileName);
       
   157 	iTotalLength = l;
       
   158 	TInt remain = l;
       
   159 	iFlags = 0;
       
   160 	iUid3 = 0;
       
   161 	iModuleVersion = 0;
       
   162 	iBaseLength = l;
       
   163 	iExtPos = l;
       
   164 	const char* s = iFileName + l;
       
   165 	for (; s>=iFileName && *s!='.' && *s!='}' && (!aLookForUid || *s!=']'); --s)
       
   166 	{
       
   167 	}
       
   168 	
       
   169 	if (s<iFileName)
       
   170 		return;
       
   171 	if (*s == '.')
       
   172 	{
       
   173 		iExtPos = s - iFileName;
       
   174 		if (iExtPos == 0)
       
   175 		{
       
   176 			iBaseLength = 0;
       
   177 			return;
       
   178 		}
       
   179 		remain = iExtPos;
       
   180 		--s;
       
   181 	}
       
   182 	else if (s != iFileName + l)
       
   183 		return;
       
   184 	
       
   185 	if (aLookForUid && remain>=10 && IsBracketedHex(s-9, "[]", 8, iUid3))
       
   186 	{
       
   187 		iFlags |= EUidPresent;
       
   188 		remain -= 10;
       
   189 		s -= 10;
       
   190 	}
       
   191 	
       
   192 	if (remain>=10 && IsBracketedHex(s-9, "{}", 8, iModuleVersion))
       
   193 	{
       
   194 		iFlags |= EVerPresent;
       
   195 		remain -= 10;
       
   196 		s -= 10;
       
   197 	}
       
   198 	else
       
   199 	{
       
   200 		TInt n = CheckForDecimalVersion(iFileName, s, iModuleVersion);
       
   201 		if (n>0)
       
   202 		{
       
   203 			iFlags |= EVerPresent;
       
   204 			remain -= n;
       
   205 			s -= n;
       
   206 		}
       
   207 	}
       
   208 	iBaseLength = remain;
       
   209 }
       
   210 
       
   211 /*-------------------------------------------------------------------------
       
   212 String comparison on Linux seems to be a little half-baked.
       
   213 -------------------------------------------------------------------------*/
       
   214 #ifdef __LINUX__
       
   215 
       
   216 int stricmp(const char *a, const char *b)
       
   217 	{
       
   218 	unsigned char ca,cb;
       
   219 
       
   220 	do {
       
   221 		ca = *a++;
       
   222 		cb = *b++;
       
   223 		ca = tolower(ca);
       
   224 		cb = tolower(cb);
       
   225 	}
       
   226 	while((ca == cb) && (ca));
       
   227 	return (int) ca-cb;
       
   228 	}
       
   229 
       
   230 int strnicmp(const char *a, const char *b, int n)
       
   231 	{
       
   232 	unsigned char ca,cb;
       
   233 	int i = 0;
       
   234 
       
   235 	do {
       
   236 		if (++i > n) return 0;
       
   237 		ca = *a++;
       
   238 		cb = *b++;
       
   239 		ca = tolower(ca);
       
   240 		cb = tolower(cb);
       
   241 	}
       
   242 	while((ca == cb) && (ca));
       
   243 	return (int) ca-cb;
       
   244 	}
       
   245 
       
   246 char* strupr(char *a)
       
   247 	{
       
   248 		char *ret = a;
       
   249 
       
   250 		while (*a)
       
   251 			{
       
   252 			*a = toupper(*a);
       
   253 			a++;
       
   254 			}
       
   255 
       
   256 		return ret;
       
   257 	}
       
   258 
       
   259 
       
   260 #endif