persistentstorage/sql/SRC/Server/SqlSrvUtil.cpp
changeset 0 08ec8eefde2f
child 10 fa9941cf3867
equal deleted inserted replaced
-1:000000000000 0:08ec8eefde2f
       
     1 // Copyright (c) 2006-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 //
       
    15 
       
    16 #include <stdlib.h>				//wchar_t
       
    17 #include "SqlPanic.h"
       
    18 #include "SqlSrvUtil.h"
       
    19 
       
    20 #ifdef _NOTIFY
       
    21 
       
    22 //Used in PrintAuthorizerArguments()
       
    23 _LIT8(KCreateIndex,			"Create index");			//SQLITE_CREATE_INDEX
       
    24 _LIT8(KCreateTable, 		"Create table");			//SQLITE_CREATE_TABLE
       
    25 _LIT8(KCreateTempIndex, 	"Create temp index");		//SQLITE_CREATE_TEMP_INDEX
       
    26 _LIT8(KCreateTempTable, 	"Create temp table");		//SQLITE_CREATE_TEMP_TABLE
       
    27 _LIT8(KCreateTempTrigger, 	"Create temp trigger");		//SQLITE_CREATE_TEMP_TRIGGER
       
    28 _LIT8(KCreateTempView, 		"Create temp view");		//SQLITE_CREATE_TEMP_VIEW
       
    29 _LIT8(KCreateTrigger, 		"Create trigger");			//SQLITE_CREATE_TRIGGER
       
    30 _LIT8(KCreateView, 			"Create view");				//SQLITE_CREATE_VIEW
       
    31 _LIT8(KDelete, 				"DELETE");					//SQLITE_DELETE
       
    32 _LIT8(KDropIndex, 			"Drop index");				//SQLITE_DROP_INDEX
       
    33 _LIT8(KDropTable, 			"Drop table");				//SQLITE_DROP_TABLE
       
    34 _LIT8(KDropTempIndex, 		"Drop temp index");			//SQLITE_DROP_TEMP_INDEX
       
    35 _LIT8(KDropTempTable, 		"Drop temp table");			//SQLITE_DROP_TEMP_TABLE
       
    36 _LIT8(KDropTempTrigger, 	"Drop temp trigger");		//SQLITE_DROP_TEMP_TRIGGER
       
    37 _LIT8(KDropTempView, 		"Drop temp view");			//SQLITE_DROP_TEMP_VIEW
       
    38 _LIT8(KDropTrigger, 		"Drop trigger");			//SQLITE_DROP_TRIGGER
       
    39 _LIT8(KDropView, 			"Drop view");				//SQLITE_DROP_VIEW
       
    40 _LIT8(KInsert, 				"INSERT");					//SQLITE_INSERT
       
    41 _LIT8(KPragma, 				"PRAGMA");					//SQLITE_PRAGMA
       
    42 _LIT8(KRead, 				"Read");					//SQLITE_READ
       
    43 _LIT8(KSelect, 				"SELECT");					//SQLITE_SELECT
       
    44 _LIT8(KTransaction, 		"TRANSACTION");				//SQLITE_TRANSACTION
       
    45 _LIT8(KUpdate, 				"UPDATE");					//SQLITE_UPDATE
       
    46 _LIT8(KAttach, 				"ATTACH");					//SQLITE_ATTACH
       
    47 _LIT8(KDetach, 				"DETACH");					//SQLITE_DETACH
       
    48 _LIT8(KAlterTable, 			"Alter table");				//SQLITE_ALTER_TABLE
       
    49 _LIT8(KReindex, 			"Reindex");					//SQLITE_REINDEX
       
    50 _LIT8(KAnalyze, 			"Analyze");					//SQLITE_ANALYZE
       
    51 _LIT8(KCreateVTable, 		"Create virt.table");		//SQLITE_CREATE_VTABLE
       
    52 _LIT8(KDropVTable, 			"Drop virt.table");			//SQLITE_DROP_VTABLE
       
    53 _LIT8(KFunctionCall, 		"Function call");			//SQLITE_FUNCTION
       
    54 
       
    55 _LIT8(KNull, 				"NULL");
       
    56 _LIT8(KInvalid, 			"INVALID");
       
    57 
       
    58 //Used in PrintAuthorizerArguments()
       
    59 const TPtrC8 KDbOpNames[] = 
       
    60 	{
       
    61 	KCreateIndex(),	KCreateTable(), KCreateTempIndex(), KCreateTempTable(), KCreateTempTrigger(),
       
    62 	KCreateTempView(), KCreateTrigger(), KCreateView(), KDelete(), KDropIndex(), 
       
    63 	KDropTable(), KDropTempIndex(), KDropTempTable(), KDropTempTrigger(), KDropTempView(),
       
    64 	KDropTrigger(), KDropView(), KInsert(), KPragma(), KRead(), 
       
    65 	KSelect(), KTransaction(), KUpdate(), KAttach(), KDetach(), KAlterTable(), KReindex(), KAnalyze(),
       
    66 	KCreateVTable(), KDropVTable(), KFunctionCall()
       
    67 	};
       
    68 
       
    69 const TInt KMaxOpCodes = sizeof(KDbOpNames) / sizeof(KDbOpNames[0]);
       
    70 
       
    71 _LIT(KFormatStr, "!!Authorize: %20.20S, %40.40S, %10.10S, %10.10S, %10.10S\r\n"); 
       
    72 
       
    73 /**
       
    74 This function has a defined implementaion only in _DEBUG mode and is used to print the authorizer arguments.
       
    75 
       
    76 @internalComponent
       
    77 */
       
    78 void PrintAuthorizerArguments(TInt aDbOpType, const char* aDbObjName1, const char* aDbObjName2, 
       
    79 							  const char* aDbName, const char* aTrgOrViewName)
       
    80 	{
       
    81 	__SQLASSERT(aDbOpType > 0 && aDbOpType <= KMaxOpCodes, ESqlPanicInternalError);
       
    82 	
       
    83 	//TPtrC8 objects cannot be used for the function arguments, because the arguments may not be 16-bit aligned!!!	
       
    84 
       
    85 	TBuf<20> opName;
       
    86 	opName.Copy(KDbOpNames[aDbOpType - 1]);
       
    87 
       
    88 	TBuf<64> dbObjName1; 
       
    89 	dbObjName1.Copy(KNull);
       
    90 	if(aDbObjName1)
       
    91 		{
       
    92 		dbObjName1.Copy(KInvalid);
       
    93 		if(User::StringLength((const TUint8*)aDbObjName1) <= dbObjName1.MaxLength())
       
    94 			{
       
    95 			dbObjName1.Copy(TPtrC8(reinterpret_cast <const TUint8*> (aDbObjName1)));
       
    96 			}
       
    97 		}
       
    98 	TBuf<64> dbObjName2; 
       
    99 	dbObjName2.Copy(KNull);
       
   100 	if(aDbObjName2)
       
   101 		{
       
   102 		dbObjName2.Copy(KInvalid);
       
   103 		if(User::StringLength((const TUint8*)aDbObjName2) <= dbObjName2.MaxLength())
       
   104 			{
       
   105 			dbObjName2.Copy(TPtrC8(reinterpret_cast <const TUint8*> (aDbObjName2)));
       
   106 			}
       
   107 		}
       
   108 	TBuf<64> dbName; 
       
   109 	dbName.Copy(KNull);
       
   110 	if(aDbName)
       
   111 		{
       
   112 		dbName.Copy(KInvalid);
       
   113 		if(User::StringLength((const TUint8*)aDbName) <= dbName.MaxLength())
       
   114 			{
       
   115 			dbName.Copy(TPtrC8(reinterpret_cast <const TUint8*> (aDbName)));
       
   116 			}
       
   117 		}
       
   118 	TBuf<64> trgOrViewName; 
       
   119 	trgOrViewName.Copy(KNull);
       
   120 	if(aTrgOrViewName)
       
   121 		{
       
   122 		trgOrViewName.Copy(KInvalid);
       
   123 		if(User::StringLength((const TUint8*)aTrgOrViewName) <= trgOrViewName.MaxLength())
       
   124 			{
       
   125 			trgOrViewName.Copy(TPtrC8(reinterpret_cast <const TUint8*> (aTrgOrViewName)));
       
   126 			}
       
   127 		}
       
   128 		
       
   129 	RDebug::Print(KFormatStr, &opName, &dbObjName1, &dbObjName2, &dbName, &trgOrViewName);
       
   130 	}
       
   131 #endif//_NOTIFY
       
   132 
       
   133 /**
       
   134 Converts a UTF16 encoded descriptor to a UTF8 encoded descriptor.
       
   135 Note: the function works only for input descriptors with length less or equal than KMaxFileName.
       
   136 
       
   137 @param aIn  The input UTF16 encoded descriptor,
       
   138 @param aOut The output buffer where the converted input descriptor will be stored. 
       
   139 @return True if the conversion was successful, false otherwise.
       
   140 
       
   141 @panic SqlDb 4 In _DEBUG mode if aIn length is bigger than KMaxFileName.
       
   142 @panic SqlDb 4 In _DEBUG mode if aOut max length is less than KMaxFileName.
       
   143 
       
   144 @internalComponent
       
   145 */
       
   146 TBool UTF16ToUTF8(const TDesC& aIn, TDes8& aOut)
       
   147 	{
       
   148 	__SQLASSERT(aIn.Length() <= KMaxFileName, ESqlPanicBadArgument);
       
   149 	__SQLASSERT(aOut.MaxLength() >= KMaxFileName, ESqlPanicBadArgument);
       
   150 	if(aIn.Length() > KMaxFileName || aOut.MaxLength() < KMaxFileName)
       
   151 		{
       
   152 		return EFalse;	
       
   153 		}
       
   154 	TBuf16<KMaxFileName + 1> des;
       
   155 	des.Copy(aIn);
       
   156 	des.Append(TChar(0));
       
   157 	TInt len = wcstombs((char*)aOut.Ptr(), (const wchar_t*)des.Ptr(), KMaxFileName);
       
   158 	//Check the file name length. If it is longer than KMaxFileName characters, then the file name is not valid.
       
   159 	if(len >= 0 && len <= KMaxFileName)
       
   160 		{
       
   161 		aOut.SetLength(len);
       
   162 		return ETrue;
       
   163 		}
       
   164 	return EFalse;
       
   165 	}
       
   166 
       
   167 /**
       
   168 Converts a zero-terminated, UTF16 encoded file name to a zero-terminated, UTF8 encoded file name.
       
   169 @param aFileName The input file name buffer. aFileName argument is expected to point to UTF16 encoded, 
       
   170 				 zero terminated string,
       
   171 @param aFileNameDestBuf The output file name buffer where the converted input file name will be stored. 
       
   172 						The output file name buffer max length should be at least KMaxFileName + 1.
       
   173 @return True if the conversion was successful, false otherwise.
       
   174 
       
   175 @panic SqlDb 4 In _DEBUG mode if aFileName length is bigger than KMaxFileName + 1.
       
   176 @panic SqlDb 4 In _DEBUG mode if aFileName is not zero-terminated or if aFileNameDestBuf max length is less than KMaxFileName + 1.
       
   177 
       
   178 @internalComponent
       
   179 */
       
   180 TBool UTF16ZToUTF8Z(const TDesC& aFileName, TDes8& aFileNameDestBuf)
       
   181 	{
       
   182 	__SQLASSERT(aFileName.Length() <= (KMaxFileName + 1), ESqlPanicBadArgument);
       
   183 	__SQLASSERT(aFileName[aFileName.Length() - 1] == 0, ESqlPanicBadArgument);
       
   184 	__SQLASSERT(aFileNameDestBuf.MaxLength() >= (KMaxFileName + 1), ESqlPanicBadArgument);
       
   185 	const wchar_t* src = reinterpret_cast <const wchar_t*> (aFileName.Ptr());
       
   186 	TInt len = wcstombs((char*)aFileNameDestBuf.Ptr(), src, KMaxFileName);
       
   187 	//Check the file name length. If it is longer than KMaxFileName characters, then the file name is not valid.
       
   188 	if(len >= 0 && len <= KMaxFileName)
       
   189 		{
       
   190 		aFileNameDestBuf.SetLength(len + 1);
       
   191 		aFileNameDestBuf[len] = 0;
       
   192 		return ETrue;
       
   193 		}
       
   194 	return EFalse;
       
   195 	}
       
   196 
       
   197 /**
       
   198 Converts a UTF16 encoded file name to a zero-terminated, UTF8 encoded file name.
       
   199 @param aFileName The input file name buffer. aFileName argument is expected to point to UTF16 encoded string,
       
   200 @param aFileNameDestBuf The output file name buffer where the converted input file name will be stored. 
       
   201 						The output file name buffer max length should be at least KMaxFileName + 1.
       
   202 @return True if the conversion was successful, false otherwise.
       
   203 
       
   204 @panic SqlDb 4 In _DEBUG mode if aFileName length is bigger than KMaxFileName.
       
   205 @panic SqlDb 4 In _DEBUG mode if aFileNameDestBuf max length is less than KMaxFileName + 1.
       
   206 
       
   207 @internalComponent
       
   208 */
       
   209 TBool UTF16ToUTF8Z(const TDesC& aFileName, TDes8& aFileNameDestBuf)
       
   210 	{
       
   211 	__SQLASSERT(aFileName.Length() <= KMaxFileName, ESqlPanicBadArgument);
       
   212 	__SQLASSERT(aFileNameDestBuf.MaxLength() >= (KMaxFileName + 1), ESqlPanicBadArgument);
       
   213 	TBool rc = ::UTF16ToUTF8(aFileName, aFileNameDestBuf);
       
   214 	if(rc)
       
   215 		{
       
   216 		aFileNameDestBuf.Append(0);
       
   217 		}
       
   218 	return rc;
       
   219 	}
       
   220 
       
   221 //Returns true if aDbFileName is a read-only file
       
   222 TBool IsReadOnlyFileL(RFs& aFs, const TDesC& aDbFileName)
       
   223 	{
       
   224 	TEntry entry;
       
   225 	TInt err = aFs.Entry(aDbFileName, entry);
       
   226 	if(err == KErrNotFound)
       
   227 		{//Non-existing file
       
   228 		return EFalse;	
       
   229 		}
       
   230 	__SQLLEAVE_IF_ERROR(err);
       
   231 	return entry.IsReadOnly();
       
   232 	}