os/security/cryptomgmtlibs/securitycommonutils/inc/streamingarray.inl
author sl@SLION-WIN7.fritz.box
Fri, 15 Jun 2012 03:10:57 +0200
changeset 0 bde4ae8d615e
permissions -rw-r--r--
First public contribution.
sl@0
     1
/*
sl@0
     2
* Copyright (c) 2008-2009 Nokia Corporation and/or its subsidiary(-ies).
sl@0
     3
* All rights reserved.
sl@0
     4
* This component and the accompanying materials are made available
sl@0
     5
* under the terms of the License "Eclipse Public License v1.0"
sl@0
     6
* which accompanies this distribution, and is available
sl@0
     7
* at the URL "http://www.eclipse.org/legal/epl-v10.html".
sl@0
     8
*
sl@0
     9
* Initial Contributors:
sl@0
    10
* Nokia Corporation - initial contribution.
sl@0
    11
*
sl@0
    12
* Contributors:
sl@0
    13
*
sl@0
    14
* Description: 
sl@0
    15
* Implementation of utility functions for copying and streaming RArray/RpointerArray. 
sl@0
    16
*
sl@0
    17
*/
sl@0
    18
sl@0
    19
sl@0
    20
/**
sl@0
    21
 @file 
sl@0
    22
 @internalTechnology
sl@0
    23
 @released
sl@0
    24
*/
sl@0
    25
sl@0
    26
#ifndef STREAMINGARRAY_INL
sl@0
    27
#define STREAMINGARRAY_INL
sl@0
    28
sl@0
    29
#include <s32mem.h>
sl@0
    30
#include <scs/streamingarray.h>
sl@0
    31
#include <scs/nullstream.h>
sl@0
    32
#include <scs/streamingarray.h>
sl@0
    33
sl@0
    34
// Traits classes - internally required by RArray/RPointerArray functions
sl@0
    35
sl@0
    36
template <class T>
sl@0
    37
class TTraits
sl@0
    38
	{
sl@0
    39
public:
sl@0
    40
	static T* CopyL(const T& aRight) { return T::NewL(aRight);}
sl@0
    41
	static T* ReadFromStreamL(RReadStream& aStream) { return T::NewL(aStream); }
sl@0
    42
	static T* ReadFromStreamLC(RReadStream& aStream) { return T::NewLC(aStream); }
sl@0
    43
	static void WriteToStreamL(const T& aItem, RWriteStream& aStream) { aStream << aItem; }
sl@0
    44
	};
sl@0
    45
sl@0
    46
// Specialisation for HBufC
sl@0
    47
template <>
sl@0
    48
class TTraits<HBufC16>
sl@0
    49
	{
sl@0
    50
public:
sl@0
    51
	static HBufC16* CopyL(const HBufC16& aOther) { return aOther.AllocL();}
sl@0
    52
	static HBufC16* ReadFromStreamL(RReadStream& aStream) { return HBufC16::NewL(aStream, KMaxTInt); }
sl@0
    53
	static HBufC16* ReadFromStreamLC(RReadStream& aStream) { return HBufC16::NewLC(aStream, KMaxTInt); }
sl@0
    54
	static void WriteToStreamL(const HBufC16& aItem, RWriteStream& aStream) { aStream << aItem; }
sl@0
    55
	};
sl@0
    56
		
sl@0
    57
template <>
sl@0
    58
class TTraits<HBufC8>
sl@0
    59
	{
sl@0
    60
public:
sl@0
    61
	static HBufC8* CopyL(const HBufC8& aOther) { return aOther.AllocL();}
sl@0
    62
	static HBufC8* ReadFromStreamL(RReadStream& aStream) { return HBufC8::NewL(aStream, KMaxTInt); }
sl@0
    63
	static HBufC8* ReadFromStreamLC(RReadStream& aStream) { return HBufC8::NewLC(aStream, KMaxTInt); }
sl@0
    64
	static void WriteToStreamL(const HBufC8& aItem, RWriteStream& aStream) { aStream << aItem; }
sl@0
    65
	};
sl@0
    66
sl@0
    67
// Size calculator - internally required by RPointerArray functions
sl@0
    68
sl@0
    69
template <typename T>
sl@0
    70
