os/security/contentmgmt/contentaccessfwfordrm/source/cafutils/bitset.cpp
author sl
Tue, 10 Jun 2014 14:32:02 +0200
changeset 1 260cb5ec6c19
permissions -rw-r--r--
Update contrib.
sl@0
     1
/*
sl@0
     2
* Copyright (c) 2003-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
*
sl@0
    16
*/
sl@0
    17
sl@0
    18
sl@0
    19
#ifndef REMOVE_CAF1
sl@0
    20
sl@0
    21
#include <e32std.h>
sl@0
    22
#include <s32strm.h>
sl@0
    23
#include <stdarg.h>
sl@0
    24
#include <caf/caftypes.h>
sl@0
    25
#include <caf/bitset.h>
sl@0
    26
sl@0
    27
using namespace ContentAccess;
sl@0
    28
sl@0
    29
EXPORT_C CBitset* CBitset::NewLC(TInt aMaxBits)
sl@0
    30
	{
sl@0
    31
	CBitset* self = new(ELeave) CBitset();
sl@0
    32
	CleanupStack::PushL(self);
sl@0
    33
	self->ConstructL(aMaxBits);
sl@0
    34
	return self;
sl@0
    35
	}
sl@0
    36
sl@0
    37
EXPORT_C CBitset* CBitset::NewL(TInt aMaxBits)
sl@0
    38
	{
sl@0
    39
	CBitset* self=CBitset::NewLC(aMaxBits);
sl@0
    40
	CleanupStack::Pop(self);
sl@0
    41
	return self;
sl@0
    42
	}
sl@0
    43
sl@0
    44
EXPORT_C CBitset* CBitset::NewLC(const CBitset& aSource)
sl@0
    45
	{
sl@0
    46
	CBitset* self = new (ELeave) CBitset(aSource);
sl@0
    47
	CleanupStack::PushL(self);
sl@0
    48
	self->ConstructL(aSource);
sl@0
    49
	return self;
sl@0
    50
	}
sl@0
    51
sl@0
    52
EXPORT_C CBitset* CBitset::NewL(const CBitset& aSource)
sl@0
    53
	{
sl@0
    54
	CBitset* self=CBitset::NewLC(aSource);
sl@0
    55
	CleanupStack::Pop(self);
sl@0
    56
	return self;
sl@0
    57
	}
sl@0
    58
sl@0
    59
CBitset::~CBitset() 
sl@0
    60
	{ 
sl@0
    61
	delete [] iBitSet;
sl@0
    62
	}
sl@0
    63
sl@0
    64
CBitset::CBitset()
sl@0
    65
	{
sl@0
    66
	}
sl@0
    67
sl@0
    68
CBitset::CBitset(const CBitset& aSource)
sl@0
    69
	: iWidth(aSource.iWidth), iMaxBits(aSource.iMaxBits)
sl@0
    70
	{
sl@0
    71
	}
sl@0
    72
sl@0
    73
void CBitset::IdentifyBit(TInt aBitId, // in
sl@0
    74
						  TInt &aByte, // out
sl@0
    75
						  TInt &aBitInByte) const // out
sl@0
    76
	{
sl@0
    77
	// Identify the byte & bit corresponding to the Id
sl@0
    78
	aByte = aBitId / 8;
sl@0
    79
	aBitInByte  = aBitId % 8;
sl@0
    80
	}
sl@0
    81
sl@0
    82
EXPORT_C void CBitset::Invert()
sl@0
    83
	{
sl@0
    84
	// Invert array
sl@0
    85
	for (TInt j = 0; j < iWidth ; j++)
sl@0
    86
		{
sl@0
    87
		iBitSet[j] = static_cast<TUint8> (~iBitSet[j]);
sl@0
    88
		}
sl@0
    89
	}
sl@0
    90
sl@0
    91
EXPORT_C TInt CBitset::MaxBits() const
sl@0
    92
	{
sl@0
    93
	return iMaxBits;
sl@0
    94
	}
sl@0
    95
sl@0
    96
EXPORT_C void CBitset::Set(TInt aBit)
sl@0
    97
	{
sl@0
    98
	__ASSERT_ALWAYS(ValidBit(aBit), User::Panic(KCafUtilsPanic, EInvalidBit));
sl@0
    99
	
sl@0
   100
	TInt targetByte;
sl@0
   101
	TInt targetBit;
sl@0
   102
	IdentifyBit(aBit, targetByte, targetBit);
sl@0
   103
sl@0
   104
	// Or-in the relevant bit
sl@0
   105
	iBitSet[targetByte] |= (1 << targetBit);
sl@0
   106
	}
sl@0
   107
sl@0
   108
