persistentstorage/dbms/security/SC_TextIn.cpp
changeset 0 08ec8eefde2f
equal deleted inserted replaced
-1:000000000000 0:08ec8eefde2f
       
     1 // Copyright (c) 2004-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 // CPDTextLoader class
       
    15 // 
       
    16 //
       
    17 
       
    18 #define __INCLUDE_CAPABILITY_NAMES__
       
    19 #define __REFERENCE_CAPABILITY_NAMES__
       
    20 #include "e32capability.h"
       
    21 
       
    22 #include "SC_Strings.h"
       
    23 #include "SC_TextIn.h"
       
    24 
       
    25 namespace DBSC
       
    26 {
       
    27 
       
    28 /**
       
    29 Max capability count, when SID or VID used.
       
    30 @internalComponent
       
    31 */
       
    32 const TInt KMaxCapabilityCount1 = 3;
       
    33 
       
    34 /**
       
    35 Max capability count, when no SID and no VID are used.
       
    36 @internalComponent
       
    37 */
       
    38 const TInt KMaxCapabilityCount2 = 7;
       
    39 
       
    40 static TInt CompareCapabilities(const TCapability& aLeft, const TCapability& aRight)
       
    41 	{
       
    42 	return aRight - aLeft;
       
    43 	}
       
    44 
       
    45 /**
       
    46 TStmtProps describes an object representing text file statement type and value
       
    47 (the right side of "=" expression)
       
    48 @internalComponent
       
    49 */
       
    50 struct TStmtProps
       
    51 	{
       
    52 	inline TStmtProps() :
       
    53 		iType(EStmtTEof),
       
    54 		iValue(NULL, 0)
       
    55 		{
       
    56 		}
       
    57 	TStmtType	iType;
       
    58 	TPtrC		iValue;
       
    59 	};
       
    60 
       
    61 /**
       
    62 StmtType2Class() function returns the statement class of the supplied statement type parameter.
       
    63 @param aType Statement type.
       
    64 @return Statement class.
       
    65 @internalComponent
       
    66 */
       
    67 static TStmtClass StmtType2Class(TStmtType aType)
       
    68 	{
       
    69 	switch(aType)
       
    70 		{
       
    71 		case EStmtTComment: 
       
    72 		case EStmtTBlank: 
       
    73 			return EStmtCNoData;
       
    74 		case EStmtTDatabase: 
       
    75 		case EStmtTTable: 
       
    76 			return EStmtCPolicyObj;
       
    77 		case EStmtTRead: 
       
    78 		case EStmtTWrite: 
       
    79 		case EStmtTSchema: 
       
    80 			return EStmtCPolicyType;
       
    81 		case EStmtTCapability:
       
    82 		case EStmtTSID: 
       
    83 		case EStmtTVID: 
       
    84 			return EStmtCPolicyItem;
       
    85 		case EStmtTBackup:
       
    86 			return EStmtCBackup;
       
    87 		default:
       
    88 			break;
       
    89 		}
       
    90 	return EStmtCInvalid;
       
    91 	}
       
    92 
       
    93 /**
       
    94 Capabilities count.
       
    95 @internalComponent
       
    96 */
       
    97 
       
    98 const TInt KCapabilityCount = sizeof(CapabilityNames) / sizeof(CapabilityNames[0]);
       
    99 
       
   100 /**
       
   101 CapabilityName2Id() function searches and returns the related capability ID having
       
   102 the capability name as an input parameter.
       
   103 @param aName Capability name
       
   104 @return Related to aName capability ID
       
   105 @internalComponent
       
   106 */
       
   107 static TCapability CapabilityName2Id(const TDesC& aName)
       
   108 	{
       
   109 	const TInt KMaxCapabilityStringLen = 20;
       
   110 
       
   111 	TBufC8<KMaxCapabilityStringLen> cap;
       
   112 	TPtr8 capPtr (cap.Des());
       
   113 
       
   114 	capPtr.Copy(aName);
       
   115  
       
   116  	for(TInt i=0;i<KCapabilityCount;++i)
       
   117 		{	
       
   118 		if(0 == capPtr.CompareF(TBuf8<20>((TText8*)CapabilityNames[i])))
       
   119 			return (TCapability)i;
       
   120 		}			
       
   121 	return (TCapability) -1; // Return 'None' if no other capabilities are found
       
   122 	}
       
   123 
       
   124 /**
       
   125 Statement keywords, which format is:
       
   126 <Keyword>
       
   127 KStmtKeywordT1 array is in 1:1 relation with KStmtT1 array, except the last KStmtT1 
       
   128 member - EStmtTInvalid - it does not have a match in KStmtKeywordT1 array.
       
   129 @internalComponent
       
   130 */
       
   131 const TDesC* const KStmtKeywordT1[] = 
       
   132 	{
       
   133 	&KDbStr(), &KTblStr(), &KReadStr(), &KWriteStr(), &KSchemaStr(), &KBackupStr()
       
   134 	};
       
   135 
       
   136 /**
       
   137 Statements count, which format is:
       
   138 <Keyword>
       
   139 @internalComponent
       
   140 */
       
   141 const TInt KStmtT1Count = sizeof(KStmtKeywordT1) / sizeof(KStmtKeywordT1[0]);
       
   142 
       
   143 /**
       
   144 Statement IDs, which format is:
       
   145 <Keyword>
       
   146 KStmtKeywordT1 array is in 1:1 relation with KStmtT1 array, except the last KStmtT1 
       
   147 member - EStmtTInvalid - it does not have a match in KStmtKeywordT1 array.
       
   148 "EStmtTInvalid" always has to be the last array element.
       
   149 @internalComponent
       
   150 */
       
   151 const TStmtType KStmtT1[KStmtT1Count + 1] = 
       
   152 	{ 
       
   153 	EStmtTDatabase, EStmtTTable, EStmtTRead, EStmtTWrite, EStmtTSchema, EStmtTBackup, EStmtTInvalid
       
   154 	};
       
   155 
       
   156 /**
       
   157 StmtKeywordT1ToId() function searches and returns the related statement ID having
       
   158 the statement keyword as an input parameter.
       
   159 @param aKeyword Statement keyword
       
   160 @return Related to aKeyword statement ID
       
   161 @internalComponent
       
   162 */
       
   163 static TStmtType StmtKeywordT1ToId(const TDesC& aKeyword)
       
   164 	{
       
   165 	TInt i;
       
   166 	for(i=0;i<KStmtT1Count && aKeyword.CompareF(*(KStmtKeywordT1[i]));++i)
       
   167 		{
       
   168 		}
       
   169 	return KStmtT1[i];
       
   170 	}
       
   171 
       
   172 /**
       
   173 Statement keywords, which format is:
       
   174 <Keyword>=<Data>
       
   175 KStmtKeywordT2 array is in 1:1 relation with KStmtT2 array, except the last KStmtT2 
       
   176 member - EStmtTInvalid - it does not have a match in KStmtKeywordT2 array.
       
   177 @internalComponent
       
   178 */
       
   179 const TDesC* const KStmtKeywordT2[] = 
       
   180 	{
       
   181 	&KNameStr(), &KCapabilityStr(), &KSIDStr(), &KVIDStr()
       
   182 	};
       
   183 
       
   184 /**
       
   185 Statements count, which format is:
       
   186 <Keyword>=<Data>
       
   187 @internalComponent
       
   188 */
       
   189 const TInt KStmtT2Count = sizeof(KStmtKeywordT2) / sizeof(KStmtKeywordT2[0]);
       
   190 
       
   191 /**
       
   192 Statement IDs, which format is:
       
   193 <Keyword>=<Data>
       
   194 KStmtKeywordT2 array is in 1:1 relation with KStmtT2 array, except the last KStmtT2 
       
   195 member - EStmtTInvalid - it does not have a match in KStmtKeywordT2 array.
       
   196 EStmtTInvalid always has to be the last element in KStmtT2 array.
       
   197 @internalComponent
       
   198 */
       
   199 const TStmtType KStmtT2[KStmtT2Count + 1] = 
       
   200 	{ 
       
   201 	EStmtTName, EStmtTCapability, EStmtTSID, EStmtTVID, EStmtTInvalid
       
   202 	};
       
   203 
       
   204 /**
       
   205 StmtKeywordT2ToId() function searches and returns the related statement ID having
       
   206 the statement keyword as an input parameter.
       
   207 @param aKeyword Statement keyword
       
   208 @return Related to aKeyword statement ID
       
   209 @internalComponent
       
   210 */
       
   211 static TStmtType StmtKeywordT2ToId(const TDesC& aKeyword)
       
   212 	{
       
   213 	TInt i;
       
   214 	for(i=0;i<KStmtT2Count && aKeyword.CompareF(*(KStmtKeywordT2[i]));++i)
       
   215 		{
       
   216 		}
       
   217 	return KStmtT2[i];
       
   218 	}
       
   219 
       
   220 /** 
       
   221 TDes::Trim() does not work properly when the descriptor length is 0, 
       
   222 or the descriptor holds a NULL pointer.
       
   223 @internalComponent 
       
   224 */
       
   225 static void Trim(TDes& aDes) 
       
   226 	{
       
   227 	if(aDes.Length() > 0)
       
   228 		{
       
   229 		aDes.Trim();
       
   230 		}
       
   231 	}
       
   232 
       
   233 /**
       
   234 StmtType2PolicyType() function returns the related to aStmtType value - policy type.
       
   235 @param aStmtType Statement type
       
   236 @return The related policy type - R/W/S
       
   237 @internalComponent
       
   238 */
       
   239 static TPolicyType StmtType2PolicyType(TStmtType aStmtType) 
       
   240 	{
       
   241 	switch(aStmtType)
       
   242 		{
       
   243 		case EStmtTRead:
       
   244 			return EPTRead;
       
   245 		case EStmtTWrite:
       
   246 			return EPTWrite;
       
   247 		case EStmtTSchema:
       
   248 			return EPTSchema;
       
   249 		default:
       
   250 			break;
       
   251 		}
       
   252 	return EPTNone;
       
   253 	}
       
   254 
       
   255 /**
       
   256 Creates TSecurityPolicy instance of type 1: SID and a set of up to 3 capabilities.
       
   257 @param aSid Security ID
       
   258 @param aCapabilities Capabilities array.
       
   259 @param aSecurityPolicy Output. Created security policy.
       
   260 @internalComponent
       
   261 */
       
   262 static void CreateSecurityPolicyT1(TSecureId aSid, const RArray<TCapability>& aCapabilities, 
       
   263 									TSecurityPolicy& aSecurityPolicy)
       
   264 	{
       
   265 	TInt count = aCapabilities.Count();
       
   266 	if(count == 0)
       
   267 		{
       
   268 		aSecurityPolicy = TSecurityPolicy(aSid);
       
   269 		}
       
   270 	else if(count == 1)
       
   271 		{
       
   272 		aSecurityPolicy = TSecurityPolicy(aSid, aCapabilities[0]);
       
   273 		}
       
   274 	else if(count == 2)
       
   275 		{
       
   276 		aSecurityPolicy = TSecurityPolicy(aSid, aCapabilities[0], aCapabilities[1]);
       
   277 		}
       
   278 	else if(count == 3)
       
   279 		{
       
   280 		aSecurityPolicy = TSecurityPolicy(aSid, aCapabilities[0], aCapabilities[1], aCapabilities[2]);
       
   281 		}
       
   282 	else
       
   283 		{
       
   284 		User::Invariant();
       
   285 		}
       
   286 	}
       
   287 
       
   288 /**
       
   289 Creates TSecurityPolicy instance of type 2: VID and a set of up to 3 capabilities.
       
   290 @param aVid Vendor ID
       
   291 @param aCapabilities Capabilities array.
       
   292 @param aSecurityPolicy Output. Created security policy.
       
   293 @internalComponent
       
   294 */
       
   295 static void CreateSecurityPolicyT2(TVendorId aVid, const RArray<TCapability>& aCapabilities, 
       
   296 									TSecurityPolicy& aSecurityPolicy)
       
   297 	{
       
   298 	TInt count = aCapabilities.Count();
       
   299 	if(count == 0)
       
   300 		{
       
   301 		aSecurityPolicy = TSecurityPolicy(aVid);
       
   302 		}
       
   303 	else if(count == 1)
       
   304 		{
       
   305 		aSecurityPolicy = TSecurityPolicy(aVid, aCapabilities[0]);
       
   306 		}
       
   307 	else if(count == 2)
       
   308 		{
       
   309 		aSecurityPolicy = TSecurityPolicy(aVid, aCapabilities[0], aCapabilities[1]);
       
   310 		}
       
   311 	else if(count == 3)
       
   312 		{
       
   313 		aSecurityPolicy = TSecurityPolicy(aVid, aCapabilities[0], aCapabilities[1], aCapabilities[2]);
       
   314 		}
       
   315 	else
       
   316 		{
       
   317 		User::Invariant();
       
   318 		}
       
   319 	}
       
   320 
       
   321 /**
       
   322 Creates TSecurityPolicy instance of type 3: A set of up to 7 capabilities.
       
   323 @param aCapabilities Capabilities array.
       
   324 @param aSecurityPolicy Output. Created security policy.
       
   325 @internalComponent
       
   326 */
       
   327 static void CreateSecurityPolicyT3(const RArray<TCapability>& aCapabilities, TSecurityPolicy& aSecurityPolicy)
       
   328 	{
       
   329 	TInt count = aCapabilities.Count();
       
   330 	if(count == 1)
       
   331 		{
       
   332 		aSecurityPolicy = TSecurityPolicy(aCapabilities[0]);
       
   333 		}
       
   334 	else if(count == 2)
       
   335 		{
       
   336 		aSecurityPolicy = TSecurityPolicy(aCapabilities[0], aCapabilities[1]);
       
   337 		}
       
   338 	else if(count == 3)
       
   339 		{
       
   340 		aSecurityPolicy = TSecurityPolicy(aCapabilities[0], aCapabilities[1], aCapabilities[2]);
       
   341 		}
       
   342 	else if(count == 4)
       
   343 		{
       
   344 		aSecurityPolicy = TSecurityPolicy(aCapabilities[0], aCapabilities[1], aCapabilities[2], aCapabilities[3]);
       
   345 		}
       
   346 	else if(count == 5)
       
   347 		{
       
   348 		aSecurityPolicy = TSecurityPolicy(aCapabilities[0], aCapabilities[1], aCapabilities[2], aCapabilities[3], aCapabilities[4]);
       
   349 		}
       
   350 	else if(count == 6)
       
   351 		{
       
   352 		aSecurityPolicy = TSecurityPolicy(aCapabilities[0], aCapabilities[1], aCapabilities[2], aCapabilities[3], aCapabilities[4], aCapabilities[5]);
       
   353 		}
       
   354 	else if(count == 7)
       
   355 		{
       
   356 		aSecurityPolicy = TSecurityPolicy(aCapabilities[0], aCapabilities[1], aCapabilities[2], aCapabilities[3], aCapabilities[4], aCapabilities[5], aCapabilities[6]);
       
   357 		}
       
   358 	else
       
   359 		{
       
   360 		User::Invariant();
       
   361 		}
       
   362 	}
       
   363 
       
   364 /**
       
   365 Creates TSecurityPolicy instance (initializing aSecurityPolicy parameter).
       
   366 @param aSid Security ID
       
   367 @param aVid Vendor ID
       
   368 @param aCapabilities Capabilities array.
       
   369 @leave KErrCorrupt Bad set of SID/VID/Capabilities
       
   370 @internalComponent
       
   371 */
       
   372 static void CreateSecurityPolicyL(TSecureId aSid, TVendorId aVid, 
       
   373 								  const RArray<TCapability>& aCapabilities, 
       
   374 								  TSecurityPolicy& aSecurityPolicy)
       
   375 	{
       
   376 	TInt cababilityCount = aCapabilities.Count();
       
   377 	if(aSid != 0 && aVid != 0)
       
   378 		{
       
   379 		__LEAVE(KErrCorrupt);
       
   380 		}
       
   381 	if(aSid != 0 || aVid != 0)
       
   382 		{
       
   383 		if(cababilityCount > KMaxCapabilityCount1)
       
   384 			{
       
   385 			__LEAVE(KErrCorrupt);
       
   386 			}
       
   387 		if(aSid != 0)
       
   388 			{
       
   389 			DBSC::CreateSecurityPolicyT1(aSid, aCapabilities, aSecurityPolicy);
       
   390 			}
       
   391 		else
       
   392 			{
       
   393 			DBSC::CreateSecurityPolicyT2(aVid, aCapabilities, aSecurityPolicy);
       
   394 			}
       
   395 		}
       
   396 	else if(cababilityCount > KMaxCapabilityCount2 || cababilityCount == 0)
       
   397 		{
       
   398 		__LEAVE(KErrCorrupt);
       
   399 		}
       
   400 	else
       
   401 		{
       
   402 		DBSC::CreateSecurityPolicyT3(aCapabilities, aSecurityPolicy);
       
   403 		}
       
   404 	}
       
   405 
       
   406 /**
       
   407 */
       
   408 inline CPDTextLoader::CPDTextLoader()
       
   409 	{
       
   410 	}
       
   411 
       
   412 /**
       
   413 Standard phase-one factory method for CPDTextLoader instance.
       
   414 CPDTextLoader instance will be used for loading a set of security policies information 
       
   415 from a text file, creating the related security policy objects and adding them to 
       
   416 a CPolicyDomain collection.
       
   417 @param aFs File server session
       
   418 @param aTextFileName Full text file path, which will be used as an input.
       
   419 @return A pointer to just created CPDTextLoader instance.
       
   420 @leave System-wide error codes, including KErrNoMemory.
       
   421 */
       
   422 CPDTextLoader* CPDTextLoader::NewLC(RFs& aFs, const TDesC& aTextFileName)
       
   423 	{
       
   424 	CPDTextLoader* self = new (ELeave) CPDTextLoader;
       
   425 	CleanupStack::PushL(self);
       
   426 	self->ConstructL(aFs, aTextFileName);
       
   427 	return self;
       
   428 	}
       
   429 
       
   430 /**
       
   431 */
       
   432 CPDTextLoader::~CPDTextLoader()
       
   433 	{
       
   434 	iRdStream.Close();
       
   435 	}
       
   436 
       
   437 /**
       
   438 Standard phase-two construction method for CPDTextLoader instance.
       
   439 @param aFs File server session
       
   440 @param aTextFileName Full text file path, which will be used as an input.
       
   441 */
       
   442 void CPDTextLoader::ConstructL(RFs& aFs, const TDesC& aTextFileName)
       
   443 	{
       
   444 	__LEAVE_IF_ERROR(iRdStream.Open(aFs, aTextFileName, EFileRead | EFileStreamText));
       
   445 	}
       
   446 
       
   447 /**
       
   448 MPolicyDomainLoader::RunL() implementation, which is used to load security policies
       
   449 from a text file, create the related security policy objects and add them
       
   450 to CPolicyDomain instance, controlled by aPolicyDomainBuilder object.
       
   451 It is not called directly, but will be called back.
       
   452 @param aPolicyDomainBuilder TPolicyDomainBuilder instance, which will be used to add
       
   453        created security policy objects to the controlled by it collection. 
       
   454 @leave System-wide error codes
       
   455 */
       
   456 void CPDTextLoader::RunL(TPolicyDomainBuilder& aPolicyDomainBuilder)
       
   457 	{
       
   458 	TStmtProps stmtProps;
       
   459 	const CDbPolicy* dbPolicy = LoadDbPolicyL(aPolicyDomainBuilder, stmtProps);
       
   460 	__ASSERT(dbPolicy);
       
   461 	LoadTblPoliciesL(aPolicyDomainBuilder, stmtProps, dbPolicy);
       
   462 	LoadBackupSIDL(aPolicyDomainBuilder, stmtProps);
       
   463 	}
       
   464 
       
   465 /**
       
   466 The method returns ETrue if this is the end of file.
       
   467 @return ETrue - EOF, EFalse otherwise
       
   468 */
       
   469 TBool CPDTextLoader::IsEofL()
       
   470 	{
       
   471 	return iRdStream.Source()->TellL(MStreamBuf::ERead) >= iRdStream.Source()->SizeL();
       
   472 	}
       
   473 
       
   474 /**
       
   475 The method parses a line from the text file, detects its type and gets the right side
       
   476 of "=" as a text line data descriptor. The information will be stored in aStmtProps
       
   477 parameter. If the text line is not recognizable, the method leaves with KErrCorrupt.
       
   478 Recognizable text line formats are:
       
   479 <[keyword]>
       
   480 <keyword>
       
   481 <keyword>=<value>
       
   482 <;>[comments]
       
   483 <blank line>
       
   484 @param aStmt Current text line
       
   485 @param aStmtProps The collected information will be stored there. Output parameter.
       
   486 @leave KErrCorrupt - the text line has unknown format
       
   487 */
       
   488 void CPDTextLoader::GetStmtPropsL(const TDesC& aStmt, TStmtProps& aStmtProps) const
       
   489 	{
       
   490 	aStmtProps.iValue.Set(aStmt);
       
   491 	if(aStmt.Length() == 0)
       
   492 		{
       
   493 		aStmtProps.iType = EStmtTBlank;
       
   494 		return;
       
   495 		}
       
   496 	else if(aStmt[0] == ';')
       
   497 		{
       
   498 		aStmtProps.iType = EStmtTComment;
       
   499 		return;
       
   500 		}
       
   501 	TBool res = TryGetStmt1Props(aStmt, aStmtProps);
       
   502 	if(!res)
       
   503 		{
       
   504 		res = TryGetStmt2Props(aStmt, aStmtProps);
       
   505 		}
       
   506 	if(!res)
       
   507 		{
       
   508 		__LEAVE(KErrCorrupt);
       
   509 		}
       
   510 	}
       
   511 
       
   512 /**
       
   513 Tries to process a text line as a:
       
   514 <keyword>
       
   515 or
       
   516 <[keyword]>
       
   517 @param aStmt Current text line
       
   518 @param aStmtProps Output parameter
       
   519 @return ETrue, if it is recognizable text line. Then the method will set the text line type 
       
   520 			   in aStmtProps.iType data member.
       
   521 		EFalse This is not recognizable text line with <keyword> or <[keyword]> format.
       
   522 */
       
   523 TBool CPDTextLoader::TryGetStmt1Props(const TDesC& aStmt, TStmtProps& aStmtProps) const
       
   524 	{
       
   525 	aStmtProps.iType = DBSC::StmtKeywordT1ToId(aStmt);
       
   526 	return aStmtProps.iType != EStmtTInvalid;
       
   527 	}
       
   528 
       
   529 /**
       
   530 Tries to process a text line as a:
       
   531 <keyword>=<value>
       
   532 @param aStmt Current text line
       
   533 @param aStmtProps Output parameter
       
   534 @return ETrue, if it is recognizable text line. Then the method will set the text line type 
       
   535 			   in aStmtProps.iType data member and the line value in aStmtProps.iValue
       
   536 			   data member. The text will be converted to a upper case.
       
   537 		EFalse This is not recognizable text line with <keyword>=<value> format.
       
   538 */
       
   539 TBool CPDTextLoader::TryGetStmt2Props(const TDesC& aStmt, TStmtProps& aStmtProps) const
       
   540 	{
       
   541 	TInt eqPos = aStmt.Find(KEqStr);
       
   542 	if(eqPos != KErrNotFound && eqPos < (aStmt.Length() - 1))
       
   543 		{
       
   544 		TPtr stmtKeyword(const_cast <TText*> (aStmt.Left(eqPos).Ptr()), eqPos, eqPos);
       
   545 		DBSC::Trim(stmtKeyword);
       
   546 		aStmtProps.iType = DBSC::StmtKeywordT2ToId(stmtKeyword);
       
   547 		if(aStmtProps.iType != EStmtTInvalid)
       
   548 			{
       
   549 			TInt valPos = eqPos + 1;
       
   550 			TInt valLen = aStmt.Length() - valPos;
       
   551 			TPtr value(const_cast <TText*> (aStmt.Mid(valPos).Ptr()), valLen, valLen);
       
   552 			DBSC::Trim(value);
       
   553 			aStmtProps.iValue.Set(value);
       
   554 			return ETrue;
       
   555 			}
       
   556 		}
       
   557 	return EFalse;
       
   558 	}
       
   559 
       
   560 /**
       
   561 The method loads a single text line from the file in the place, pointed by aStmt parameter.
       
   562 @param aStmt The place, where the text line data will be stored
       
   563 @return ETrue This not the end of file, the information in aStmt is valid.
       
   564         EFalse End of file.
       
   565 @leave System-wide error codes, including KErrCorrupt - unknown file format.
       
   566  */
       
   567 TBool CPDTextLoader::LoadStmtL(TPtr& aStmt)
       
   568 	{
       
   569 	if(IsEofL())
       
   570 		{
       
   571 		return EFalse;
       
   572 		}
       
   573 	TChar char_LF('\n');
       
   574 	iStmt8.Zero();
       
   575 	iRdStream.ReadL(iStmt8, char_LF);
       
   576 	aStmt.Copy(iStmt8);
       
   577 	const TInt len = aStmt.Length();
       
   578 	if(len < 2)
       
   579 		{//Unknown text file format. The text line should have at the end CR-LF pair.
       
   580 		__LEAVE(KErrCorrupt);
       
   581 		}
       
   582 	if(TChar(aStmt[len - 1]) != char_LF)
       
   583 		{//Too long line
       
   584 		__LEAVE(KErrCorrupt);
       
   585 		}
       
   586 	aStmt.SetLength(len - 1);
       
   587 	//The last character is (CR). Check for (LF).
       
   588 	TChar char_CR('\r');
       
   589 	if(TChar(aStmt[len - 2]) == char_CR)
       
   590 		{
       
   591 		aStmt.SetLength(len - 2);
       
   592 		}
       
   593 	DBSC::Trim(aStmt);	
       
   594 	return ETrue;
       
   595 	}
       
   596 
       
   597 /**
       
   598 The method loads a single text line from the file in the place, pointed by iStmt data member
       
   599 skipping lines with comments and blank lines.
       
   600 @param aStmtProps Output parameter. It will be initialized after the call.
       
   601 @return Statement class.
       
   602 @leave System-wide error codes, including KErrCorrupt - unknown file format.
       
   603 */
       
   604 TStmtClass CPDTextLoader::LoadNextStmtL(TStmtProps& aStmtProps)
       
   605 	{
       
   606 	TPtr stmt(const_cast <TText*> (iStmt.Ptr()), 0, iStmt.MaxLength());
       
   607 	TStmtClass stmtClass = EStmtCInvalid;
       
   608 	do
       
   609 		{
       
   610 		if(!LoadStmtL(stmt))
       
   611 			{
       
   612 			aStmtProps.iType = EStmtTEof;
       
   613 			break;
       
   614 			}
       
   615 		GetStmtPropsL(stmt, aStmtProps);
       
   616 		}
       
   617 	while((stmtClass = DBSC::StmtType2Class(aStmtProps.iType)) == EStmtCNoData);
       
   618 	return stmtClass;
       
   619 	}
       
   620 
       
   621 /**
       
   622 The method loads a single text line from the file in the place, pointed by iStmt data member
       
   623 skipping lines with comments and blank lines. The loaded text line type is expected to be
       
   624 aStmtType.
       
   625 @param aStmtProps Output parameter. It will be initialized after the call.
       
   626 @param aStmtType Expected type of the loaded text line.
       
   627 @leave System-wide error codes, including KErrCorrupt - unknown file format or the loaded line type is 
       
   628        not the expected type.
       
   629 */
       
   630 void CPDTextLoader::LoadNextStmtOfTypeL(TStmtProps& aStmtProps, TStmtType aStmtType)
       
   631 	{
       
   632 	(void)LoadNextStmtL(aStmtProps);
       
   633 	if(aStmtProps.iType != aStmtType)
       
   634 		{
       
   635 		__LEAVE(KErrCorrupt);
       
   636 		}
       
   637 	}
       
   638 
       
   639 /**
       
   640 The method loads all database policy related data from the text file.
       
   641 @param aPolicyDomainBuilder TPolicyDomainBuilder instance, which will be used to add
       
   642        created database security policy object to the controlled by it policy collection. 
       
   643 @param aStmtProps The information about the last loaded text line.
       
   644 @return A const pointer to just created database policy object from loaded text data.
       
   645 @leave System-wide error codes.
       
   646 */
       
   647 const CDbPolicy* CPDTextLoader::LoadDbPolicyL(TPolicyDomainBuilder& aPolicyDomainBuilder, 
       
   648 												TStmtProps& aStmtProps)
       
   649 	{
       
   650 	LoadNextStmtOfTypeL(aStmtProps, EStmtTDatabase);
       
   651 
       
   652 	CPolicyBase::RPolicyCollection policyColl;
       
   653 	CleanupClosePushL(policyColl);
       
   654 
       
   655 	LoadSecurityPoliciesL(policyColl, aStmtProps);
       
   656 
       
   657 	CDbPolicy* dbPolicy = CDbPolicy::NewLC(policyColl);
       
   658 	aPolicyDomainBuilder.SetDbPolicyL(dbPolicy);
       
   659 	CleanupStack::Pop(dbPolicy);
       
   660 
       
   661 	CleanupStack::PopAndDestroy(&policyColl);
       
   662 	return dbPolicy;
       
   663 	}
       
   664 
       
   665 /**
       
   666 The method loads all table policy related data from the text file.
       
   667 @param aPolicyDomainBuilder TPolicyDomainBuilder instance, which will be used to add
       
   668        created table security policy objects to the controlled by it policy collection. 
       
   669 @param aStmtProps The information about the last loaded text line.
       
   670 @param aDbPolicy A const pointer to the database policy object, created previously from loaded text data.
       
   671 @leave System-wide error codes.
       
   672 */
       
   673 void CPDTextLoader::LoadTblPoliciesL(TPolicyDomainBuilder& aPolicyDomainBuilder, 
       
   674 									 TStmtProps& aStmtProps, const CDbPolicy* aDbPolicy)
       
   675 	{
       
   676 	__ASSERT(aDbPolicy);
       
   677 	CPolicyBase::RPolicyCollection policyColl;
       
   678 	CleanupClosePushL(policyColl);
       
   679 	while(aStmtProps.iType == EStmtTTable)
       
   680 		{
       
   681 		LoadNextStmtOfTypeL(aStmtProps, EStmtTName);
       
   682 		TBufC<KMaxFileName> tableName;
       
   683 		tableName = aStmtProps.iValue;
       
   684 
       
   685 		LoadSecurityPoliciesL(policyColl, aStmtProps);
       
   686 
       
   687 		CTblPolicy* tblPolicy = CTblPolicy::NewLC(tableName, policyColl, aDbPolicy);
       
   688 		aPolicyDomainBuilder.AddTblPolicyL(tblPolicy);
       
   689 		CleanupStack::Pop(tblPolicy);
       
   690 		}
       
   691 	CleanupStack::PopAndDestroy(&policyColl);
       
   692 	}
       
   693 
       
   694 /**
       
   695 The method loads the backup & restore SID, if it is in the file.
       
   696 @param aPolicyDomainBuilder TPolicyDomainBuilder instance, which will be used to store
       
   697        loaded backup & restore SID. 
       
   698 @param aStmtProps The information about the last loaded text line.
       
   699 @leave System-wide error codes.
       
   700 */
       
   701 void CPDTextLoader::LoadBackupSIDL(TPolicyDomainBuilder& aPolicyDomainBuilder, 
       
   702 								   TStmtProps& aStmtProps)
       
   703 	{
       
   704 	TSecureId backupSID((TUint32)ECapability_None);//ECapability_None is used in TSecurityPolicy constructors too.
       
   705 	if(aStmtProps.iType == EStmtTBackup)
       
   706 		{
       
   707 		LoadNextStmtOfTypeL(aStmtProps, EStmtTSID);
       
   708 		backupSID = GetIdL(aStmtProps.iValue);
       
   709 		}
       
   710 	else if(aStmtProps.iType != EStmtTEof)
       
   711 		{
       
   712 		__LEAVE(KErrCorrupt);
       
   713 		}
       
   714 	aPolicyDomainBuilder.SetBackupSID(backupSID);
       
   715 	}
       
   716 
       
   717 /**
       
   718 The method loads all database/table related security policy information from the text file.
       
   719 @param aPolicyColl Output parameter - an array, which elements type is CPolicyBase::RPolicyCollection.
       
   720                    The collected from the text file security policy information wil be stored there.
       
   721 @param aStmtProps The information about the last loaded text line.
       
   722 @leave System-wide error codes.
       
   723 */
       
   724 void CPDTextLoader::LoadSecurityPoliciesL(CPolicyBase::RPolicyCollection& aPolicyColl, 
       
   725 										  TStmtProps& aStmtProps)
       
   726 	{
       
   727 	aPolicyColl.Reset();
       
   728 	(void)LoadNextStmtL(aStmtProps);
       
   729 	while(DBSC::StmtType2Class(aStmtProps.iType) == EStmtCPolicyType)
       
   730 		{
       
   731 		CPolicyBase::TPolicy policy;
       
   732 		policy.iType = DBSC::StmtType2PolicyType(aStmtProps.iType);
       
   733 		__ASSERT(policy.iType != EPTNone);
       
   734 		LoadSecurityPolicyL(policy.iData, aStmtProps);
       
   735 		__LEAVE_IF_ERROR(aPolicyColl.Append(policy));
       
   736 		}
       
   737 	if(aPolicyColl.Count() == 0)
       
   738 		{
       
   739 		__LEAVE(KErrCorrupt);
       
   740 		}
       
   741 	}
       
   742 
       
   743 /**
       
   744 The method loads a single security policy from the text file.
       
   745 @param aSecurityPolicy Output parameter - the information from the file will be stored there.
       
   746                    The collected from the text file security policy information wil be stored there.
       
   747 @param aStmtProps The information about the last loaded text line.
       
   748 @leave System-wide error codes.
       
   749 */
       
   750 void CPDTextLoader::LoadSecurityPolicyL(TSecurityPolicy& aSecurityPolicy, 
       
   751 										TStmtProps& aStmtProps)
       
   752 	{
       
   753 	TSecureId sid(0);
       
   754 	TVendorId vid(0);
       
   755 	RArray<TCapability> capabilities;
       
   756 	CleanupClosePushL(capabilities);
       
   757 	while(LoadNextStmtL(aStmtProps) == EStmtCPolicyItem)
       
   758 		{
       
   759 		if(aStmtProps.iType == EStmtTCapability)
       
   760 			{
       
   761 			GetCapabilitiesL(aStmtProps.iValue, capabilities);
       
   762 			}
       
   763 		else if(aStmtProps.iType == EStmtTSID)
       
   764 			{
       
   765 			if(sid != 0) 
       
   766 				{//duplicated SID text line
       
   767 				__LEAVE(KErrCorrupt);
       
   768 				}
       
   769 			sid = GetIdL(aStmtProps.iValue);
       
   770 			}
       
   771 		else if(aStmtProps.iType == EStmtTVID)
       
   772 			{
       
   773 			if(vid != 0) 
       
   774 				{//duplicated VID text line
       
   775 				__LEAVE(KErrCorrupt);
       
   776 				}
       
   777 			vid = GetIdL(aStmtProps.iValue);
       
   778 			}
       
   779 		else
       
   780 			{
       
   781 			__ASSERT(0);
       
   782 			}
       
   783 		}
       
   784 	if(capabilities.Count() == 0 && sid == 0 && vid == 0)
       
   785 		{//invalid security policy data
       
   786 		__LEAVE(KErrCorrupt);
       
   787 		}
       
   788 	DBSC::CreateSecurityPolicyL(sid, vid, capabilities, aSecurityPolicy);
       
   789 	CleanupStack::PopAndDestroy(&capabilities);
       
   790 	}
       
   791 
       
   792 /**
       
   793 The method parses a string with capabilities information and puts found capabilities in aCapability
       
   794 output parameter.
       
   795 @param aCapabilityStr Capabilities string.
       
   796 @param aCapabilities The collected capabilities will be stored there.
       
   797 @leave System-wide error codes. KErrCorrupt, if aCapability is not 0, which means there are
       
   798        2 or more capability strings for the same policy.    
       
   799 */
       
   800 void CPDTextLoader::GetCapabilitiesL(const TDesC& aCapabilityStr, 
       
   801 									 RArray<TCapability>& aCapabilities) const
       
   802 	{
       
   803 	if(aCapabilities.Count() > 0)
       
   804 		{//No more than one "capability" statement in the text file!
       
   805 		__LEAVE(KErrCorrupt);
       
   806 		}
       
   807 	TLinearOrder<TCapability> linearOrder(&DBSC::CompareCapabilities);
       
   808 	TLex lex(aCapabilityStr);
       
   809 	for(TPtrC token=lex.NextToken();token.Length()!=0;token.Set(lex.NextToken()))
       
   810 		{
       
   811 		TCapability cap = DBSC::CapabilityName2Id(token);
       
   812 		if(cap != ECapability_Limit)
       
   813 			{//InsertInOrder() - to warn the user in case of duplicates 
       
   814 			__LEAVE_IF_ERROR(aCapabilities.InsertInOrder(cap, linearOrder));
       
   815 			}
       
   816 		else
       
   817 			{
       
   818 			__LEAVE(KErrGeneral);//Unknown capability
       
   819 			}
       
   820 		}
       
   821 	}
       
   822 
       
   823 /**
       
   824 @param aStr A string with SID or VID.
       
   825 @return The UID, extracted from the string
       
   826 @leave System-wide error codes.
       
   827 */
       
   828 TUint CPDTextLoader::GetIdL(const TDesC& aStr) const
       
   829 	{
       
   830 	TLex lex(aStr);
       
   831 	TUint id;
       
   832 	__LEAVE_IF_ERROR(lex.Val(id, EHex));
       
   833 	if(id == 0)
       
   834 		{
       
   835 		__LEAVE(KErrCorrupt);
       
   836 		}
       
   837 	return id;
       
   838 	}
       
   839 
       
   840 } //end of - namespace DBSC