sl@0: // Copyright (c) 2006-2010 Nokia Corporation and/or its subsidiary(-ies). sl@0: // All rights reserved. sl@0: // This component and the accompanying materials are made available sl@0: // under the terms of "Eclipse Public License v1.0" sl@0: // which accompanies this distribution, and is available sl@0: // at the URL "http://www.eclipse.org/legal/epl-v10.html". sl@0: // sl@0: // Initial Contributors: sl@0: // Nokia Corporation - initial contribution. sl@0: // sl@0: // Contributors: sl@0: // sl@0: // Description: sl@0: // sl@0: sl@0: #include //wchar_t sl@0: #include "SqlAssert.h" sl@0: #include "SqlSrvUtil.h" sl@0: #include "OstTraceDefinitions.h" sl@0: #ifdef OST_TRACE_COMPILER_IN_USE sl@0: #include "SqlSrvUtilTraces.h" sl@0: #endif sl@0: #include "SqlTraceDef.h" sl@0: sl@0: #ifdef _SQL_AUTHORIZER_TRACE_ENABLED sl@0: sl@0: //Used in PrintAuthorizerArguments() sl@0: _LIT8(KCreateIndex, "Create index"); //SQLITE_CREATE_INDEX sl@0: _LIT8(KCreateTable, "Create table"); //SQLITE_CREATE_TABLE sl@0: _LIT8(KCreateTempIndex, "Create temp index"); //SQLITE_CREATE_TEMP_INDEX sl@0: _LIT8(KCreateTempTable, "Create temp table"); //SQLITE_CREATE_TEMP_TABLE sl@0: _LIT8(KCreateTempTrigger, "Create temp trigger"); //SQLITE_CREATE_TEMP_TRIGGER sl@0: _LIT8(KCreateTempView, "Create temp view"); //SQLITE_CREATE_TEMP_VIEW sl@0: _LIT8(KCreateTrigger, "Create trigger"); //SQLITE_CREATE_TRIGGER sl@0: _LIT8(KCreateView, "Create view"); //SQLITE_CREATE_VIEW sl@0: _LIT8(KDelete, "DELETE"); //SQLITE_DELETE sl@0: _LIT8(KDropIndex, "Drop index"); //SQLITE_DROP_INDEX sl@0: _LIT8(KDropTable, "Drop table"); //SQLITE_DROP_TABLE sl@0: _LIT8(KDropTempIndex, "Drop temp index"); //SQLITE_DROP_TEMP_INDEX sl@0: _LIT8(KDropTempTable, "Drop temp table"); //SQLITE_DROP_TEMP_TABLE sl@0: _LIT8(KDropTempTrigger, "Drop temp trigger"); //SQLITE_DROP_TEMP_TRIGGER sl@0: _LIT8(KDropTempView, "Drop temp view"); //SQLITE_DROP_TEMP_VIEW sl@0: _LIT8(KDropTrigger, "Drop trigger"); //SQLITE_DROP_TRIGGER sl@0: _LIT8(KDropView, "Drop view"); //SQLITE_DROP_VIEW sl@0: _LIT8(KInsert, "INSERT"); //SQLITE_INSERT sl@0: _LIT8(KPragma, "PRAGMA"); //SQLITE_PRAGMA sl@0: _LIT8(KRead, "Read"); //SQLITE_READ sl@0: _LIT8(KSelect, "SELECT"); //SQLITE_SELECT sl@0: _LIT8(KTransaction, "TRANSACTION"); //SQLITE_TRANSACTION sl@0: _LIT8(KUpdate, "UPDATE"); //SQLITE_UPDATE sl@0: _LIT8(KAttach, "ATTACH"); //SQLITE_ATTACH sl@0: _LIT8(KDetach, "DETACH"); //SQLITE_DETACH sl@0: _LIT8(KAlterTable, "Alter table"); //SQLITE_ALTER_TABLE sl@0: _LIT8(KReindex, "Reindex"); //SQLITE_REINDEX sl@0: _LIT8(KAnalyze, "Analyze"); //SQLITE_ANALYZE sl@0: _LIT8(KCreateVTable, "Create virt.table"); //SQLITE_CREATE_VTABLE sl@0: _LIT8(KDropVTable, "Drop virt.table"); //SQLITE_DROP_VTABLE sl@0: _LIT8(KFunctionCall, "Function call"); //SQLITE_FUNCTION sl@0: sl@0: _LIT8(KNull, "NULL"); sl@0: _LIT8(KInvalid, "INVALID"); sl@0: sl@0: //Used in PrintAuthorizerArguments() sl@0: const TPtrC8 KDbOpNames[] = sl@0: { sl@0: KCreateIndex(), KCreateTable(), KCreateTempIndex(), KCreateTempTable(), KCreateTempTrigger(), sl@0: KCreateTempView(), KCreateTrigger(), KCreateView(), KDelete(), KDropIndex(), sl@0: KDropTable(), KDropTempIndex(), KDropTempTable(), KDropTempTrigger(), KDropTempView(), sl@0: KDropTrigger(), KDropView(), KInsert(), KPragma(), KRead(), sl@0: KSelect(), KTransaction(), KUpdate(), KAttach(), KDetach(), KAlterTable(), KReindex(), KAnalyze(), sl@0: KCreateVTable(), KDropVTable(), KFunctionCall() sl@0: }; sl@0: sl@0: #ifdef _DEBUG sl@0: const TInt KMaxOpCodes = sizeof(KDbOpNames) / sizeof(KDbOpNames[0]); sl@0: #endif sl@0: sl@0: /** sl@0: This function has a defined implementaion only in _DEBUG mode and is used to print the authorizer arguments. sl@0: sl@0: @internalComponent sl@0: */ sl@0: void PrintAuthorizerArguments(TInt aDbOpType, const char* aDbObjName1, const char* aDbObjName2, sl@0: const char* aDbName, const char* aTrgOrViewName) sl@0: { sl@0: __ASSERT_DEBUG(aDbOpType > 0 && aDbOpType <= KMaxOpCodes, __SQLPANIC2(ESqlPanicInternalError)); sl@0: sl@0: //TPtrC8 objects cannot be used for the function arguments, because the arguments may not be 16-bit aligned!!! sl@0: sl@0: TBuf<20> opName; sl@0: opName.Copy(KDbOpNames[aDbOpType - 1]); sl@0: sl@0: TBuf<64> dbObjName1; sl@0: dbObjName1.Copy(KNull); sl@0: if(aDbObjName1) sl@0: { sl@0: dbObjName1.Copy(KInvalid); sl@0: if(User::StringLength((const TUint8*)aDbObjName1) <= dbObjName1.MaxLength()) sl@0: { sl@0: dbObjName1.Copy(TPtrC8(reinterpret_cast (aDbObjName1))); sl@0: } sl@0: } sl@0: TBuf<64> dbObjName2; sl@0: dbObjName2.Copy(KNull); sl@0: if(aDbObjName2) sl@0: { sl@0: dbObjName2.Copy(KInvalid); sl@0: if(User::StringLength((const TUint8*)aDbObjName2) <= dbObjName2.MaxLength()) sl@0: { sl@0: dbObjName2.Copy(TPtrC8(reinterpret_cast (aDbObjName2))); sl@0: } sl@0: } sl@0: TBuf<64> dbName; sl@0: dbName.Copy(KNull); sl@0: if(aDbName) sl@0: { sl@0: dbName.Copy(KInvalid); sl@0: if(User::StringLength((const TUint8*)aDbName) <= dbName.MaxLength()) sl@0: { sl@0: dbName.Copy(TPtrC8(reinterpret_cast (aDbName))); sl@0: } sl@0: } sl@0: TBuf<64> trgOrViewName; sl@0: trgOrViewName.Copy(KNull); sl@0: if(aTrgOrViewName) sl@0: { sl@0: trgOrViewName.Copy(KInvalid); sl@0: if(User::StringLength((const TUint8*)aTrgOrViewName) <= trgOrViewName.MaxLength()) sl@0: { sl@0: trgOrViewName.Copy(TPtrC8(reinterpret_cast (aTrgOrViewName))); sl@0: } sl@0: } sl@0: sl@0: SQL_TRACE_AUTHORIZER(OstTraceExt5(TRACE_INTERNALS, SQLAUTHORIZER, "0;PrintAuthorizerArguments;%20.20S;%40.40S;%10.10S;%10.10S;%10.10S", __SQLPRNSTR(opName), __SQLPRNSTR(dbObjName1), __SQLPRNSTR(dbObjName2), __SQLPRNSTR(dbName), __SQLPRNSTR(trgOrViewName))); sl@0: } sl@0: #endif//_SQL_AUTHORIZER_TRACE_ENABLED sl@0: sl@0: /** sl@0: Converts a UTF16 encoded descriptor to a UTF8 encoded descriptor. sl@0: Note: the function works only for input descriptors with length less or equal than KMaxFileName. sl@0: sl@0: @param aIn The input UTF16 encoded descriptor, sl@0: @param aOut The output buffer where the converted input descriptor will be stored. sl@0: @return True if the conversion was successful, false otherwise. sl@0: sl@0: @panic SqlDb 4 In _DEBUG mode if aIn length is bigger than KMaxFileName. sl@0: @panic SqlDb 4 In _DEBUG mode if aOut max length is less than KMaxFileName. sl@0: sl@0: @internalComponent sl@0: */ sl@0: TBool UTF16ToUTF8(const TDesC& aIn, TDes8& aOut) sl@0: { sl@0: __ASSERT_DEBUG(aIn.Length() <= KMaxFileName, __SQLPANIC2(ESqlPanicBadArgument)); sl@0: __ASSERT_DEBUG(aOut.MaxLength() >= KMaxFileName, __SQLPANIC2(ESqlPanicBadArgument)); sl@0: TBuf16 des; sl@0: des.Copy(aIn); sl@0: des.Append(TChar(0)); sl@0: TInt len = wcstombs((char*)aOut.Ptr(), (const wchar_t*)des.Ptr(), KMaxFileName); sl@0: //Check the file name length. If it is longer than KMaxFileName characters, then the file name is not valid. sl@0: if((TUint)len <= KMaxFileName) sl@0: { sl@0: aOut.SetLength(len); sl@0: return ETrue; sl@0: } sl@0: return EFalse; sl@0: } sl@0: sl@0: /** sl@0: Converts a zero-terminated, UTF16 encoded file name to a zero-terminated, UTF8 encoded file name. sl@0: @param aFileName The input file name buffer. aFileName argument is expected to point to UTF16 encoded, sl@0: zero terminated string, sl@0: @param aFileNameDestBuf The output file name buffer where the converted input file name will be stored. sl@0: The output file name buffer max length should be at least KMaxFileName + 1. sl@0: @return True if the conversion was successful, false otherwise. sl@0: sl@0: @panic SqlDb 4 In _DEBUG mode if aFileName length is bigger than KMaxFileName + 1. sl@0: @panic SqlDb 4 In _DEBUG mode if aFileName is not zero-terminated or if aFileNameDestBuf max length is less than KMaxFileName + 1. sl@0: sl@0: @internalComponent sl@0: */ sl@0: TBool UTF16ZToUTF8Z(const TDesC& aFileName, TDes8& aFileNameDestBuf) sl@0: { sl@0: __ASSERT_DEBUG(aFileName.Length() <= (KMaxFileName + 1), __SQLPANIC2(ESqlPanicBadArgument)); sl@0: __ASSERT_DEBUG(aFileName[aFileName.Length() - 1] == 0, __SQLPANIC2(ESqlPanicBadArgument)); sl@0: __ASSERT_DEBUG(aFileNameDestBuf.MaxLength() >= (KMaxFileName + 1), __SQLPANIC2(ESqlPanicBadArgument)); sl@0: const wchar_t* src = reinterpret_cast (aFileName.Ptr()); sl@0: TInt len = wcstombs((char*)aFileNameDestBuf.Ptr(), src, KMaxFileName); sl@0: //Check the file name length. If it is longer than KMaxFileName characters, then the file name is not valid. sl@0: if((TUint)len <= KMaxFileName) sl@0: { sl@0: aFileNameDestBuf.SetLength(len + 1); sl@0: aFileNameDestBuf[len] = 0; sl@0: return ETrue; sl@0: } sl@0: return EFalse; sl@0: } sl@0: sl@0: /** sl@0: Converts a UTF16 encoded file name to a zero-terminated, UTF8 encoded file name. sl@0: @param aFileName The input file name buffer. aFileName argument is expected to point to UTF16 encoded string, sl@0: @param aFileNameDestBuf The output file name buffer where the converted input file name will be stored. sl@0: The output file name buffer max length should be at least KMaxFileName + 1. sl@0: @return True if the conversion was successful, false otherwise. sl@0: sl@0: @panic SqlDb 4 In _DEBUG mode if aFileName length is bigger than KMaxFileName. sl@0: @panic SqlDb 4 In _DEBUG mode if aFileNameDestBuf max length is less than KMaxFileName + 1. sl@0: sl@0: @internalComponent sl@0: */ sl@0: TBool UTF16ToUTF8Z(const TDesC& aFileName, TDes8& aFileNameDestBuf) sl@0: { sl@0: __ASSERT_DEBUG(aFileName.Length() <= KMaxFileName, __SQLPANIC2(ESqlPanicBadArgument)); sl@0: __ASSERT_DEBUG(aFileNameDestBuf.MaxLength() >= (KMaxFileName + 1), __SQLPANIC2(ESqlPanicBadArgument)); sl@0: TBool rc = ::UTF16ToUTF8(aFileName, aFileNameDestBuf); sl@0: if(rc) sl@0: { sl@0: aFileNameDestBuf.Append(0); sl@0: } sl@0: return rc; sl@0: } sl@0: sl@0: //Returns true if aDbFileName is a read-only file sl@0: TBool IsReadOnlyFileL(RFs& aFs, const TDesC& aDbFileName) sl@0: { sl@0: TEntry entry; sl@0: TInt err = aFs.Entry(aDbFileName, entry); sl@0: if(err == KErrNotFound) sl@0: {//Non-existing file sl@0: return EFalse; sl@0: } sl@0: __SQLLEAVE_IF_ERROR2(err); sl@0: return entry.IsReadOnly(); sl@0: }