TInt GetObjectBufferSizeL(const T& aObject)
sl@0
    71
	{
sl@0
    72
	TInt size(0);
sl@0
    73
	RNullWriteStream nullstream;
sl@0
    74
	CleanupClosePushL(nullstream);
sl@0
    75
	nullstream << aObject;
sl@0
    76
	nullstream.CommitL();
sl@0
    77
	size = nullstream.BytesWritten();
sl@0
    78
	CleanupStack::PopAndDestroy(&nullstream);
sl@0
    79
	return size;
sl@0
    80
	}
sl@0
    81
sl@0
    82
template <class T>
sl@0
    83
TInt ExternalizedBufferSizeL(const RPointerArray<T>& aArray)
sl@0
    84
/**
sl@0
    85
	Calculates how much buffer is required for a pointers array to be externalized.
sl@0
    86
	@param aArray The pointer array whose buffer size will be calculated.
sl@0
    87
	@return Returns the number of bytes occupied by the elements of the pointer array.
sl@0
    88
 */
sl@0
    89
	{
sl@0
    90
	TInt totalSize(1);	// 1-byte is reserved for the array count
sl@0
    91
	TInt count = aArray.Count();
sl@0
    92
	
sl@0
    93
	for (TInt i = 0; i < count; ++i)
sl@0
    94
			{
sl@0
    95
			totalSize += GetObjectBufferSizeL(*aArray[i]);
sl@0
    96
			}
sl@0
    97
	return totalSize;
sl@0
    98
	}
sl@0
    99
sl@0
   100
sl@0
   101
// RPointerArray utilities
sl@0
   102
sl@0
   103
template <class T>
sl@0
   104
void CopyPointerArrayL(RPointerArray<T>& aTarget, const RPointerArray<T>& aSource)
sl@0
   105
	{
sl@0
   106
	TInt arrayCount =  aSource.Count();
sl@0
   107
	for (TInt i = 0; i < arrayCount; ++i)
sl@0
   108
		{
sl@0
   109
		T* item = TTraits<T>::CopyL(*aSource[i]);
sl@0
   110
		CleanupStack::PushL(item);
sl@0
   111
		User::LeaveIfError(aTarget.Append(item));
sl@0
   112
		CleanupStack::Pop(item);
sl@0
   113
		}
sl@0
   114
	}
sl@0
   115
	
sl@0
   116
template <class T>
sl@0
   117
HBufC8* ExternalizePointersArrayLC(const RPointerArray<T>& aArray, TBool aAddLength)
sl@0
   118
	{
sl@0
   119
	TInt arraySize = ExternalizedBufferSizeL(aArray);
sl@0
   120
	HBufC8 *arrayBuf = HBufC8::NewLC(arraySize + sizeof(TInt32));
sl@0
   121
	TPtr8 arrayPtr(arrayBuf->Des());
sl@0
   122
	
sl@0
   123
	RDesWriteStream stream(arrayPtr);
sl@0
   124
	stream.PushL();
sl@0
   125
	ExternalizePointersArrayL(aArray, stream, aAddLength);
sl@0
   126
	stream.Pop();
sl@0
   127
	stream.Release();
sl@0
   128
	return arrayBuf;
sl@0
   129
	}
sl@0
   130
sl@0
   131
template <class T>
sl@0
   132
void ExternalizePointersArrayL(const RPointerArray<T>& aArray, RWriteStream& aStream, TBool aAddLength)
sl@0
   133
	{
sl@0
   134
	TInt arrayCount = aArray.Count();
sl@0
   135
	if(aAddLength)
sl@0
   136
		aStream << TCardinality(arrayCount);
sl@0
   137
	for (TInt i = 0; i < arrayCount; ++i)
sl@0
   138
		{
sl@0
   139
		TTraits<T>::WriteToStreamL(*aArray[i], aStream);
sl@0
   140
		}
sl@0
   141
	aStream.CommitL();
sl@0
   142
	}
sl@0
   143
sl@0
   144
template <class T>
sl@0
   145
void InternalizePointersArrayL(RPointerArray<T>& aArray, RReadStream& aStream, TBool aAddLength)
sl@0
   146
	{
sl@0
   147
	TInt count (0);
sl@0
   148
	if(aAddLength)
sl@0
   149
		{
sl@0
   150
		TCardinality c;
sl@0
   151
		aStream >> c;
sl@0
   152
		count = c;
sl@0
   153
		}
sl@0
   154
	for (TInt i = 0; i < count; ++i)
sl@0
   155
		{
sl@0
   156
		T* item = TTraits<T>::ReadFromStreamL(aStream);
sl@0
   157
		CleanupStack::PushL(item);
sl@0
   158
		User::LeaveIfError(aArray.Append(item));
sl@0
   159
		CleanupStack::Pop(item);
sl@0
   160
		}
sl@0
   161
	}