EXPORT_C void CBitset::Unset(TInt aBit)
sl@0
   109
	{
sl@0
   110
	__ASSERT_ALWAYS(ValidBit(aBit), User::Panic(KCafUtilsPanic, EInvalidBit));
sl@0
   111
	TInt targetByte;
sl@0
   112
	TInt targetBit;
sl@0
   113
	IdentifyBit(aBit, targetByte, targetBit);
sl@0
   114
sl@0
   115
	// And-in NOT(the relevant bit)
sl@0
   116
	iBitSet[targetByte] &= ~(1 << targetBit);
sl@0
   117
	}
sl@0
   118
sl@0
   119
EXPORT_C TBool CBitset::IsSet(TInt aBit) const
sl@0
   120
	{
sl@0
   121
	__ASSERT_ALWAYS(ValidBit(aBit), User::Panic(KCafUtilsPanic, EInvalidBit));
sl@0
   122
	
sl@0
   123
	TInt targetByte;
sl@0
   124
	TInt targetBit;
sl@0
   125
	IdentifyBit(aBit, targetByte, targetBit);
sl@0
   126
sl@0
   127
	// And-in the relevant bit and test for non-zero
sl@0
   128
	if (iBitSet[targetByte] & (1 << targetBit))
sl@0
   129
		{
sl@0
   130
		return(ETrue);
sl@0
   131
		}
sl@0
   132
	else
sl@0
   133
		{
sl@0
   134
		return(EFalse);
sl@0
   135
		}
sl@0
   136
	}
sl@0
   137
sl@0
   138
EXPORT_C void CBitset::Reset()
sl@0
   139
	{
sl@0
   140
	// Reset the array
sl@0
   141
	Mem::FillZ(iBitSet, iWidth);
sl@0
   142
	}
sl@0
   143
sl@0
   144
EXPORT_C void CBitset::SetAll()
sl@0
   145
	{
sl@0
   146
	Mem::Fill(iBitSet, iWidth, 0xFF);
sl@0
   147
	}
sl@0
   148
sl@0
   149
void CBitset::ConstructL(TInt aMaxBits)
sl@0
   150
	{
sl@0
   151
	// Calculate the required number of bytes
sl@0
   152
	iMaxBits = aMaxBits;
sl@0
   153
	iWidth          = 1 + aMaxBits / 8;
sl@0
   154
sl@0
   155
	iBitSet = new (ELeave) TUint8[iWidth];
sl@0
   156
	Reset(); // Zero the set
sl@0
   157
    }
sl@0
   158
sl@0
   159
void CBitset::ConstructL(const CBitset& aSource)
sl@0
   160
	{
sl@0
   161
	// Copy the bits of the source
sl@0
   162
	iBitSet = new (ELeave) TUint8[iWidth];
sl@0
   163
	Mem::Copy(iBitSet, aSource.iBitSet, iWidth);
sl@0
   164
    }
sl@0
   165
sl@0
   166
EXPORT_C void CBitset::ExternalizeL(RWriteStream& aStream) const
sl@0
   167
	{
sl@0
   168
	// Write the max-Id
sl@0
   169
	aStream.WriteInt32L(iMaxBits);
sl@0
   170
	// Create a descriptor to encapsulate the set
sl@0
   171
	TPtrC8 ptr(iBitSet, iWidth);
sl@0
   172
	// Write the descriptor
sl@0
   173
	aStream.WriteL(ptr);
sl@0
   174
	}
sl@0
   175
sl@0
   176
EXPORT_C void CBitset::InternalizeL(RReadStream& aStream)
sl@0
   177
	{
sl@0
   178
	// Delete the array 
sl@0
   179
	delete [] iBitSet;
sl@0
   180
	iBitSet = NULL; // safety
sl@0
   181
	// Call the constructor with the max-Id value
sl@0
   182
	ConstructL(aStream.ReadInt32L());
sl@0
   183
	// Create a descriptor to encapsulate the set
sl@0
   184
	TPtr8 ptr(iBitSet, 0, iWidth);
sl@0
   185
	// Load into the descriptor
sl@0
   186
	aStream.ReadL(ptr);
sl@0
   187
	}
sl@0
   188
sl@0
   189
//
sl@0
   190
// NB: leavescan will complain that this method calls a leaving
sl@0
   191
// function. The Leave is properly documented in the header
sl@0
   192
// file. Other than that, not much can be done, especially this
sl@0
   193
// class has already been deprecated.
sl@0
   194
