diff -r 000000000000 -r 08ec8eefde2f persistentstorage/dbms/SPConv/cn_util.cpp --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/persistentstorage/dbms/SPConv/cn_util.cpp Fri Jan 22 11:06:30 2010 +0200 @@ -0,0 +1,318 @@ +// Copyright (c) 2004-2009 Nokia Corporation and/or its subsidiary(-ies). +// All rights reserved. +// This component and the accompanying materials are made available +// under the terms of "Eclipse Public License v1.0" +// which accompanies this distribution, and is available +// at the URL "http://www.eclipse.org/legal/epl-v10.html". +// +// Initial Contributors: +// Nokia Corporation - initial contribution. +// +// Contributors: +// +// Description: +// TSPConvUtil class +// +// + +#include +#include +#include "D32Assert.h" +#include "cn_util.h" +#include "cn_main.h" +#include "cn_cmdparse.h" + +TBool TSPConvUtil::iPromptOnError = ETrue; + +_LIT(KTxt0, "Program information"); +_LIT(KTxt1, "EDbSpConv version 1.0\n"); +_LIT(KTxt2, "Symbian OS text policy file/binary policy file converter\n"); +_LIT(KTxt3, "Copyright (c) 2004 Symbian Software Ltd. All rights reserved.\n"); +_LIT(KTxt4, "\nUsage:\n"); +_LIT(KTxt5, "EDbSpConv /h\n"); +_LIT(KTxt6, "EDbSpConv /f= /b=[path]\\[.spd] [/s]\n"); +_LIT(KTxt7, "\nWhere:\n"); +_LIT(KTxt8, "/h - displays program information\n"); +_LIT(KTxt9, " - security policy text file, including path\n"); +_LIT(KTxt10,"[.spd] - security policy binary file\n"); +_LIT(KTxt11,"[path] - optional - binary policy file path.\n"); +_LIT(KTxt12," By default the file will be placed in the current directory.\n"); +_LIT(KTxt13," - database format uid - hex\n"); +_LIT(KTxt14,"[/s] - optional - does not wait for a button pressing in case\n"); +_LIT(KTxt15," of an error (when an error message is displayed).\n"); + +/** +Prints program information. +*/ +static void PrintInfoL() + { + CConsoleBase* con = Console::NewL(KTxt0, TSize(KConsFullScreen, KConsFullScreen)); + CleanupStack::PushL(con); + con->Printf(KTxt1); + con->Printf(KTxt2); + con->Printf(KTxt3); + con->Printf(KTxt4); + con->Printf(KTxt5); + con->Printf(KTxt6); + con->Printf(KTxt7); + con->Printf(KTxt8); + con->Printf(KTxt9); + con->Printf(KTxt10); + con->Printf(KTxt11); + con->Printf(KTxt12); + con->Printf(KTxt13); + con->Printf(KTxt14); + con->Printf(KTxt15); + con->Getch(); + CleanupStack::PopAndDestroy(con); + } + +/** +Parses command line arguments and fills TCmdLinePrm structure with the parsed data. +A a result of method's execution, the following items will be stored in aCmdLinePrm: +requested action: (BIN->TXT) or (TXT->BIN), text policy file path and +binary policy file path. +If there is a "/s" command line argument and there are parsing errors, the error message +will be stored in epocwind.out file only. By default (no "/s" command) the error messages +will be displayed on the screen and stored in epocwind.out file. +@param aCmdLineParser A reference to a command line argument parser instance. +@param aCmdLinePrm A reference to a TCmdLinePrm instance. Output parameter. The parsed + command line arguments will be stored there. +@leave KErrArgument Not enough command line arguments, bad argument, non-recognizable argument. +@leave KErrNotFound Missing command line argument. +*/ +void TSPConvUtil::ParseL(const CCommandLineArguments& aCmdLineParser, + TCmdLinePrm& aCmdLinePrm) + { + aCmdLinePrm.Zero(); + TInt prmCnt = aCmdLineParser.Count(); + if(prmCnt < 2) + {//Print program information and exit + ::PrintInfoL(); + User::Exit(KErrNone); + } + else + { + //The comand is fixed explicitly, because now there is only one conversion: + //from text policy file to binary policy file. + aCmdLinePrm.iAction = TCmdLinePrm::ETxt2Bin; + for(TInt i=0;i\\ + 2) \\.spd + 3) + 4) .spd + '/' symbol might be used as a directory separator as well. + Output parameter - the created binary policy file path will be stored there. +@leave KErrArgument Bad format of aFile input parameter. +*/ +void TSPConvUtil::ConstructBinFileNameL(TDes& aFile) + { + //Replace all '/' in aFile with '\', otherwise TParse won't work properly. + TInt pos; + while((pos = aFile.Locate('/')) != KErrNotFound) + { + aFile[pos] = '\\'; + } + TParse fileNameParser; + __LEAVE_IF_ERROR(fileNameParser.Set(aFile, NULL, NULL)); + TPtrC fileName = fileNameParser.Name(); + TUid dbUid; + TLex lex(fileName); + if(lex.Val(*(TUint32*)&dbUid, EHex) == KErrNone && lex.Eos()) + { + if(dbUid != KNullUid) + { + _LIT(KExt, ".SPD"); + TPtrC fileExt = fileNameParser.Ext(); + if(fileExt.Length() == 0) + { + aFile.Append(KExt); + } + else if(fileExt != KExt) + { + _LIT(KText, "Invalid \"UID\" file extension: \"%S\"\n"); + TSPConvUtil::Print(KText, fileExt); + __LEAVE(KErrArgument); + } + return; + } + } + _LIT(KText, "Invalid \"UID\" file: \"%S\"\n"); + TSPConvUtil::Print(KText, aFile); + __LEAVE(KErrArgument); + } + +/** +The method extracts the UID from aFile parameter, which is expected to represent +binary security policy file path. +The method asserts that the extracted UID is not KNullUid. +@param aFile Binary policy file path +@leave System-wide error codes from file name parsing or KErrNoMemory (from the parser creation). +*/ +TUid TSPConvUtil::UidFromFileNameL(const TDesC& aFile) + { + TParse* parser = new (ELeave) TParse; + CleanupStack::PushL(parser); + __LEAVE_IF_ERROR(parser->Set(aFile, NULL, NULL)); + TPtrC fileName = parser->Name(); + TUid domainUid; + TLex lex(fileName); + if(lex.Val(*(TUint32*)&domainUid, EHex) == KErrNone && lex.Eos() && domainUid != KNullUid) + { + } + CleanupStack::PopAndDestroy(parser); + __ASSERT(domainUid != KNullUid); + return domainUid; + } + +/** +The method prints aText string to epocwnd.out file and on the screen and waits for +a button pressing. +If "/s" command line argument is presented, the method won't wait for a button pressing. +@param aText The text which has to be printed. +*/ +void TSPConvUtil::Print(const TDesC& aText) + { + RDebug::Print(aText); + if(TSPConvUtil::iPromptOnError) + { + RNotifier notify; + TInt err = notify.Connect(); + if(err == KErrNone) + { + TRequestStatus stat; + TInt but; + _LIT(KNotify,"EDBSPConv"); + _LIT(KContinue,"Continue"); + notify.Notify(KNotify, aText, KContinue, KNullDesC, but, stat); + User::WaitForRequest(stat); + notify.Close(); + } + else + { + RDebug::Print(_L("Error=%d connecting notifier session!\n"), err); + } + } + User::InfoPrint(aText); + } + +/** +The method outputs a formatted text to epocwnd.out file and on the screen using aFormat +parameter as a format string and aNumber as a number which has to be printed out with +the supplied format string. +If "/s" command line argument is presented, the method won't wait for a button pressing. +@param aFormat The number format string. +@param aNumber The number, which has to be printed together with the text. There must be a "%d" + format specification somewhere in aFormat parameter. +*/ +void TSPConvUtil::Print(const TDesC& aFormat, TInt aNumber) + { + TBuf<300> buf; + buf.Format(aFormat, aNumber); + TSPConvUtil::Print(buf); + } + +/** +The method outputs a formatted text to epocwnd.out file and on the screen using aFormat +parameter as a format string and aText as a text which has to be printed out with the +supplied format string. +If "/s" command line argument is presented, the method won't wait for a button pressing. +@param aFormat The number format string. +@param aText The text, which has to be formatted. There must be a "%S" + format specification somewhere in aFormat parameter. +*/ +void TSPConvUtil::Print(const TDesC& aFormat, const TDesC& aText) + { + TBuf<300> buf; + buf.Format(aFormat, &aText); + TSPConvUtil::Print(buf); + } + +/** +The method outputs a formatted text to epocwnd.out file and on the screen using aFormat +parameter as a format string and aText1 and aText2 as texts which have to be printed +out with the supplied format string. +If "/s" command line argument is presented, the method won't wait for a button pressing. +@param aFormat The number format string. +@param aText1 The text, which has to be printed out. There must be a "%S" + format specification somewhere in aFormat parameter. +@param aText2 The text, which has to be printed out. There must be a "%S" + format specification somewhere in aFormat parameter. +*/ +void TSPConvUtil::Print(const TDesC& aFormat, const TDesC& aText1, const TDesC& aText2) + { + TBuf<500> buf; + buf.Format(aFormat, &aText1, &aText2); + TSPConvUtil::Print(buf); + } +