sl@0
   162
sl@0
   163
sl@0
   164
// Size calculator - internally required by RArray/RPointerArray functions
sl@0
   165
sl@0
   166
template <class T>
sl@0
   167
TInt ExternalizedBufferSizeL(const RArray<T>& aArray)
sl@0
   168
/**
sl@0
   169
	Calculates how much buffer is required for a fixed length array to be externalized.
sl@0
   170
	@param aArray The array whose buffer size will be calculated.
sl@0
   171
	@return Returns the number of bytes of the array's dynamic memory.
sl@0
   172
 */
sl@0
   173
	{
sl@0
   174
	TInt totalSize(1); // 1-byte is reserved for the array count
sl@0
   175
	TInt count = aArray.Count();
sl@0
   176
sl@0
   177
	for (TInt i = 0; i < count; ++i)
sl@0
   178
			{
sl@0
   179
			totalSize += GetObjectBufferSizeL(TPckgC<T>(aArray[i]));
sl@0
   180
			}
sl@0
   181
	return totalSize;
sl@0
   182
	}
sl@0
   183
sl@0
   184
sl@0
   185
// RArray utilities
sl@0
   186
sl@0
   187
template <class T>
sl@0
   188
void CopyFixedLengthArrayL(RArray<T>& aTarget, const RArray<T>& aSource)
sl@0
   189
	{
sl@0
   190
	TInt arrayCount = aSource.Count();
sl@0
   191
	for (TInt i = 0; i < arrayCount; ++i)
sl@0
   192
		{
sl@0
   193
		User::LeaveIfError(aTarget.Append(aSource[i]));
sl@0
   194
		}
sl@0
   195
	}
sl@0
   196
sl@0
   197
template <class T>
sl@0
   198
HBufC8* ExternalizeFixedLengthArrayL(const RArray<T>& aArray)
sl@0
   199
	{
sl@0
   200
	TInt arraySize = ExternalizedBufferSizeL(aArray, arraySize);
sl@0
   201
	HBufC8 *arrayBuf = HBufC8::NewLC(arraySize + sizeof(TInt32));
sl@0
   202
	TPtr8 arrayPtr(arrayBuf->Des());
sl@0
   203
	
sl@0
   204
	RDesWriteStream stream(arrayPtr);
sl@0
   205
	stream.PushL();
sl@0
   206
	ExternalizeFixedLengthArrayL(aArray, stream);
sl@0
   207
	stream.Pop();
sl@0
   208
	stream.Release();
sl@0
   209
	return arrayBuf;
sl@0
   210
	}
sl@0
   211
sl@0
   212
template <class T>
sl@0
   213
void ExternalizeFixedLengthArrayL(const RArray<T>& aArray, RWriteStream& aStream)
sl@0
   214
	{
sl@0
   215
	TInt arrayCount = aArray.Count();
sl@0
   216
	aStream << TCardinality(arrayCount);
sl@0
   217
	for (TInt i = 0; i < arrayCount; ++i)
sl@0
   218
		{
sl@0
   219
		aStream.WriteL(TPckgC<T>(aArray[i]));
sl@0
   220
		}
sl@0
   221
	aStream.CommitL();
sl@0
   222
	}
sl@0
   223
sl@0
   224
sl@0
   225
template <class T>
sl@0
   226
void InternalizeFixedLengthArrayL(RArray<T>& aArray, RReadStream& aStream)
sl@0
   227
	{
sl@0
   228
	TCardinality c;
sl@0
   229
	aStream >> c;
sl@0
   230
	TInt count = c;
sl@0
   231
	for (TInt i = 0; i < count; ++i)
sl@0
   232
		{
sl@0
   233
		T item;
sl@0
   234
		TPckg<T> itemPckg(item);
sl@0
   235
		aStream.ReadL(itemPckg);
sl@0
   236
		User::LeaveIfError(aArray.Append(item));
sl@0
   237
		}
sl@0
   238
	}
sl@0
   239
sl@0
   240
#endif /* STREAMINGARRAY_INL*/