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