EXPORT_C CBitset& CBitset::operator=(const CBitset& aSource)
sl@0
   195
	{
sl@0
   196
	// avoid self-assignment
sl@0
   197
	if (this == &aSource)
sl@0
   198
		{
sl@0
   199
		return *this;
sl@0
   200
		}
sl@0
   201
	
sl@0
   202
	// check for source/dest size mismatch
sl@0
   203
	if (aSource.iWidth != iWidth)
sl@0
   204
		{
sl@0
   205
		// always use largest width
sl@0
   206
		TInt widest = iWidth; 
sl@0
   207
		if (widest < aSource.iWidth)
sl@0
   208
			{
sl@0
   209
			widest = aSource.iWidth;
sl@0
   210
			}
sl@0
   211
		
sl@0
   212
		// Malloc the desired width
sl@0
   213
		TUint8 *temp = (TUint8 *) new (ELeave) TUint8[widest];
sl@0
   214
		
sl@0
   215
		// And zero
sl@0
   216
		Mem::FillZ(temp, widest);
sl@0
   217
			  
sl@0
   218
		// Now it is safe to re-assign the set
sl@0
   219
		delete [] iBitSet;
sl@0
   220
		iBitSet = temp;
sl@0
   221
		iWidth = widest;
sl@0
   222
		}
sl@0
   223
sl@0
   224
	// Width may be the same, but the Max-Id may not, so
sl@0
   225
	// align these here
sl@0
   226
	iMaxBits = aSource.iMaxBits;
sl@0
   227
sl@0
   228
	// Based on the source-size, copy the bitset
sl@0
   229
	Mem::Copy(iBitSet, aSource.iBitSet, aSource.iWidth);
sl@0
   230
sl@0
   231
	return *this;
sl@0
   232
	}
sl@0
   233
sl@0
   234
EXPORT_C TBool CBitset::operator==(const CBitset& aSource) const
sl@0
   235
	{
sl@0
   236
	TBool result = ETrue;
sl@0
   237
	if (this == &aSource)
sl@0
   238
		{
sl@0
   239
		return ETrue;
sl@0
   240
		}
sl@0
   241
	TInt narrowest = iMaxBits; 
sl@0
   242
	if (narrowest > aSource.iMaxBits)
sl@0
   243
		{
sl@0
   244
		narrowest = aSource.iMaxBits;
sl@0
   245
		}
sl@0
   246
sl@0
   247
	// Compare the first "narrowest/8" bytes - if the result is non-zero,
sl@0
   248
	// then they are not equal
sl@0
   249
	if (Mem::Compare(iBitSet, narrowest/8, aSource.iBitSet, narrowest/8))
sl@0
   250
		{
sl@0
   251
		return EFalse;
sl@0
   252
		}
sl@0
   253
	
sl@0
   254
	// Now, compare the last set of bits manually
sl@0
   255
	for (TInt i = narrowest/8 * 8; i < narrowest; ++i)
sl@0
   256
		{
sl@0
   257
		// Comparison of booleans - compare their negations
sl@0
   258
		if (!IsSet(i) != !aSource.IsSet(i))
sl@0
   259
			{
sl@0
   260
			return EFalse;
sl@0
   261
			}
sl@0
   262
		}
sl@0
   263
		
sl@0
   264
	return result;
sl@0
   265
	}
sl@0
   266
sl@0
   267
EXPORT_C void CBitset::SetListL(TInt aItems, ...)
sl@0
   268
	{
sl@0
   269
	va_list ap;
sl@0
   270
	va_start(ap, aItems);
sl@0
   271
sl@0
   272
	// Create a new set (of identical size)
sl@0
   273
	CBitset *tempSet = CBitset::NewLC(iMaxBits);
sl@0
   274
sl@0
   275
	// Duplicate this into temporary bitset
sl@0
   276
	*tempSet = *this;
sl@0
   277
	
sl@0
   278
	for (TInt i = 0; i < aItems; i++)
sl@0
   279
		{
sl@0
   280
		TInt x = va_arg(ap, TInt);
sl@0
   281
		tempSet->Set(x);
sl@0
   282
		}
sl@0
   283
	
sl@0
   284
	// No failure, so copy back into this
sl@0
   285
	*this = *tempSet;
sl@0
   286
	va_end(ap);
sl@0
   287
	
sl@0
   288
	CleanupStack::Pop(tempSet);
sl@0
   289
	delete tempSet;
sl@0
   290
	}
sl@0
   291
sl@0
   292
EXPORT_C TBool CBitset::IsSetList(TInt aItems, ...) const
sl@0
   293
	{
sl@0
   294
	va_list ap;
sl@0
   295
	va_start(ap, aItems);
sl@0
   296
sl@0
   297
	for (TInt i = 0; i < aItems; i++)
sl@0
   298
		{
sl@0
   299
		TInt x = va_arg(ap, TInt);
sl@0
   300
		if (!IsSet(x))
sl@0
   301
			{
sl@0
   302
			return (EFalse);
sl@0
   303
			}
sl@0
   304
		}
sl@0
   305
	va_end(ap);
sl@0
   306
	return (ETrue);
sl@0
   307
	}
sl@0
   308
sl@0
   309
#endif // #ifndef REMOVE_CAF1